Skip to content

Instantly share code, notes, and snippets.

@munro
Forked from djmunro/parser.py
Last active December 9, 2015 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save munro/56fe0edc39140a4142b1 to your computer and use it in GitHub Desktop.
Save munro/56fe0edc39140a4142b1 to your computer and use it in GitHub Desktop.
import re
import sys
import json
import itertools
from collections import Counter
from collections import defaultdict
SPLIT_MESSAGES = re.compile(r'\n.?\w{3} \w+ +\d+ \d+:\d+:\d+ \d+[\r\s]*\n')
MATCH_SIGNAL = re.compile(r'signal sender=(?P<sender>.*) -> dest=(?P<dest>.*) serial=(?P<serial>.*) path=(?P<path>.*) interface=(?P<interface>.*) member=(?P<member>.*)\n\s+string (?P<function>.*)\n\s+string (?P<params>.*)')
MATCH_METHOD_CALL = re.compile(r'method call sender=(?P<sender>.*) -> dest=(?P<dest>.*) serial=(?P<serial>.*) path=(?P<path>.*) interface=(?P<interface>.*) member=(?P<member>.*)\n\s+string (?P<function>.*)\n\s+string (?P<params>.*)')
MATCH_METHOD_RETURN = re.compile(r'method return sender=(?P<sender>.*) -> dest=(?P<dest>.*) reply_serial=(?P<reply_serial>.*)')
def parse_message(text):
signal = MATCH_SIGNAL.search(text)
method_call = MATCH_METHOD_CALL.search(text)
method_return = MATCH_METHOD_RETURN.search(text)
if signal:
return signal.groupdict()
if method_call:
return method_call.groupdict()
if method_return:
return method_return.groupdict()
raise Exception('Cannot parse message:\n{}'.format(text))
def parse_messages(text):
messages = SPLIT_MESSAGES.split('\n' + text)
return [parse_message(message) for message in messages if message.strip()]
if __name__ == '__main__':
data = open(sys.argv[1], 'r').read()
messages = parse_messages(data)
function_messages = [x for x in messages if 'function' in x]
def groupby_key(message):
return message['function']
"""
Signal printout
"""
print '{0: <30}{1: <15}{2:}'.format('Signals', 'Occurrence', 'Sender')
# this prints out all the signal names, and their respective occurences
grouped = itertools.groupby(sorted(function_messages, key=groupby_key), groupby_key)
for group, items in grouped:
items = list(items)
print '{func:<30}{occurences:<15}{path}'.format(
func=items[0]['function'],
occurences=len(items),
path=items[0]['path']
)
"""
Alternative was return both function & path to the counter
print '-----'
c = Counter((x['function'], x['path']) for x in messages if 'function' in x and 'path' in x)
for (function, path), count in c.items():
print '{func}\t{count}\t{path}'.format(
func=function,
count=count,
path=path
)
"""
"""
Methods printout
"""
print '\n{0: <35}{1: <15}{2: <10}{3}'.format('Methods: (Member=Invoke)', 'Occurrence', 'Sender', 'Destination')
grouped = itertools.groupby(sorted(function_messages, key=groupby_key), groupby_key)
for group, items in grouped:
items = list(items)
print '{func:<35}{occurences:<15}{sender:<10}{destination}'.format(
func=items[0]['function'],
occurences=len(items),
sender=items[0]['sender'],
destination=items[0]['dest']
)
"""
SERVICES printout
"""
print '\n{0: <45}{1: <15}'.format('Services', 'Occurrence')
services = Counter((x['path']) for x in messages if 'path' in x) # give me a Counter with all the serives from path=
for service, occurrence in services.iteritems():
print '{key:<45}{value}'.format(key=service, value=occurrence)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment