Skip to content

Py3 modularinput refactor #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions splunklib/modularinput/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import absolute_import
try:
import xml.etree.ElementTree as ET
except ImportError:
import xml.etree.cElementTree as ET
import xml.etree.ElementTree as ET

class Argument(object):

class Argument:
"""Class representing an argument to a modular input kind.

``Argument`` is meant to be used with ``Scheme`` to generate an XML
Expand Down Expand Up @@ -100,4 +97,4 @@ def add_to_document(self, parent):
for name, value in subelements:
ET.SubElement(arg, name).text = str(value).lower()

return arg
return arg
10 changes: 3 additions & 7 deletions splunklib/modularinput/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import absolute_import
from io import TextIOBase
import xml.etree.ElementTree as ET
from splunklib.six import ensure_text

try:
import xml.etree.cElementTree as ET
except ImportError as ie:
import xml.etree.ElementTree as ET

class Event(object):
class Event:
"""Represents an event or fragment of an event to be written by this modular input to Splunk.

To write an input to a stream, call the ``write_to`` function, passing in a stream.
Expand Down Expand Up @@ -111,4 +107,4 @@ def write_to(self, stream):
stream.write(ensure_text(ET.tostring(event)))
else:
stream.write(ET.tostring(event))
stream.flush()
stream.flush()
11 changes: 3 additions & 8 deletions splunklib/modularinput/event_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import absolute_import
import sys

from splunklib.six import ensure_str
from .event import ET

try:
from splunklib.six.moves import cStringIO as StringIO
except ImportError:
from splunklib.six import StringIO

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

self._err.write("%s %s\n" % (severity, message))
self._err.write(f"{severity} {message}\n")
self._err.flush()

def write_xml_document(self, document):
Expand All @@ -83,5 +78,5 @@ def write_xml_document(self, document):
def close(self):
"""Write the closing </stream> tag to make this XML well formed."""
if self.header_written:
self._out.write("</stream>")
self._out.write("</stream>")
self._out.flush()
9 changes: 2 additions & 7 deletions splunklib/modularinput/input_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import absolute_import
try:
import xml.etree.cElementTree as ET
except ImportError as ie:
import xml.etree.ElementTree as ET

import xml.etree.ElementTree as ET
from .utils import parse_xml_data

class InputDefinition:
Expand Down Expand Up @@ -57,4 +52,4 @@ def parse(stream):
else:
definition.metadata[node.tag] = node.text

return definition
return definition
11 changes: 4 additions & 7 deletions splunklib/modularinput/scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import absolute_import
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
import xml.etree.ElementTree as ET

class Scheme(object):

class Scheme:
"""Class representing the metadata for a modular input kind.

A ``Scheme`` specifies a title, description, several options of how Splunk should run modular inputs of this
Expand Down Expand Up @@ -82,4 +79,4 @@ def to_xml(self):
for arg in self.arguments:
arg.add_to_document(args)

return root
return root
13 changes: 3 additions & 10 deletions splunklib/modularinput/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,18 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import absolute_import
from abc import ABCMeta, abstractmethod
from splunklib.six.moves.urllib.parse import urlsplit
import sys
import xml.etree.ElementTree as ET
from urllib.parse import urlsplit

from ..client import Service
from .event_writer import EventWriter
from .input_definition import InputDefinition
from .validation_definition import ValidationDefinition
from splunklib import six

try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET


class Script(six.with_metaclass(ABCMeta, object)):
class Script(metaclass=ABCMeta):
"""An abstract base class for implementing modular inputs.

Subclasses should override ``get_scheme``, ``stream_events``,
Expand Down Expand Up @@ -165,7 +159,6 @@ def validate_input(self, definition):

:param definition: The parameters for the proposed input passed by splunkd.
"""
pass

@abstractmethod
def stream_events(self, inputs, ew):
Expand Down
7 changes: 3 additions & 4 deletions splunklib/modularinput/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

# File for utility functions

from __future__ import absolute_import
from splunklib.six.moves import zip

def xml_compare(expected, found):
"""Checks equality of two ``ElementTree`` objects.

Expand All @@ -39,7 +38,7 @@ def xml_compare(expected, found):
return False

# compare children
if not all([xml_compare(a, b) for a, b in zip(expected_children, found_children)]):
if not all(xml_compare(a, b) for a, b in zip(expected_children, found_children)):
return False

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

def parse_xml_data(parent_node, child_node_tag):
data = {}
Expand Down
10 changes: 3 additions & 7 deletions splunklib/modularinput/validation_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@
# under the License.


from __future__ import absolute_import
try:
import xml.etree.cElementTree as ET
except ImportError as ie:
import xml.etree.ElementTree as ET
import xml.etree.ElementTree as ET

from .utils import parse_xml_data


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

Expand Down Expand Up @@ -83,4 +79,4 @@ def parse(stream):
# Store anything else in metadata
definition.metadata[node.tag] = node.text

return definition
return definition