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):
"""
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[:]
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)