Skip to content

Commit d019d04

Browse files
author
Shakeel Mohamed
committed
Code cleanup, created a utils file in splunklib/modularinput
1 parent f4d70f2 commit d019d04

5 files changed

Lines changed: 98 additions & 91 deletions

File tree

‎splunklib/modularinput/argument.py‎

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,11 @@
1919

2020
class Argument(object):
2121
"""Class representing an argument to a modular input kind.
22+
2223
Argument is meant to be used with Scheme to generate an XML definition of the modular input
23-
kind that Splunk understands."""
24+
kind that Splunk understands.
2425
25-
# Constant values, do not change
26-
dataTypeBoolean = "BOOLEAN"
27-
dataTypeNumber = "NUMBER"
28-
dataTypeString = "STRING"
29-
30-
def __init__(self, name, description=None, validation=None, \
31-
dataType=dataTypeString, requiredOnEdit=False, requiredOnCreate=False):
32-
"""name is the only required parameter for the constructor
26+
name is the only required parameter for the constructor
3327
3428
Example with least parameters:
3529
@@ -41,29 +35,40 @@ def __init__(self, name, description=None, validation=None, \
4135
name="arg2",
4236
description="This is an argument with lots of parameters",
4337
validation="is_pos_int('some_name')",
44-
dataType=Argument.dataTypeNumber,
45-
requiredOnEdit=True,
46-
requiredOnCreate=True
38+
data_type=Argument.data_type_number,
39+
required_on_edit=True,
40+
required_on_create=True
4741
)
42+
"""
4843

44+
# Constant values, do not change
45+
data_type_boolean = "BOOLEAN"
46+
data_type_number = "NUMBER"
47+
data_type_string = "STRING"
48+
49+
def __init__(self, name, description=None, validation=None, \
50+
data_type=data_type_string, required_on_edit=False, required_on_create=False):
51+
"""
4952
:param name: string, identifier for this argument in Splunk
5053
:param description: string, human readable description of the argument
51-
:param validation: string, specifying how the argument should be validated, if using internal validation. If using
52-
external validation, this will be ignored.
53-
:param dataType: string, data type of this field; use the class constants
54-
dataTypeBoolean, dataTypeNumber, or dataTypeString
55-
:param requiredOnEdit: boolean, is this arg required when editing an existing modular input of this kind?
56-
:param requiredOnCreate: boolean, is this arg required when creating a modular input of this kind?
54+
:param validation: string, specifying how the argument should be validated, if using internal validation.
55+
If using external validation, this will be ignored.
56+
:param data_type: string, data type of this field; use the class constants
57+
data_type_boolean, data_type_number, or data_type_string
58+
:param required_on_edit: boolean, is this arg required when editing an existing modular input of this kind?
59+
:param required_on_create: boolean, is this arg required when creating a modular input of this kind?
5760
"""
5861
self.name = name
5962
self.description = description
6063
self.validation = validation
61-
self.dataType = dataType
62-
self.requiredOnEdit = requiredOnEdit
63-
self.requiredOnCreate = requiredOnCreate
64+
self.data_type = data_type
65+
self.required_on_edit = required_on_edit
66+
self.required_on_create = required_on_create
6467

6568
def add_to_document(self, parent):
66-
"""Adds an <arg> SubElement to the Parent Element, typically <args>
69+
"""Add an Argument object to this ElementTree Document
70+
71+
Adds an <arg> SubElement to the Parent Element, typically <args>
6772
and setup its subelements with their respective text
6873
6974
:param parent: an ET.Element to be the parent of a new <arg> SubElement
@@ -75,11 +80,11 @@ def add_to_document(self, parent):
7580
if self.description is not None:
7681
ET.SubElement(arg, "description").text = self.description
7782

78-
if self.validation:
83+
if self.validation is not None:
7984
ET.SubElement(arg, "validation").text = self.validation
8085

81-
ET.SubElement(arg, "data_type").text = self.dataType.lower()
82-
ET.SubElement(arg, "required_on_edit").text = str(self.requiredOnEdit).lower()
83-
ET.SubElement(arg, "required_on_create").text = str(self.requiredOnCreate).lower()
86+
ET.SubElement(arg, "data_type").text = self.data_type.lower()
87+
ET.SubElement(arg, "required_on_edit").text = str(self.required_on_edit).lower()
88+
ET.SubElement(arg, "required_on_create").text = str(self.required_on_create).lower()
8489

8590
return arg

‎splunklib/modularinput/scheme.py‎

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ class Scheme(object):
2727
"""
2828

2929
# Constant values, do not change
30-
streamingModeSimple = "SIMPLE"
31-
streamingModeXML = "XML"
30+
streaming_mode_simple = "SIMPLE"
31+
streaming_mode_xml = "XML"
3232

3333
def __init__(self, title):
3434
"""
3535
:param title: string identifier for this Scheme in Splunk
3636
"""
3737
self.title = title
3838
self.description = None
39-
self.useExternalValidation = True
39+
self.use_external_validation = True
4040
self.useSingleInstance = False
41-
self.streamingMode = Scheme.streamingModeXML
41+
self.streamingMode = Scheme.streaming_mode_xml
4242

4343
# list of Argument objects, each to be represented by an <arg> tag
4444
self.arguments = []
@@ -57,23 +57,16 @@ def to_XML(self):
5757
"""
5858
root = ET.Element("scheme")
5959

