|
| 1 | +import collections |
| 2 | +import csv |
| 3 | +import io |
| 4 | +import json |
| 5 | + |
| 6 | +import splunklib.searchcommands.internals |
| 7 | +from splunklib import six |
| 8 | + |
| 9 | + |
| 10 | +class Chunk(object): |
| 11 | + def __init__(self, version, meta, data): |
| 12 | + self.version = six.ensure_str(version) |
| 13 | + self.meta = json.loads(meta) |
| 14 | + dialect = splunklib.searchcommands.internals.CsvDialect |
| 15 | + self.data = csv.DictReader(io.StringIO(data.decode("utf-8")), |
| 16 | + dialect=dialect) |
| 17 | + |
| 18 | + |
| 19 | +class ChunkedDataStreamIter(collections.Iterator): |
| 20 | + def __init__(self, chunk_stream): |
| 21 | + self.chunk_stream = chunk_stream |
| 22 | + |
| 23 | + def __next__(self): |
| 24 | + return self.next() |
| 25 | + |
| 26 | + def next(self): |
| 27 | + try: |
| 28 | + return self.chunk_stream.read_chunk() |
| 29 | + except EOFError: |
| 30 | + raise StopIteration |
| 31 | + |
| 32 | + |
| 33 | +class ChunkedDataStream(collections.Iterable): |
| 34 | + def __iter__(self): |
| 35 | + return ChunkedDataStreamIter(self) |
| 36 | + |
| 37 | + def __init__(self, stream): |
| 38 | + empty = stream.read(0) |
| 39 | + assert isinstance(empty, bytes) |
| 40 | + self.stream = stream |
| 41 | + |
| 42 | + def read_chunk(self): |
| 43 | + header = self.stream.readline() |
| 44 | + |
| 45 | + while len(header) > 0 and header.strip() == b'': |
| 46 | + header = self.stream.readline() # Skip empty lines |
| 47 | + if len(header) == 0: |
| 48 | + raise EOFError |
| 49 | + |
| 50 | + version, meta, data = header.rstrip().split(b',') |
| 51 | + metabytes = self.stream.read(int(meta)) |
| 52 | + databytes = self.stream.read(int(data)) |
| 53 | + return Chunk(version, metabytes, databytes) |
| 54 | + |
| 55 | + |
| 56 | +def build_chunk(keyval, data=None): |
| 57 | + metadata = six.ensure_binary(json.dumps(keyval), 'utf-8') |
| 58 | + data_output = _build_data_csv(data) |
| 59 | + return b"chunked 1.0,%d,%d\n%s%s" % (len(metadata), len(data_output), metadata, data_output) |
| 60 | + |
| 61 | + |
| 62 | +def build_empty_searchinfo(): |
| 63 | + return { |
| 64 | + 'earliest_time': 0, |
| 65 | + 'latest_time': 0, |
| 66 | + 'search': "", |
| 67 | + 'dispatch_dir': "", |
| 68 | + 'sid': "", |
| 69 | + 'args': [], |
| 70 | + 'splunk_version': "42.3.4", |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | +def build_getinfo_chunk(): |
| 75 | + return build_chunk({ |
| 76 | + 'action': 'getinfo', |
| 77 | + 'preview': False, |
| 78 | + 'searchinfo': build_empty_searchinfo()}) |
| 79 | + |
| 80 | + |
| 81 | +def build_data_chunk(data, finished=True): |
| 82 | + return build_chunk({'action': 'execute', 'finished': finished}, data) |
| 83 | + |
| 84 | + |
| 85 | +def _build_data_csv(data): |
| 86 | + if data is None: |
| 87 | + return b'' |
| 88 | + if isinstance(data, bytes): |
| 89 | + return data |
| 90 | + csvout = splunklib.six.StringIO() |
| 91 | + |
| 92 | + headers = set() |
| 93 | + for datum in data: |
| 94 | + headers.update(datum.keys()) |
| 95 | + writer = csv.DictWriter(csvout, headers, |
| 96 | + dialect=splunklib.searchcommands.internals.CsvDialect) |
| 97 | + writer.writeheader() |
| 98 | + for datum in data: |
| 99 | + writer.writerow(datum) |
| 100 | + return six.ensure_binary(csvout.getvalue()) |
0 commit comments