Skip to content
Snippets Groups Projects
Commit 0568ed21 authored by Hendrik Buschmeier's avatar Hendrik Buschmeier
Browse files

ipaaca-iu-sniffer: show links, more colours, code easier to maintain.

parent 1eb05e36
No related branches found
No related tags found
No related merge requests found
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
# Forschungsgemeinschaft (DFG) in the context of the German # Forschungsgemeinschaft (DFG) in the context of the German
# Excellence Initiative. # Excellence Initiative.
from __future__ import print_function from __future__ import division, print_function
import logging import logging
import re import re
...@@ -44,27 +44,44 @@ import platform ...@@ -44,27 +44,44 @@ import platform
import ipaaca import ipaaca
def event_type_color(typ):
colors = {
'ADDED': '32;1',
'RETRACTED': '31;1',
'UPDATED': '33;1',
'MESSAGE': '34;1',
'COMMITTED': '35;1',
'LINKSUPDATED': '36;1',
'RETRACTED': '37;1',
}
return colors.get(typ, '1')
def highlight_if_color(s, c='1'): def highlight_if_color(s, c='1'):
return s if not arguments.color else '['+c+'m'+s+'' return s if not arguments.color else '[' + c + 'm' + s +''
def pretty_printed_iu_payload(iu): def pretty_printed_dict(d):
s='{ ' s='{\n'
for k,v in iu.payload.items(): for k, v in d.items():
v2 = (('\''+v+'\'') if len(v)<=arguments.max_size else ('\''+v[:arguments.max_size]+'\'<excess length omitted>')).replace('\\','\\\\').replace('\n',highlight_if_color('\\n')) v = str(v)
s += '\n' + '\t\t\'' + highlight_if_color(unicode(k),'1')+'\': '+unicode(v2)+', ' v2 = v if len(v) <= arguments.max_size else v[:arguments.max_size] + '<excess length omitted>'
s+=' }' v2.replace('\\','\\\\').replace('\n', highlight_if_color('\\n'))
s += "\t '%s': '%s'," % (highlight_if_color(unicode(k),'1'), unicode(v2))
s+='\n}'
return s return s
def event_type_color(typ):
colors={'ADDED':'32;1', 'RETRACTED':'31;1', 'UPDATED':'33;1', 'MESSAGE':'34;1'}
return '1' if typ not in colors else colors[typ]
def pretty_printed_iu_event(iu, event_type, local): def pretty_printed_iu_event(iu, event_type, local):
s = '' s = ''
t = time.time() t = time.time()
lt = time.localtime(t) lt = time.localtime(t)
s += highlight_if_color('%.3f'%t, '1')+' '+"%02d:%02d:%02d"%(lt.tm_hour, lt.tm_min, lt.tm_sec) s += highlight_if_color('%.3f' % t, '1')
s += ' '+highlight_if_color('%-9s'%event_type,event_type_color(event_type))+' category='+highlight_if_color(iu.category,event_type_color(event_type))+' uid='+iu.uid+' owner='+iu.owner_name+' payload='+pretty_printed_iu_payload(iu) s += ' %02d:%02d:%02d' % (lt.tm_hour, lt.tm_min, lt.tm_sec)
s += ' ' + highlight_if_color('%-9s' % event_type, event_type_color(event_type))
s += ' category=' + highlight_if_color(iu.category,event_type_color(event_type))
s += ' uid=' + iu.uid
s += ' owner=' + iu.owner_name
if event_type is not 'MESSAGE':
s += '\nlinks=' + pretty_printed_dict(iu.get_all_links())
s += '\npayload=' + pretty_printed_dict(iu.payload)
return s return s
def my_update_handler(iu, event_type, local): def my_update_handler(iu, event_type, local):
...@@ -74,7 +91,7 @@ def my_update_handler(iu, event_type, local): ...@@ -74,7 +91,7 @@ def my_update_handler(iu, event_type, local):
break break
else: else:
return return
print(pretty_printed_iu_event(iu, event_type, local)) print(pretty_printed_iu_event(iu, event_type, local), end='\n\n')
...@@ -117,7 +134,11 @@ if __name__ == '__main__': ...@@ -117,7 +134,11 @@ if __name__ == '__main__':
buffers = {} buffers = {}
for channel in arguments.channels: for channel in arguments.channels:
buffers[channel] = ipaaca.InputBuffer('IpaacaIUSniffer', arguments.categories if not arguments.regex else [''], channel) buffers[channel] = ipaaca.InputBuffer(
'IpaacaIUSniffer',
category_interests=arguments.categories if not arguments.regex else [''],
channel=channel,
resend_active=True)
buffers[channel].register_handler(my_update_handler) buffers[channel].register_handler(my_update_handler)
print('') print('')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment