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:
parent
0f8fb1e450
commit
7b4aefb28c
@ -34,9 +34,12 @@
|
||||
import struct, zlib
|
||||
|
||||
class WeechatObject:
|
||||
def __init__(self, objtype, value):
|
||||
def __init__(self, objtype, value, separator='\n'):
|
||||
self.objtype = objtype;
|
||||
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):
|
||||
if type(v) is str and not v is None:
|
||||
@ -44,19 +47,17 @@ class WeechatObject:
|
||||
return str(v)
|
||||
|
||||
def _str_value_hdata(self):
|
||||
lines = ['',
|
||||
' keys: %s' % str(self.value['keys']),
|
||||
' path: %s' % str(self.value['path'])]
|
||||
lines = ['%skeys: %s%s%spath: %s' % (self.separator1, str(self.value['keys']), self.separator, self.indent, str(self.value['path']))]
|
||||
for i, item in enumerate(self.value['items']):
|
||||
lines.append(' item %d:' % (i + 1))
|
||||
lines.append('\n'.join([' %s: %s' % (key, self._str_value(value)) for key, value in sorted(item.items())]))
|
||||
lines.append(' item %d:%s%s' % ((i + 1), self.separator,
|
||||
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)
|
||||
|
||||
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']):
|
||||
lines.append(' item %d:' % (i + 1))
|
||||
lines.append('\n'.join([' %s: %s' % (key, self._str_value(value)) for key, value in sorted(item.items())]))
|
||||
lines.append(' item %d:%s%s' % ((i + 1), self.separator,
|
||||
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)
|
||||
|
||||
def _str_value_other(self):
|
||||
@ -70,8 +71,11 @@ class WeechatObject:
|
||||
|
||||
|
||||
class WeechatObjects(list):
|
||||
def __init__(self, separator='\n'):
|
||||
self.separator = separator
|
||||
|
||||
def __str__(self):
|
||||
return '\n'.join([str(obj) for obj in self])
|
||||
return self.separator.join([str(obj) for obj in self])
|
||||
|
||||
|
||||
class WeechatMessage:
|
||||
@ -260,7 +264,7 @@ class Protocol:
|
||||
values.append(self._obj_cb[type_values]())
|
||||
return values
|
||||
|
||||
def decode(self, data):
|
||||
def decode(self, data, separator='\n'):
|
||||
"""Decode binary data and return list of objects."""
|
||||
self.data = data
|
||||
size = len(self.data)
|
||||
@ -280,11 +284,11 @@ class Protocol:
|
||||
if msgid is None:
|
||||
msgid = ''
|
||||
# read objects
|
||||
objects = WeechatObjects()
|
||||
objects = WeechatObjects(separator=separator)
|
||||
while len(self.data) > 0:
|
||||
objtype = self._obj_type()
|
||||
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)
|
||||
|
||||
|
||||
|
@ -37,7 +37,7 @@
|
||||
import os, sys, socket, select, struct, time
|
||||
import protocol # WeeChat/relay protocol
|
||||
|
||||
options = { 'h': False, 'v': False, '6': False }
|
||||
options = { 'h': 0, 'v': 0, '6': 0 }
|
||||
hostname = None
|
||||
port = None
|
||||
|
||||
@ -45,7 +45,7 @@ def usage():
|
||||
"""Display usage."""
|
||||
print('\nSyntax: python %s [-h] [-v] [-6] <hostname> <port>\n' % sys.argv[0])
|
||||
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(' hostname, port hostname (or IP address) and port of machine running WeeChat relay')
|
||||
print('')
|
||||
@ -90,9 +90,9 @@ def decode(message):
|
||||
global options
|
||||
try:
|
||||
proto = protocol.Protocol()
|
||||
message = proto.decode(message)
|
||||
message = proto.decode(message, separator='\n' if options['v'] else ', ')
|
||||
print('')
|
||||
if options['v'] and message.uncompressed:
|
||||
if options['v'] >= 2 and message.uncompressed:
|
||||
# display raw message
|
||||
print('\x1b[32m--> message uncompressed (%d bytes):\n%s\x1b[0m'
|
||||
% (message.size_uncompressed,
|
||||
@ -166,7 +166,8 @@ if len(sys.argv) < 3:
|
||||
try:
|
||||
for arg in sys.argv[1:]:
|
||||
if arg[0] == '-':
|
||||
options[arg[1:]] = True
|
||||
for opt in arg[1:]:
|
||||
options[opt] = options.get(opt, 0) + 1
|
||||
elif hostname:
|
||||
port = int(arg)
|
||||
else:
|
||||
|
Loading…
Reference in New Issue
Block a user