60-
title = ET.SubElement(root, "title")
61-
title.text = self.title
60+
ET.SubElement(root, "title").text = self.title
6261

6362
# add a description subelement if it's defined
6463
if self.description is not None:
65-
description = ET.SubElement(root, "description")
66-
description.text = self.description
64+
ET.SubElement(root, "description").text = self.description
6765

6866
# add other subelements
69-
use_external_validation = ET.SubElement(root, "use_external_validation")
70-
use_external_validation.text = str(self.useExternalValidation).lower()
71-
72-
use_single_instance = ET.SubElement(root, "use_single_instance")
73-
use_single_instance.text = str(self.useSingleInstance).lower()
74-
75-
streaming_mode = ET.SubElement(root, "streaming_mode")
76-
streaming_mode.text = self.streamingMode.lower()
67+
ET.SubElement(root, "use_external_validation").text = str(self.use_external_validation).lower()
68+
ET.SubElement(root, "use_single_instance").text = str(self.useSingleInstance).lower()
69+
ET.SubElement(root, "streaming_mode").text = self.streamingMode.lower()
7770

7871
endpoint = ET.SubElement(root, "endpoint")
7972

‎splunklib/modularinput/utils.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2011-2013 Splunk, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
# File for utility functions
16+
17+
def xml_compare(expected, found):
18+
"""Checks equality of two ElementTree objects
19+
20+
:param expected: an ElementTree object
21+
:param found: an ElementTree object
22+
:return: boolean, equality of expected and found
23+
"""
24+
25+
# if comparing the same ET object
26+
if expected == found:
27+
return True
28+
29+
# compare element attributes, ignoring order
30+
if set(expected.items()) != set(found.items()):
31+
return False
32+
33+
# check for equal number of children
34+
expected_children = list(expected)
35+
found_children = list(found)
36+
if len(expected_children) != len(found_children):
37+
return False
38+
39+
# compare children
40+
if not all([xml_compare(a, b) for a, b in zip(expected_children, found_children)]):
41+
return False
42+
43+
# compare elements, if there is no text node, return True
44+
if (expected.text is None or expected.text.strip() == "") and (found.text is None or found.text.strip() == ""):
45+
return True
46+
else:
47+
return expected.tag == found.tag and expected.text == found.text and expected.attrib == found.attrib

‎tests/modularinput/modularinput_testlib.py‎

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

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

27+
from splunklib.modularinput.utils import xml_compare
28+
2729
# utility functions
2830

2931
def parse_parameters(paramNode):
@@ -47,44 +49,4 @@ def parse_xml_data(parent_node, child_node_tag):
4749
data[child.get("name")][param.get("name")] = parse_parameters(param)
4850
elif "item" == parent_node.tag:
4951
data[child.get("name")] = parse_parameters(child)
50-
return data
51-
52-
def xml_compare(expected, found):
53-
"""Checks equality of two ElementTrees
54-
55-
:param expected: an ElementTree object
56-
:param found: an ElementTree object
57-
:return: boolean, equality of expected and found
58-
"""
59-
60-
# if comparing the same ET object
61-
if expected == found:
62-
return True
63-
64-
# compare element attributes, ignoring order
65-
if set(expected.items()) != set(found.items()):
66-
return False
67-
68-
# check for equal number of children
69-
expected_children = list(expected)
70-
found_children = list(found)
71-
if len(expected_children) != len(found_children):
72-
return False
73-
74-
75-
76-
# compare children
77-
if not all([xml_compare(a, b) for a, b in zip(expected_children, found_children)]):
78-
return False
79-
80-
# compare element text if it exists, else elements are equal
81-
if expected.text and expected.text.strip() != "":
82-
if expected.tag == found.tag and expected.text == found.text:
83-
if expected.attrib == found.attrib:
84-
return True
85-
else:
86-
return False
87-
else:
88-
return False
89-
else:
90-
return True
52+
return data

‎tests/modularinput/test_scheme.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def test_generate_xml_from_scheme(self):
4040

4141
scheme = Scheme("abcd")
4242
scheme.description = u"쎼 and 쎶 and <&> für"
43-
scheme.streamingMode = Scheme.streamingModeSimple
44-
scheme.useExternalValidation = "false"
43+
scheme.streamingMode = Scheme.streaming_mode_simple
44+
scheme.use_external_validation = "false"
4545
scheme.useSingleInstance = "true"
4646

4747
arg1 = Argument(name="arg1")
@@ -51,9 +51,9 @@ def test_generate_xml_from_scheme(self):
5151
name="arg2",
5252
description=u"쎼 and 쎶 and <&> für",
5353
validation="is_pos_int('some_name')",
54-
dataType=Argument.dataTypeNumber,
55-
requiredOnEdit=True,
56-
requiredOnCreate=True
54+
data_type=Argument.data_type_number,
55+
required_on_edit=True,
56+
required_on_create=True
5757
)
5858
scheme.add_argument(arg2)
5959

@@ -82,9 +82,9 @@ def test_generate_xml_from_argument(self):
8282
name="some_name",
8383
description=u"쎼 and 쎶 and <&> für",
8484
validation="is_pos_int('some_name')",
85-
dataType=Argument.dataTypeBoolean,
86-
requiredOnEdit="true",
87-
requiredOnCreate="true"
85+
data_type=Argument.data_type_boolean,
86+
required_on_edit="true",
87+
required_on_create="true"
8888
)
8989

9090
root = ET.Element("")

0 commit comments

Comments
 (0)