Skip to content

Commit 739a94a

Browse files
authored
Merge pull request #453 from splunk/py3-modularinput-refactor
Py3 modularinput refactor
2 parents b0ca411 + 308873b commit 739a94a

File tree

8 files changed

+25
-57
lines changed

8 files changed

+25
-57
lines changed

splunklib/modularinput/argument.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from __future__ import absolute_import
16-
try:
17-
import xml.etree.ElementTree as ET
18-
except ImportError:
19-
import xml.etree.cElementTree as ET
15+
import xml.etree.ElementTree as ET
2016

21-
class Argument(object):
17+
18+
class Argument:
2219
"""Class representing an argument to a modular input kind.
2320
2421
``Argument`` is meant to be used with ``Scheme`` to generate an XML
@@ -100,4 +97,4 @@ def add_to_document(self, parent):
10097
for name, value in subelements:
10198
ET.SubElement(arg, name).text = str(value).lower()
10299

103-
return arg
100+
return arg

splunklib/modularinput/event.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from __future__ import absolute_import
1615
from io import TextIOBase
16+
import xml.etree.ElementTree as ET
1717
from splunklib.six import ensure_text
1818

19-
try:
20-
import xml.etree.cElementTree as ET
21-
except ImportError as ie:
22-
import xml.etree.ElementTree as ET
2319

24-
class Event(object):
20+
class Event:
2521
"""Represents an event or fragment of an event to be written by this modular input to Splunk.
2622
2723
To write an input to a stream, call the ``write_to`` function, passing in a stream.
@@ -111,4 +107,4 @@ def write_to(self, stream):
111107
stream.write(ensure_text(ET.tostring(event)))
112108
else:
113109
stream.write(ET.tostring(event))
114-
stream.flush()
110+
stream.flush()

splunklib/modularinput/event_writer.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,13 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from __future__ import absolute_import
1615
import sys
1716

1817
from splunklib.six import ensure_str
1918
from .event import ET
2019

21-
try:
22-
from splunklib.six.moves import cStringIO as StringIO
23-
except ImportError:
24-
from splunklib.six import StringIO
2520

26-
class EventWriter(object):
21+
class EventWriter:
2722
"""``EventWriter`` writes events and error messages to Splunk from a modular input.
2823
Its two important methods are ``writeEvent``, which takes an ``Event`` object,
2924
and ``log``, which takes a severity and an error message.
@@ -68,7 +63,7 @@ def log(self, severity, message):
6863
:param message: ``string``, message to log.
6964
"""
7065

71-
self._err.write("%s %s\n" % (severity, message))
66+
self._err.write(f"{severity} {message}\n")
7267
self._err.flush()
7368

7469
def write_xml_document(self, document):
@@ -83,5 +78,5 @@ def write_xml_document(self, document):
8378
def close(self):
8479
"""Write the closing </stream> tag to make this XML well formed."""
8580
if self.header_written:
86-
self._out.write("</stream>")
81+
self._out.write("</stream>")
8782
self._out.flush()

splunklib/modularinput/input_definition.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from __future__ import absolute_import
16-
try:
17-
import xml.etree.cElementTree as ET
18-
except ImportError as ie:
19-
import xml.etree.ElementTree as ET
20-
15+
import xml.etree.ElementTree as ET
2116
from .utils import parse_xml_data
2217

2318
class InputDefinition:
@@ -57,4 +52,4 @@ def parse(stream):
5752
else:
5853
definition.metadata[node.tag] = node.text
5954

60-
return definition
55+
return definition

splunklib/modularinput/scheme.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from __future__ import absolute_import
16-
try:
17-
import xml.etree.cElementTree as ET
18-
except ImportError:
19-
import xml.etree.ElementTree as ET
15+
import xml.etree.ElementTree as ET
2016

21-
class Scheme(object):
17+
18+
class Scheme:
2219
"""Class representing the metadata for a modular input kind.
2320
2421
A ``Scheme`` specifies a title, description, several options of how Splunk should run modular inputs of this
@@ -82,4 +79,4 @@ def to_xml(self):
8279
for arg in self.arguments:
8380
arg.add_to_document(args)
8481

85-
return root
82+
return root

splunklib/modularinput/script.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,18 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from __future__ import absolute_import
1615
from abc import ABCMeta, abstractmethod
17-
from splunklib.six.moves.urllib.parse import urlsplit
1816
import sys
17+
import xml.etree.ElementTree as ET
18+
from urllib.parse import urlsplit
1919

2020
from ..client import Service
2121
from .event_writer import EventWriter
2222
from .input_definition import InputDefinition
2323
from .validation_definition import ValidationDefinition
24-
from splunklib import six
2524

26-
try:
27-
import xml.etree.cElementTree as ET
28-
except ImportError:
29-
import xml.etree.ElementTree as ET
3025

31-
32-
class Script(six.with_metaclass(ABCMeta, object)):
26+
class Script(metaclass=ABCMeta):
3327
"""An abstract base class for implementing modular inputs.
3428
3529
Subclasses should override ``get_scheme``, ``stream_events``,
@@ -165,7 +159,6 @@ def validate_input(self, definition):
165159
166160
:param definition: The parameters for the proposed input passed by splunkd.
167161
"""
168-
pass
169162

170163
@abstractmethod
171164
def stream_events(self, inputs, ew):

splunklib/modularinput/utils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
# File for utility functions
1616

17-
from __future__ import absolute_import
18-
from splunklib.six.moves import zip
17+
1918
def xml_compare(expected, found):
2019
"""Checks equality of two ``ElementTree`` objects.
2120
@@ -39,7 +38,7 @@ def xml_compare(expected, found):
3938
return False
4039

4140
# compare children
42-
if not all([xml_compare(a, b) for a, b in zip(expected_children, found_children)]):
41+
if not all(xml_compare(a, b) for a, b in zip(expected_children, found_children)):
4342
return False
4443

4544
# compare elements, if there is no text node, return True
@@ -59,7 +58,7 @@ def parse_parameters(param_node):
5958
parameters.append(mvp.text)
6059
return parameters
6160
else:
62-
raise ValueError("Invalid configuration scheme, %s tag unexpected." % param_node.tag)
61+
raise ValueError(f"Invalid configuration scheme, {param_node.tag} tag unexpected.")
6362

6463
def parse_xml_data(parent_node, child_node_tag):
6564
data = {}

splunklib/modularinput/validation_definition.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@
1313
# under the License.
1414

1515

16-
from __future__ import absolute_import
17-
try:
18-
import xml.etree.cElementTree as ET
19-
except ImportError as ie:
20-
import xml.etree.ElementTree as ET
16+
import xml.etree.ElementTree as ET
2117

2218
from .utils import parse_xml_data
2319

2420

25-
class ValidationDefinition(object):
21+
class ValidationDefinition:
2622
"""This class represents the XML sent by Splunk for external validation of a
2723
new modular input.
2824
@@ -83,4 +79,4 @@ def parse(stream):
8379
# Store anything else in metadata
8480
definition.metadata[node.tag] = node.text
8581

86-
return definition
82+
return definition

0 commit comments

Comments
 (0)