Skip to content

Commit 8dec167

Browse files
committed
Cleaner naming and semantics for custom search command input streams
1 parent 253784c commit 8dec167

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

splunklib/searchcommands/generating_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def _execute(self, ifile, process):
204204
205205
"""
206206
if self._protocol_version == 2:
207-
result = self._read_chunk(ifile)
207+
result = self._read_chunk(self._as_binary_stream(ifile))
208208

209209
if not result:
210210
return

splunklib/searchcommands/search_command.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ def _process_protocol_v2(self, argv, ifile, ofile):
656656
# noinspection PyBroadException
657657
try:
658658
debug('Reading metadata')
659-
metadata, body = self._read_chunk(ifile)
659+
metadata, body = self._read_chunk(self._as_binary_stream(ifile))
660660

661661
action = getattr(metadata, 'action', None)
662662

@@ -850,15 +850,20 @@ def _execute(self, ifile, process):
850850
self.finish()
851851

852852
@staticmethod
853-
def _read_chunk(ifile):
854-
# noinspection PyBroadException
853+
def _as_binary_stream(ifile):
855854
if six.PY2:
856-
istream = ifile
857-
else:
858-
try:
859-
istream = ifile.buffer
860-
except AttributeError as error:
861-
raise RuntimeError('Failed to get underlying buffer: {}'.format(error))
855+
return ifile
856+
857+
try:
858+
return ifile.buffer
859+
except AttributeError as error:
860+
raise RuntimeError('Failed to get underlying buffer: {}'.format(error))
861+
862+
@staticmethod
863+
def _read_chunk(istream):
864+
# noinspection PyBroadException
865+
if not six.PY2:
866+
assert issubclass(type(istream), (io.BufferedIOBase, io.RawIOBase)), 'Stream must be binary'
862867

863868
try:
864869
header = istream.readline()
@@ -930,9 +935,10 @@ def _records_protocol_v1(self, ifile):
930935
yield record
931936

932937
def _records_protocol_v2(self, ifile):
938+
istream = self._as_binary_stream(ifile)
933939

934940
while True:
935-
result = self._read_chunk(ifile)
941+
result = self._read_chunk(istream)
936942

937943
if not result:
938944
return

tests/searchcommands/test_search_command.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
import pytest
3939

4040
def build_command_input(getinfo_metadata, execute_metadata, execute_body):
41-
header = ('chunked 1.0,{},0\n{}'.format(len(six.ensure_binary(getinfo_metadata)), getinfo_metadata) +
42-
'chunked 1.0,{},{}\n{}{}'.format(len(six.ensure_binary(execute_metadata)), len(six.ensure_binary(execute_body)), execute_metadata, execute_body))
41+
input = ('chunked 1.0,{},0\n{}'.format(len(six.ensure_binary(getinfo_metadata)), getinfo_metadata) +
42+
'chunked 1.0,{},{}\n{}{}'.format(len(six.ensure_binary(execute_metadata)), len(six.ensure_binary(execute_body)), execute_metadata, execute_body))
4343

44-
ifile = BytesIO(six.ensure_binary(header))
44+
ifile = BytesIO(six.ensure_binary(input))
4545

4646
if not six.PY2:
4747
ifile = TextIOWrapper(ifile)

0 commit comments

Comments
 (0)