forked from Tribler/tribler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldecoder.py
391 lines (337 loc) · 14.5 KB
/
ldecoder.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/python
from datetime import datetime
from string import printable
class NotInterested(Exception):
pass
def _counter(start):
assert isinstance(start, (int, long))
count = start
while True:
yield count
count += 1
def _ignore_seperator(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
for start in _counter(offset):
if not stream[start] == " ":
return start
raise ValueError()
def _decode_str(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
for split in _counter(offset):
if stream[split] == ":":
length = int(stream[offset:split])
return split + length + 1, stream[split+1:split+length+1]
elif not stream[split] in "1234567890":
raise ValueError("Can not decode string length", stream[split])
def _decode_hex(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
for split in _counter(offset):
if stream[split] == ":":
length = int(stream[offset:split])
return split + length + 1, stream[split+1:split+length+1].decode("HEX")
elif not stream[split] in "1234567890":
raise ValueError("Can not decode string length", stream[split])
def _decode_unicode(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
for split in _counter(offset):
if stream[split] == ":":
length = int(stream[offset:split])
return split + length + 1, stream[split+1:split+length+1].decode("UTF8")
elif not stream[split] in "1234567890":
raise ValueError("Can not decode string length", stream[split])
def _decode_Hex(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
for split in _counter(offset):
if stream[split] == ":":
length = int(stream[offset:split])
return split + length + 1, stream[split+1:split+length+1].decode("HEX").decode("UTF8")
elif not stream[split] in "1234567890":
raise ValueError("Can not decode string length", stream[split])
def _decode_int(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
for split in _counter(offset):
if not stream[split] in "1234567890-":
return split, int(stream[offset:split])
def _decode_long(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
for split in _counter(offset):
if not stream[split] in "1234567890-":
return split, long(stream[offset:split])
def _decode_float(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
for split in _counter(offset):
if not stream[split] in "1234567890-.e":
return split, float(stream[offset:split])
def _decode_boolean(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
if stream[offset:offset+4] == "True":
return offset+4, True
elif stream[offset:offset+5] == "False":
return offset+5, False
else:
raise ValueError()
def _decode_none(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
if stream[offset:offset+4] == "None":
return offset+4, None
else:
raise ValueError("Expected None")
def _decode_tuple(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
for split in _counter(offset):
if stream[split] in ":":
length = int(stream[offset:split])
if not stream[split+1] == "(":
raise ValueError("Expected '('", stream[split+1])
offset = split + 2 # compensate for ':('
l = []
if length:
for index in range(length):
offset, value = _decode(offset, stream)
l.append(value)
if index < length and stream[offset] == "," and stream[offset+1] == " ":
offset += 2 # compensate for ', '
elif index == length - 1 and stream[offset] == ")":
offset += 1 # compensate for ')'
else:
raise ValueError()
else:
if not stream[offset] == ")":
raise ValueError("Expected ')'", stream[split+1])
offset += 1 # compensate for ')'
return offset, tuple(l)
elif not stream[split] in "1234567890":
raise ValueError("Can not decode string length", stream[split])
def _decode_list(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
for split in _counter(offset):
if stream[split] in ":":
length = int(stream[offset:split])
if not stream[split+1] == "[":
raise ValueError("Expected '['", stream[split+1])
offset = split + 2 # compensate for ':['
l = []
if length:
for index in range(length):
offset, value = _decode(offset, stream)
l.append(value)
if index < length and stream[offset] == "," and stream[offset+1] == " ":
offset += 2 # compensate for ', '
elif index == length - 1 and stream[offset] == "]":
offset += 1 # compensate for ']'
else:
raise ValueError()
else:
if not stream[offset] == "]":
raise ValueError("Expected ']'", stream[split+1])
offset += 1 # compensate for ']'
return offset, l
elif not stream[split] in "1234567890":
raise ValueError("Can not decode string length", stream[split])
def _decode_dict(offset, stream):
assert isinstance(offset, (int, long))
assert isinstance(stream, str)
for split in _counter(offset):
if stream[split] in ":":
length = int(stream[offset:split])
if not stream[split+1] == "{":
raise ValueError("Expected '{'", stream[split+1])
offset = split + 2 # compensate for ':{'
d = {}
for index in range(length):
offset, key = _decode(offset, stream)
if key in d:
raise ValueError("Duplicate map entry", key)
if not stream[offset] == ":":
raise ValueError("Expected ':'", stream[offset])
offset += 1 # compensate for ':'
offset, value = _decode(offset, stream)
d[key] = value
if index < length and stream[offset] == "," and stream[offset+1] == " ":
offset += 2 # compensate for ', '
elif index == length - 1 and stream[offset] == "}":
offset += 1 # compensate for '}'
else:
raise ValueError()
return offset, d
elif not stream[split] in "1234567890":
raise ValueError("Can not decode string length", stream[split])
def _decode(offset, stream):
if stream[offset] in _decode_mapping:
return _decode_mapping[stream[offset]](offset + 1, stream)
else:
raise ValueError("Can not decode {0}".format(stream[offset]))
def parse_line(stream, lineno=-1, interests=[]):
assert isinstance(stream, str)
assert isinstance(lineno, (int, long))
assert isinstance(interests, (tuple, list, set))
offset = _ignore_seperator(14, stream)
if not stream[offset] == "s":
raise ValueError("Expected a string encoded message")
offset, message = _decode_str(offset+1, stream)
try:
if not interests or message in interests:
stamp = datetime.strptime(stream[:14], "%Y%m%d%H%M%S")
kargs = {}
while offset < len(stream) - 1:
offset = _ignore_seperator(offset, stream)
for split in _counter(offset):
if stream[split] == ":":
key = stream[offset:split].strip()
offset, value = _decode(split + 1, stream)
kargs[key] = value
break
elif not stream[split] in _valid_key_chars:
raise ValueError("Can not decode character", stream[split], "on line", lineno, "offset", offset)
return lineno, stamp, message, kargs
except Exception, e:
raise ValueError("Cannot read line", str(e), "on line", lineno)
raise NotInterested(message)
def parse(filename, interests=[]):
"""
Parse the content of FILENAME.
Yields a (LINENO, DATETIME, MESSAGE, KARGS) tuple for each line in
the file.
"""
assert isinstance(filename, (str, unicode))
assert isinstance(interests, (tuple, list, set))
assert not filter(lambda x: not isinstance(x, str), interests)
if isinstance(interests, (tuple, list)):
interests = set(interests)
for lineno, stream in zip(_counter(1), open(filename, "r")):
if stream[0] == "#":
continue
try:
yield parse_line(stream, lineno, interests)
except NotInterested:
pass
except Exception, exception:
print "#", exception
def parse_frequencies(filename, select=None):
"""
Parse the content of FILENAME and calculate the frequenties of
logged values.
SELECT is an optional dictionary that tells the parser to only
count specific keys from specific messages. When SELECT is None,
the frequenties of everything is calculated. SELECT has the
following structure:
{msg.A : [key.A.1, key.A.2],
msg.B : [key.B.1, key.B.2]}
Yields a (LINENO, DATETIME, MESSAGE, KARGS, FREQUENTIES) tuple for
each line in the file.
Where FREQUENTIES has the following structure:
{ msg.A : [count.A, {key.A.1 : [count.A.1, {value.A.1.a : count.A.1.a}]}]}
Note that print_frequencies(FREQUENTIES) can be used to display
FREQUENTIES in a human readable way.
"""
isinstance(select, dict)
def parse_with_select():
msg_freq = {}
for message, keys in select.iteritems():
key_freq = {}
for key in keys:
key_freq[key] = [0, {}]
msg_freq[message] = [0, key_freq]
for lineno, stamp, message, kargs in parse(filename):
if message in msg_freq:
msg_container = msg_freq[message]
msg_container[0] += 1
key_freq = msg_container[1]
for key, value in kargs.iteritems():
if key in key_freq:
key_container = key_freq[key]
key_container[0] += 1
value_freq = key_container[1]
if isinstance(value, list):
for value in value:
value_freq[value] = value_freq.get(value, 0) + 1
else:
value_freq[value] = value_freq.get(value, 0) + 1
yield lineno, stamp, message, kargs, msg_freq
def parse_without_select():
msg_freq = {}
for lineno, stamp, message, kargs in parse(filename):
if not message in msg_freq:
msg_freq[message] = [0, {}]
msg_container = msg_freq[message]
msg_container[0] += 1
key_freq = msg_container[1]
for key, value in kargs.iteritems():
if not key in key_freq:
key_freq[key] = [0, {}]
key_container = key_freq[key]
key_container[0] += 1
value_freq = key_container[1]
if isinstance(value, list):
for value in value:
value_freq[value] = value_freq.get(value, 0) + 1
else:
value_freq[value] = value_freq.get(value, 0) + 1
yield lineno, stamp, message, kargs, msg_freq
if select:
return parse_with_select()
else:
return parse_without_select()
def print_frequencies(frequencies, merge=None, limit=8):
def print_helper(freq, total):
for count, value in sorted([(count, value) for value, count in freq.iteritems()], reverse=True)[:limit]:
if isinstance(value, str):
for char in value:
if not char in printable:
print "{0:5} {1:4.0%}: {2}".format(count, 1.0*count/total, value.encode("HEX"))
break
else:
print "{0:5} {1:4.0%}: {2}".format(count, 1.0*count/total, value)
else:
print "{0:5} {1:4.0%}: {2}".format(count, 1.0*count/total, value)
def print_with_merge():
freq = {}
for title, selection in merge.iteritems():
count = 0
for message, key in selection:
if message in frequencies:
key_freq = frequencies[message][1]
if key in key_freq:
key_container = key_freq[key]
count += key_container[0]
for value, count in key_container[1].iteritems():
freq[value] = freq.get(value, 0) + count
print "+++ {0} . {1} +++".format(title, count)
print_helper(freq, count)
print
def print_without_merge():
for message, (msg_count, key_freq) in frequencies.iteritems():
print ">>>>> {0:20} {1:5} <<<<<".format(message, msg_count)
for key, (key_count, value_freq) in key_freq.iteritems():
print "> {0:20} {1:5} {2:4.0%} <".format(key, key_count, 1.0*key_count/msg_count)
print_helper(value_freq, key_count)
print
if merge:
print_with_merge()
else:
print_without_merge()
_valid_key_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_"
_decode_mapping = {"s":_decode_str,
"h":_decode_hex,
"u":_decode_unicode,
"H":_decode_Hex,
"i":_decode_int,
"j":_decode_long,
"f":_decode_float,
"b":_decode_boolean,
"n":_decode_none,
"t":_decode_tuple,
"l":_decode_list,
"m":_decode_dict}