Esempio n. 1
0
def build_command_input(getinfo_metadata, 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(input))

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

    return ifile
Esempio n. 2
0
 def stream(self, records):
     self.logger.debug('CountMatchesCommand: %s', self)  # logs command line
     pattern = self.pattern
     for record in records:
         count = 0
         for fieldname in self.fieldnames:
             matches = pattern.findall(
                 six.text_type(
                     six.ensure_binary(record[fieldname]).decode("utf-8")))
             count += len(matches)
         record[self.fieldname] = count
         yield record
Esempio n. 3
0
def _build_data_csv(data):
    if data is None:
        return b''
    if isinstance(data, bytes):
        return data
    csvout = splunklib.six.StringIO()

    headers = set()
    for datum in data:
        headers.update(datum.keys())
    writer = csv.DictWriter(
        csvout, headers, dialect=splunklib.searchcommands.internals.CsvDialect)
    writer.writeheader()
    for datum in data:
        writer.writerow(datum)
    return six.ensure_binary(csvout.getvalue())
Esempio n. 4
0
def build_chunk(keyval, data=None):
    metadata = six.ensure_binary(json.dumps(keyval), 'utf-8')
    data_output = _build_data_csv(data)
    return b"chunked 1.0,%d,%d\n%s%s" % (len(metadata), len(data_output),
                                         metadata, data_output)