Permalink
Browse files

Merge pull request #224 from jtschichold/csv-ens

CSV output - flexibility improvements
  • Loading branch information...
jtschichold committed Jun 8, 2017
2 parents 3af63a2 + 15785b7 commit 949221d95a7f04818c4034bdae87d80a1b0cb7f1
Showing with 26 additions and 9 deletions.
  1. +26 −9 minemeld/flask/feedredis.py
@@ -207,9 +207,22 @@ def _format_field_value(fv):

translate_ip_ranges = kwargs.pop('translate_ip_ranges', False)

# extract name of fields
fields = ['indicator']
fields.extend(kwargs.pop('f', []))
# extract name of fields and column names
columns = []
fields = []
for addf in kwargs.pop('f', []):
if '|' in addf:
fname, cname = addf.rsplit('|', 1)
else:
fname = addf
cname = addf
columns.append(cname)
fields.append(fname)

# if no fields are specified, only indicator is generated
if len(fields) == 0:
fields = ['indicator']
columns = ['indicator']

# check if header should be generated
header = kwargs.pop('h', None)
@@ -234,7 +247,7 @@ def _format_field_value(fv):
with _buffer() as current_line:
w = unicodecsv.DictWriter(
current_line,
fieldnames=fields,
fieldnames=columns,
encoding='utf-8'
)

@@ -255,11 +268,15 @@ def _format_field_value(fv):
xindicators = _translate_ip_ranges(indicator, v)

for i in xindicators:
fieldvalues = {'indicator': i}
if v is not None and fields:
for f in fields:
if f in v:
fieldvalues[f] = _format_field_value(v[f])
fieldvalues = {}

for f, c in zip(fields, columns):
if f == 'indicator':
fieldvalues[c] = i
continue

if v is not None and f in v:
fieldvalues[c] = _format_field_value(v[f])

current_line.truncate(0)
w.writerow(fieldvalues)

0 comments on commit 949221d

Please sign in to comment.