Skip to content

Commit 2e5a09c

Browse files
author
Shakeel Mohamed
committed
Moved all utility functions into utils.py, tests now reference them from here; no duplicated code
1 parent d019d04 commit 2e5a09c

4 files changed

Lines changed: 27 additions & 29 deletions

File tree

‎splunklib/modularinput/input_definition.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import xml.etree.cElementTree as ET
1515
except ImportError as ie:
1616
import xml.etree.ElementTree as ET
17-
from tests.modularinput.modularinput_testlib import parse_xml_data
17+
from utils import parse_xml_data
1818

1919
class InputDefinition:
2020
"""InputDefinition encodes the XML defining inputs that Splunk passes to

‎splunklib/modularinput/utils.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,27 @@ def xml_compare(expected, found):
4444
if (expected.text is None or expected.text.strip() == "") and (found.text is None or found.text.strip() == ""):
4545
return True
4646
else:
47-
return expected.tag == found.tag and expected.text == found.text and expected.attrib == found.attrib
47+
return expected.tag == found.tag and expected.text == found.text and expected.attrib == found.attrib
48+
49+
def parse_parameters(paramNode):
50+
if paramNode.tag == "param":
51+
return paramNode.text
52+
elif paramNode.tag == "param_list":
53+
parameters = []
54+
for mvp in paramNode:
55+
parameters.append(mvp.text)
56+
return parameters
57+
else:
58+
raise ValueError("Invalid configuration scheme, %s tag unexpected." % paramNode.tag)
59+
60+
def parse_xml_data(parent_node, child_node_tag):
61+
data = {}
62+
for child in parent_node:
63+
if child.tag == child_node_tag:
64+
if child_node_tag == "stanza":
65+
data[child.get("name")] = {}
66+
for param in child:
67+
data[child.get("name")][param.get("name")] = parse_parameters(param)
68+
elif "item" == parent_node.tag:
69+
data[child.get("name")] = parse_parameters(child)
70+
return data

‎splunklib/modularinput/validation_definition.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import xml.etree.cElementTree as ET
1818
except ImportError as ie:
1919
import xml.etree.ElementTree as ET
20-
from tests.modularinput.modularinput_testlib import parse_xml_data
20+
from utils import parse_xml_data
2121

2222
class ValidationDefinition(object):
2323
"""This class represents the XML sent by Splunk for external validation of a new modular input.

‎tests/modularinput/modularinput_testlib.py‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,4 @@
2424

2525
sys.path.insert(0, os.path.join('../../splunklib', '..'))
2626

27-
from splunklib.modularinput.utils import xml_compare
28-
29-
# utility functions
30-
31-
def parse_parameters(paramNode):
32-
if paramNode.tag == "param":
33-
return paramNode.text
34-
elif paramNode.tag == "param_list":
35-
parameters = []
36-
for mvp in paramNode:
37-
parameters.append(mvp.text)
38-
return parameters
39-
else:
40-
raise ValueError("Invalid configuration scheme, %s tag unexpected." % paramNode.tag)
41-
42-
def parse_xml_data(parent_node, child_node_tag):
43-
data = {}
44-
for child in parent_node:
45-
if child.tag == child_node_tag:
46-
if child_node_tag == "stanza":
47-
data[child.get("name")] = {}
48-
for param in child:
49-
data[child.get("name")][param.get("name")] = parse_parameters(param)
50-
elif "item" == parent_node.tag:
51-
data[child.get("name")] = parse_parameters(child)
52-
return data
27+
from splunklib.modularinput.utils import xml_compare, parse_xml_data, parse_parameters

0 commit comments

Comments
 (0)