Skip to content

Commit

Permalink
Cleaner naming and semantics for custom search command input streams
Browse files Browse the repository at this point in the history
  • Loading branch information
amysutedja committed Sep 4, 2020
1 parent 253784c commit 8dec167
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion splunklib/searchcommands/generating_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _execute(self, ifile, process):
"""
if self._protocol_version == 2:
result = self._read_chunk(ifile)
result = self._read_chunk(self._as_binary_stream(ifile))

if not result:
return
Expand Down
26 changes: 16 additions & 10 deletions splunklib/searchcommands/search_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ def _process_protocol_v2(self, argv, ifile, ofile):
# noinspection PyBroadException
try:
debug('Reading metadata')
metadata, body = self._read_chunk(ifile)
metadata, body = self._read_chunk(self._as_binary_stream(ifile))

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

Expand Down Expand Up @@ -850,15 +850,20 @@ def _execute(self, ifile, process):
self.finish()

@staticmethod
def _read_chunk(ifile):
# noinspection PyBroadException
def _as_binary_stream(ifile):
if six.PY2:
istream = ifile
else:
try:
istream = ifile.buffer
except AttributeError as error:
raise RuntimeError('Failed to get underlying buffer: {}'.format(error))
return ifile

try:
return ifile.buffer
except AttributeError as error:
raise RuntimeError('Failed to get underlying buffer: {}'.format(error))

@staticmethod
def _read_chunk(istream):
# noinspection PyBroadException
if not six.PY2:
assert issubclass(type(istream), (io.BufferedIOBase, io.RawIOBase)), 'Stream must be binary'

try:
header = istream.readline()
Expand Down Expand Up @@ -930,9 +935,10 @@ def _records_protocol_v1(self, ifile):
yield record

def _records_protocol_v2(self, ifile):
istream = self._as_binary_stream(ifile)

while True:
result = self._read_chunk(ifile)
result = self._read_chunk(istream)

if not result:
return
Expand Down
6 changes: 3 additions & 3 deletions tests/searchcommands/test_search_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
import pytest

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

ifile = BytesIO(six.ensure_binary(header))
ifile = BytesIO(six.ensure_binary(input))

if not six.PY2:
ifile = TextIOWrapper(ifile)
Expand Down

0 comments on commit 8dec167

Please sign in to comment.