Skip to content

Commit 6421962

Browse files
author
Shakeel Mohamed
committed
General code cleanup & use of pythonic code style
1 parent cf45cd4 commit 6421962

6 files changed

Lines changed: 49 additions & 319 deletions

File tree

‎splunklib/modularinput/input_definition.py‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,32 @@
1717
from malformed_data_exception import MalformedDataException
1818

1919
class InputDefinition:
20-
""" InputDefinition encodes the XML defining inputs that Splunk passes to
21-
a modular input script
22-
23-
``InputDefinition`` takes no arguments.
20+
"""
21+
InputDefinition encodes the XML defining inputs that Splunk passes to
22+
a modular input script
2423
25-
**Example**::
24+
**Example**::
2625
27-
i = InputDefinition()
26+
i = InputDefinition()
2827
2928
"""
3029
def __init__ (self):
3130
self.metadata = {}
3231
self.inputs = {}
3332

34-
def parseDefinition(self, stream):
35-
""" Parse a stream containing XML into an InputDefinition.
33+
def parse_definition(self, stream):
34+
"""
35+
Parse a stream containing XML into an InputDefinition.
3636
3737
:param stream: stream containing XML to parse
3838
:return: definition: a InputDefinition object
3939
"""
40-
if not isinstance(stream, file):
41-
raise ValueError("Stream is invalid, cannot be parsed")
4240
try:
4341
definition = InputDefinition()
4442

45-
# read everything from the stream
46-
config_str = stream.read()
43+
# parse XML from the stream, then get the root node
44+
root = ET.parse(stream).getroot()
4745

48-
# parse the configuration XML
49-
root = ET.fromstring(config_str)
5046
for node in root:
5147
if node.tag == "configuration":
5248
# get config for each stanza
@@ -57,17 +53,18 @@ def parseDefinition(self, stream):
5753
for param in config_stanza:
5854
definition.parseParameters(param, config_stanza.get("name"))
5955
else:
60-
raise MalformedDataException, "Invalid configuration scheme, "+config_stanza.tag+" tag unexpected."
56+
raise MalformedDataException("Invalid configuration scheme, %s tag unexpected." % config_stanza.tag)
6157
else:
6258
definition.metadata[node.tag] = node.text
6359
return definition
6460
except MalformedDataException:
6561
raise
66-
except Exception, e:
62+
except Exception as e:
6763
raise Exception, "Error getting configuration: %s" % str(e)
6864

6965
def parseParameters(self, paramNode, stanzaName):
70-
"""A helper function to clean up the code for parsing XML parameters
66+
"""
67+
A helper function to clean up the code for parsing XML parameters
7168
7269
:param paramNode: XML node, it should have param or param_list tag
7370
:param stanzaName: string of the stanza name containing the node
@@ -80,10 +77,9 @@ def parseParameters(self, paramNode, stanzaName):
8077
for mvp in paramNode:
8178
self.inputs[stanzaName][paramNode.get("name")].append(mvp.text)
8279
else:
83-
raise MalformedDataException, "Invalid configuration scheme, "+paramNode.tag+" tag unexpected."
84-
80+
raise MalformedDataException("Invalid configuration scheme, %s tag unexpected." % paramNode.tag)
8581

86-
def __eq__(self,other):
82+
def __eq__(self, other):
8783
if not isinstance(other, InputDefinition):
8884
return False
8985
return self.metadata == other.metadata and self.inputs == other.inputs

‎splunklib/modularinput/validation_definition.py‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
from malformed_data_exception import MalformedDataException
2121

2222
class ValidationDefinition(object):
23-
"""This class represents the XML sent by Splunk for external validation of a new modular input.
24-
25-
``ValidationDefinition`` takes no arguments.
23+
"""
24+
This class represents the XML sent by Splunk for external validation of a new modular input.
2625
2726
**Example**::
2827
@@ -33,7 +32,9 @@ def __init__(self):
3332
self.parameters = {}
3433

