/
grappa_parser.py
40 lines (31 loc) · 1.21 KB
/
grappa_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import itertools
from parser import Parser
from log2sqlite import cli
from parameters import JSONParamsParser, PARAMS_TAG
__author__ = 'brandon'
class GrappaLogParser(Parser):
def __init__(self, includes_params=True):
self.includes_params = includes_params
def recorditer(self, inputstr):
jparams = JSONParamsParser(PARAMS_TAG)
sparams = JSONParamsParser('STATS')
if self.includes_params:
assert jparams.count(inputstr) == sparams.count(inputstr), \
"different numbers of STATS and PARAMS; " \
"check your log file for errors"
# concurrently search for adjacent pairs of PARAMS and STATS
for pdict, sdict in itertools.izip(
jparams.idict_from_json(inputstr),
sparams.idict_from_json(inputstr)):
result = {}
result.update(pdict)
result.update(sdict)
yield result
else:
for sdict in itertools.izip(
jparams.idict_from_json(inputstr)):
result = {}
result.update(sdict)
yield result
if __name__ == '__main__':
cli(GrappaLogParser())