Use two verbose levels in testproto.py

According to level:
- no verbose (no "-v" given): compact view for objects
- verbose (one "-v" given): long view for objects
- extra verbose (two "-v" given): long view for objects and display raw messages.
This commit is contained in:
Sebastien Helleu 2013-03-04 13:14:00 +01:00
parent 0f8fb1e450
commit 7b4aefb28c
2 changed files with 23 additions and 18 deletions

View file

@ -34,9 +34,12 @@
import struct, zlib import struct, zlib
class WeechatObject: class WeechatObject:
def __init__(self, objtype, value): def __init__(self, objtype, value, separator='\n'):
self.objtype = objtype; self.objtype = objtype;
self.value = value self.value = value
self.separator = separator
self.indent = ' ' if separator == '\n' else ''
self.separator1 = '\n%s' % self.indent if separator == '\n' else ''
def _str_value(self, v): def _str_value(self, v):
if type(v) is str and not v is None: if type(v) is str and not v is None:
@ -44,19 +47,17 @@ class WeechatObject:
return str(v) return str(v)
def _str_value_hdata(self): def _str_value_hdata(self):
lines = ['', lines = ['%skeys: %s%s%spath: %s' % (self.separator1, str(self.value['keys']), self.separator, self.indent, str(self.value['path']))]
' keys: %s' % str(self.value['keys']),
' path: %s' % str(self.value['path'])]
for i, item in enumerate(self.value['items']): for i, item in enumerate(self.value['items']):
lines.append(' item %d:' % (i + 1)) lines.append(' item %d:%s%s' % ((i + 1), self.separator,
lines.append('\n'.join([' %s: %s' % (key, self._str_value(value)) for key, value in sorted(item.items())])) self.separator.join(['%s%s: %s' % (self.indent * 2, key, self._str_value(value)) for key, value in sorted(item.items())])))
return '\n'.join(lines) return '\n'.join(lines)
def _str_value_infolist(self): def _str_value_infolist(self):
lines = ['', ' name: %s' % self.value['name']] lines = ['%sname: %s' % (self.separator1, self.value['name'])]
for i, item in enumerate(self.value['items']): for i, item in enumerate(self.value['items']):
lines.append(' item %d:' % (i + 1)) lines.append(' item %d:%s%s' % ((i + 1), self.separator,
lines.append('\n'.join([' %s: %s' % (key, self._str_value(value)) for key, value in sorted(item.items())])) self.separator.join(['%s%s: %s' % (self.indent * 2, key, self._str_value(value)) for key, value in sorted(item.items())])))
return '\n'.join(lines) return '\n'.join(lines)
def _str_value_other(self): def _str_value_other(self):
@ -70,8 +71,11 @@ class WeechatObject:
class WeechatObjects(list): class WeechatObjects(list):
def __init__(self, separator='\n'):
self.separator = separator
def __str__(self): def __str__(self):
return '\n'.join([str(obj) for obj in self]) return self.separator.join([str(obj) for obj in self])
class WeechatMessage: class WeechatMessage:
@ -260,7 +264,7 @@ class Protocol:
values.append(self._obj_cb[type_values]()) values.append(self._obj_cb[type_values]())
return values return values
def decode(self, data): def decode(self, data, separator='\n'):
"""Decode binary data and return list of objects.""" """Decode binary data and return list of objects."""
self.data = data self.data = data
size = len(self.data) size = len(self.data)
@ -280,11 +284,11 @@ class Protocol:
if msgid is None: if msgid is None:
msgid = '' msgid = ''
# read objects # read objects
objects = WeechatObjects() objects = WeechatObjects(separator=separator)
while len(self.data) > 0: while len(self.data) > 0:
objtype = self._obj_type() objtype = self._obj_type()
value = self._obj_cb[objtype]() value = self._obj_cb[objtype]()
objects.append(WeechatObject(objtype, value)) objects.append(WeechatObject(objtype, value, separator=separator))
return WeechatMessage(size, size_uncompressed, compression, uncompressed, msgid, objects) return WeechatMessage(size, size_uncompressed, compression, uncompressed, msgid, objects)

View file

@ -37,7 +37,7 @@
import os, sys, socket, select, struct, time import os, sys, socket, select, struct, time
import protocol # WeeChat/relay protocol import protocol # WeeChat/relay protocol
options = { 'h': False, 'v': False, '6': False } options = { 'h': 0, 'v': 0, '6': 0 }
hostname = None hostname = None
port = None port = None
@ -45,7 +45,7 @@ def usage():
"""Display usage.""" """Display usage."""
print('\nSyntax: python %s [-h] [-v] [-6] <hostname> <port>\n' % sys.argv[0]) print('\nSyntax: python %s [-h] [-v] [-6] <hostname> <port>\n' % sys.argv[0])
print(' -h display this help') print(' -h display this help')
print(' -v verbose mode (display raw messages received)') print(' -v verbose mode: long objects view (two -v: display raw messages)')
print(' -6 connect using IPv6') print(' -6 connect using IPv6')
print(' hostname, port hostname (or IP address) and port of machine running WeeChat relay') print(' hostname, port hostname (or IP address) and port of machine running WeeChat relay')
print('') print('')
@ -90,9 +90,9 @@ def decode(message):
global options global options
try: try:
proto = protocol.Protocol() proto = protocol.Protocol()
message = proto.decode(message) message = proto.decode(message, separator='\n' if options['v'] else ', ')
print('') print('')
if options['v'] and message.uncompressed: if options['v'] >= 2 and message.uncompressed:
# display raw message # display raw message
print('\x1b[32m--> message uncompressed (%d bytes):\n%s\x1b[0m' print('\x1b[32m--> message uncompressed (%d bytes):\n%s\x1b[0m'
% (message.size_uncompressed, % (message.size_uncompressed,
@ -166,7 +166,8 @@ if len(sys.argv) < 3:
try: try:
for arg in sys.argv[1:]: for arg in sys.argv[1:]:
if arg[0] == '-': if arg[0] == '-':
options[arg[1:]] = True for opt in arg[1:]:
options[opt] = options.get(opt, 0) + 1
elif hostname: elif hostname:
port = int(arg) port = int(arg)
else: else: