Skip to content

Commit 2570f3b

Browse files
authored
Merge pull request #302 from splunk/make_modinput_text_consistent
Make modinput text consistent
2 parents c2cd389 + 68f8737 commit 2570f3b

File tree

8 files changed

+171
-47
lines changed

8 files changed

+171
-47
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ before_install:
3232
env:
3333
- SPLUNK_VERSION=7.0-sdk
3434
- SPLUNK_VERSION=7.2-sdk
35+
- SPLUNK_VERSION=8.0-sdk
3536

3637
language: python
3738

splunklib/modularinput/event.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# under the License.
1414

1515
from __future__ import absolute_import
16+
from io import TextIOBase
17+
from splunklib.six import ensure_text
18+
1619
try:
1720
import xml.etree.cElementTree as ET
1821
except ImportError as ie:
@@ -104,5 +107,8 @@ def write_to(self, stream):
104107
if self.done:
105108
ET.SubElement(event, "done")
106109

107-
stream.write(ET.tostring(event))
110+
if isinstance(stream, TextIOBase):
111+
stream.write(ensure_text(ET.tostring(event)))
112+
else:
113+
stream.write(ET.tostring(event))
108114
stream.flush()

splunklib/modularinput/event_writer.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from __future__ import absolute_import
1616
import sys
1717

18+
from io import TextIOWrapper, TextIOBase
19+
from splunklib.six import ensure_text
1820
from .event import ET
1921

2022
try:
@@ -24,7 +26,6 @@
2426

2527
class EventWriter(object):
2628
"""``EventWriter`` writes events and error messages to Splunk from a modular input.
27-
2829
Its two important methods are ``writeEvent``, which takes an ``Event`` object,
2930
and ``log``, which takes a severity and an error message.
3031
"""
@@ -42,8 +43,15 @@ def __init__(self, output = sys.stdout, error = sys.stderr):
4243
:param output: Where to write the output; defaults to sys.stdout.
4344
:param error: Where to write any errors; defaults to sys.stderr.
4445
"""
45-
self._out = output
46-
self._err = error
46+
if isinstance(output, TextIOBase):
47+
self._out = output
48+
else:
49+
self._out = TextIOWrapper(output)
50+
51+
if isinstance(error, TextIOBase):
52+
self._err = error
53+
else:
54+
self._err = TextIOWrapper(error)
4755

4856
# has the opening <stream> tag been written yet?
4957
self.header_written = False
@@ -55,20 +63,18 @@ def write_event(self, event):
5563
"""
5664

5765
if not self.header_written:
58-
self._out.write(b"<stream>")
66+
self._out.write(ensure_text("<stream>"))
5967
self.header_written = True
6068

6169
event.write_to(self._out)
6270

6371
def log(self, severity, message):
6472
"""Logs messages about the state of this modular input to Splunk.
6573
These messages will show up in Splunk's internal logs.
66-
6774
:param severity: ``string``, severity of message, see severities defined as class constants.
6875
:param message: ``string``, message to log.
6976
"""
70-
71-
self._err.write(("%s %s\n" % (severity, message)).encode('utf-8'))
77+
self._err.write(ensure_text("%s %s\n" % (severity, message)))
7278
self._err.flush()
7379

7480
def write_xml_document(self, document):
@@ -77,9 +83,11 @@ def write_xml_document(self, document):
7783
7884
:param document: An ``ElementTree`` object.
7985
"""
80-
self._out.write(ET.tostring(document))
86+
data = ET.tostring(document)
87+
self._out.write(ensure_text(data))
8188
self._out.flush()
8289

8390
def close(self):
8491
"""Write the closing </stream> tag to make this XML well formed."""
85-
self._out.write(b"</stream>")
92+
self._out.write(ensure_text("</stream>"))
93+
self._out.flush()

0 commit comments

Comments
 (0)