3534
def parseDefintion(self, stream):
36-
"""Create a ValidationDefinition from a provided stream containing XML. The XML typically will look like
35+
"""
36+
Creates a ValidationDefinition from a provided stream containing XML.
37+
The XML typically will look like
3738
3839
<items>
3940
<server_host>myHost</server_host>
@@ -49,20 +50,16 @@ def parseDefintion(self, stream):
4950
</param_list>
5051
</item>
5152
</items>
53+
5254
:param stream: stream containing XML to parse
5355
:return definition: a ValidationDefinition object
5456
"""
55-
56-
if not isinstance(stream, file):
57-
raise ValueError("Stream is invalid, cannot be parsed")
5857
try:
5958
definition = ValidationDefinition()
6059

61-
# read everything from the stream
62-
validation_str = stream.read()
60+
# parse XML from the stream, then get the root node
61+
root = ET.parse(stream).getroot()
6362

64-
# parse the validation XML
65-
root = ET.fromstring(validation_str)
6663
for node in root:
6764
# lone item node
6865
if node.tag == "item":
@@ -78,16 +75,17 @@ def parseDefintion(self, stream):
7875
for multi_val_param in param_node:
7976
definition.parameters[param_node.get("name")].append(multi_val_param.text)
8077
else:
81-
raise MalformedDataException, "Invalid configuration scheme, "+param_node.tag+" tag unexpected."
78+
raise MalformedDataException("Invalid configuration scheme, %s tag unexpected." % param_node.tag)
8279
else:
8380
# Store anything else in metadata
8481
definition.metadata[node.tag] = node.text
8582
return definition
86-
except Exception, e:
83+
except Exception as e:
8784
raise Exception, "Error getting validation definition: %s" % str(e)
8885

8986
def __eq__(self, other):
90-
"""Test the equality of self, and other by comparing metadata and parameters
87+
"""
88+
Test the equality of self and other by comparing metadata and parameters
9189
9290
:param other: a ValidationDefinition object
9391
:return boolean: True for equality, else False

‎tests/modularinput/test_input_definition.py‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@
1515
# under the License.
1616

1717
try:
18-
import unittest
19-
except ImportError:
2018
import unittest2 as unittest
19+
except ImportError:
20+
import unittest
2121
import sys
2222
from splunklib.modularinput.input_definition import InputDefinition
2323
from splunklib.modularinput.malformed_data_exception import MalformedDataException
24+
2425
sys.path.insert(0, "../..")
2526

2627
class InputDefinitionTestCase(unittest.TestCase):
2728

2829
def setUp(self):
2930
super(InputDefinitionTestCase, self).setUp()
3031

31-
def test_parse_zero_inputs(self):
32-
"""Check equality of parsed InputDefinition object from known XML
33-
with 1 input defined with manually initialized Input Definition object."""
34-
found = InputDefinition().parseDefinition(open("data/conf_with_0_inputs.xml"))
32+
def test_parse_inputdef_with_zero_inputs(self):
33+
found = InputDefinition().parse_definition(open("data/conf_with_0_inputs.xml"))
3534

3635
expectedDefinition = InputDefinition()
3736
expectedDefinition.metadata = {
@@ -43,10 +42,8 @@ def test_parse_zero_inputs(self):
4342

4443
self.assertEqual(found, expectedDefinition)
4544

46-
def test_parse_two_inputs(self):
47-
"""Check equality of parsed InputDefinition object from known XML
48-
with 2 inputs definedwith manually initialized Input Definition object."""
49-
found = InputDefinition().parseDefinition(open("data/conf_with_2_inputs.xml"))
45+
def test_parse_inputdef_with_two_inputs(self):
46+
found = InputDefinition().parse_definition(open("data/conf_with_2_inputs.xml"))
5047

5148
expectedDefinition = InputDefinition()
5249
expectedDefinition.metadata = {
@@ -73,15 +70,13 @@ def test_parse_two_inputs(self):
7370
self.assertEqual(expectedDefinition, found)
7471

7572

76-
def test_parse_malformed_input_definition(self):
77-
"""Check that parsing an InputDefinition from malformed XML produces the expected exception."""
73+
def test_attempt_to_parse_malformed_input_definition_will_throw_exception(self):
74+
"""
75+
Check that parsing an InputDefinition from malformed XML produces the expected exception.
76+
"""
7877
with self.assertRaises(MalformedDataException):
79-
found = InputDefinition().parseDefinition(open("data/conf_with_invalid_inputs.xml"))
78+
found = InputDefinition().parse_definition(open("data/conf_with_invalid_inputs.xml"))
8079

8180

8281
if __name__ == "__main__":
83-
try:
84-
import unittest2 as unittest
85-
except ImportError:
86-
import unittest
8782
unittest.main()

‎tests/modularinput/test_validation_definition.py‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515
# under the License.
1616

1717
try:
18-
import unittest
19-
except ImportError:
2018
import unittest2 as unittest
19+
except ImportError:
20+
import unittest
2121
import sys
2222
from splunklib.modularinput.validation_definition import ValidationDefinition
23+
2324
sys.path.insert(0, "../..")
2425

2526
class ValidationDefinitionTestCase(unittest.TestCase):
26-
"""Check equality of parsed ValidationDefinition object from known XML
27-
with manually initialized ValidationDefinition object."""
27+
"""
28+
Check equality of parsed ValidationDefinition object from known XML
29+
with manually initialized ValidationDefinition object.
30+
"""
2831
def setUp(self):
2932
super(ValidationDefinitionTestCase, self).setUp()
3033

@@ -44,8 +47,4 @@ def test_validation_definition_parse(self):
4447
self.assertEqual(expected, found)
4548

4649
if __name__ == "__main__":
47-
try:
48-
import unittest2 as unittest
49-
except ImportError:
50-
import unittest
5150
unittest.main()

0 commit comments

Comments
 (0)