#
# protocol.py - decode binary messages received from WeeChat/relay
#
-# Copyright (C) 2011-2015 Sébastien Helleu <flashcode@flashtux.org>
+# Copyright (C) 2011-2021 Sébastien Helleu <flashcode@flashtux.org>
#
# This file is part of QWeeChat, a Qt remote GUI for WeeChat.
#
import struct
import zlib
-if hasattr(collections, 'OrderedDict'):
- # python >= 2.7
- class WeechatDict(collections.OrderedDict):
- def __str__(self):
- return '{%s}' % ', '.join(
- ['%s: %s' % (repr(key), repr(self[key])) for key in self])
-else:
- # python <= 2.6
- WeechatDict = dict
+
+class WeechatDict(collections.OrderedDict):
+ def __str__(self):
+ return '{%s}' % ', '.join(
+ ['%s: %s' % (repr(key), repr(self[key])) for key in self])
class WeechatObject:
if len(self.data) < 3:
self.data = ''
return ''
- objtype = str(self.data[0:3])
+ objtype = self.data[0:3].decode()
self.data = self.data[3:]
return objtype
value = self._obj_len_data(1)
if value is None:
return None
- return int(str(value))
+ return int(value)
def _obj_str(self):
"""Read a string in data (length on 4 bytes + content)."""
value = self._obj_len_data(4)
- if value is None:
- return None
- return str(value)
+ if value in ("", None):
+ return ""
+ return value.decode()
def _obj_buffer(self):
"""Read a buffer in data (length on 4 bytes + data)."""
value = self._obj_len_data(1)
if value is None:
return None
- return '0x%s' % str(value)
+ return '0x%s' % value
def _obj_time(self):
"""Read a time in data (length on 1 byte + value as string)."""
value = self._obj_len_data(1)
if value is None:
return None
- return int(str(value))
+ return int(value)
def _obj_hashtable(self):
"""
type_values = self._obj_type()
count = self._obj_int()
hashtable = WeechatDict()
- for i in range(0, count):
+ for _ in range(count):
key = self._obj_cb[type_keys]()
value = self._obj_cb[type_values]()
hashtable[key] = value
path = self._obj_str()
keys = self._obj_str()
count = self._obj_int()
- list_path = path.split('/')
- list_keys = keys.split(',')
+ list_path = path.split('/') if path else []
+ list_keys = keys.split(',') if keys else []
keys_types = []
dict_keys = WeechatDict()
for key in list_keys:
keys_types.append(items)
dict_keys[items[0]] = items[1]
items = []
- for i in range(0, count):
+ for _ in range(count):
item = WeechatDict()
item['__path'] = []
pointers = []
- for p in range(0, len(list_path)):
+ for _ in enumerate(list_path):
pointers.append(self._obj_ptr())
for key, objtype in keys_types:
item[key] = self._obj_cb[objtype]()
name = self._obj_str()
count_items = self._obj_int()
items = []
- for i in range(0, count_items):
+ for _ in range(count_items):
count_vars = self._obj_int()
variables = WeechatDict()
- for v in range(0, count_vars):
+ for _ in range(count_vars):
var_name = self._obj_str()
var_type = self._obj_type()
var_value = self._obj_cb[var_type]()
type_values = self._obj_type()
count_values = self._obj_int()
values = []
- for i in range(0, count_values):
+ for _ in range(count_values):
values.append(self._obj_cb[type_values]())
return values
if compression:
uncompressed = zlib.decompress(self.data[5:])
size_uncompressed = len(uncompressed) + 5
- uncompressed = '%s%s%s' % (struct.pack('>i', size_uncompressed),
- struct.pack('b', 0), uncompressed)
+ uncompressed = b'%s%s%s' % (struct.pack('>i', size_uncompressed),
+ struct.pack('b', 0), uncompressed)
self.data = uncompressed
else:
uncompressed = self.data[:]
if num_lines == 0:
return ''
lines = []
- for i in range(0, num_lines):
+ for i in range(num_lines):
str_hex = []
str_ascii = []
- for char in data[i*bytes_per_line:(i*bytes_per_line)+bytes_per_line]:
+ for j in range(bytes_per_line):
+ # We can't easily iterate over individual bytes, so we are going to
+ # do it this way.
+ index = (i*bytes_per_line) + j
+ char = data[index:index+1]
+ if not char:
+ char = b'x'
byte = struct.unpack('B', char)[0]
- str_hex.append('%02X' % int(byte))
+ str_hex.append(b'%02X' % int(byte))
if byte >= 32 and byte <= 127:
str_ascii.append(char)
else:
- str_ascii.append('.')
- fmt = '%%-%ds %%s' % ((bytes_per_line * 3) - 1)
- lines.append(fmt % (' '.join(str_hex), ''.join(str_ascii)))
- return '\n'.join(lines)
+ str_ascii.append(b'.')
+ fmt = b'%%-%ds %%s' % ((bytes_per_line * 3) - 1)
+ lines.append(fmt % (b' '.join(str_hex),
+ b''.join(str_ascii)))
+ return b'\n'.join(lines)