From 58a10a26fcd5fd57726d7106a627f4abcecdd91b Mon Sep 17 00:00:00 2001 From: mwalliczek Date: Fri, 28 Jan 2022 14:05:09 +0100 Subject: [PATCH] migration to python3 --- tvh/api.py | 11 +++------ tvh/htsmsg.py | 57 +++++++++++++++++++++----------------------- tvh/htsp.py | 12 +++++----- tvh/log.py | 66 +++++++++++++++++++++++++-------------------------- 4 files changed, 69 insertions(+), 77 deletions(-) diff --git a/tvh/api.py b/tvh/api.py index 2d80778..f034f43 100644 --- a/tvh/api.py +++ b/tvh/api.py @@ -131,7 +131,7 @@ def enable_channels(self, uuids, enabled=True): 'path': 'idnode/save', 'args': args }) - print "enable_channels returned", self.htsp.recv() + print("enable_channels returned", self.htsp.recv()) def update_channels(self, uuids, data): nodes = [] @@ -150,15 +150,10 @@ def update_channels(self, uuids, data): }) return self.htsp.recv() - def get_epg(self, uuid='', title='', channeltag=''): - args = { - 'channel': uuid, - 'title': title, - 'channelTag': channeltag - } + def get_epg(self, kwargs={}): self.htsp.send('api', { 'path': 'epg/events/grid', - 'args': args + 'args': kwargs }) msg = self.htsp.recv() return msg['response']['entries'] diff --git a/tvh/htsmsg.py b/tvh/htsmsg.py index aae0e28..625b653 100644 --- a/tvh/htsmsg.py +++ b/tvh/htsmsg.py @@ -26,13 +26,12 @@ # ########################################################################### def int2bin(i): - return chr(i >> 24 & 0xFF) + chr(i >> 16 & 0xFF) \ - + chr(i >> 8 & 0xFF) + chr(i & 0xFF) + return bytes([i >> 24 & 0xFF, i >> 16 & 0xFF, i >> 8 & 0xFF, i & 0xFF]) def bin2int(d): - return (ord(d[0]) << 24) + (ord(d[1]) << 16) \ - + (ord(d[2]) << 8) + ord(d[3]) + return (d[0] << 24) + (d[1] << 16) \ + + (d[2] << 8) + d[3] def uuid2int(uuid): @@ -68,12 +67,7 @@ def uuid2int(uuid): HMF_LIST = 5 HMF_DBL = 6 HMF_BOOL = 7 - - -# Light wrapper for binary type -class HMFBin(str): - pass - +HMF_UUID = 8 # Convert python to HTSMSG type def hmf_type(f): @@ -85,7 +79,7 @@ def hmf_type(f): return HMF_STR elif type(f) == int: return HMF_S64 - elif type(f) == HMFBin: + elif type(f) == bytes: return HMF_BIN elif type(f) == bool: return HMF_BOOL @@ -96,7 +90,7 @@ def hmf_type(f): # Size for field def _binary_count(f): ret = 0 - if type(f) in [str, HMFBin]: + if type(f) in [str, bytes]: ret = ret + len(f) elif type(f) in [bool]: ret = ret + 1 @@ -128,15 +122,14 @@ def binary_count(msg): # Write out field in binary form def binary_write(msg): - ret = '' + ret = b'' lst = type(msg) == list for f in msg: - na = '' + na = b'' if not lst: - na = f + na = f.encode('utf-8') f = msg[f] - ret = ret + chr(hmf_type(f)) - ret = ret + chr(len(na) & 0xFF) + ret = ret + bytes([hmf_type(f), len(na) & 0xFF]) l = _binary_count(f) ret = ret + int2bin(l) ret = ret + na @@ -144,12 +137,14 @@ def binary_write(msg): if type(f) in [list, dict]: ret = ret + binary_write(f) elif type(f) in [bool]: - ret = ret + chr(f) - elif type(f) in [str, HMFBin]: + ret = ret + bytes([f]) + elif type(f) in [str]: + ret = ret + f.encode('utf-8') + elif type(f) in [bytes]: ret = ret + f elif type(f) == int: while f: - ret = ret + chr(f & 0xFF) + ret = ret + bytes([f & 0xFF]) f = f >> 8 else: @@ -171,33 +166,35 @@ def deserialize0(data, typ=HMF_MAP): islist = True msg = [] while len(data) > 5: - typ = ord(data[0]) - nlen = ord(data[1]) + typ = data[0] + nlen = data[1] dlen = bin2int(data[2:6]) data = data[6:] - if len < nlen + dlen: raise Exception('not enough data') + if len(data) < nlen + dlen: raise Exception('not enough data') - name = data[:nlen] + name = data[:nlen].decode('utf-8', errors='ignore') data = data[nlen:] if typ == HMF_STR: - item = data[:dlen] + item = data[:dlen].decode('utf-8', errors='ignore') elif typ == HMF_BIN: - item = HMFBin(data[:dlen]) + item = data[:dlen] elif typ == HMF_S64: item = 0 i = dlen - 1 while i >= 0: - item = (item << 8) | ord(data[i]) + item = (item << 8) | data[i] i = i - 1 elif typ in [HMF_LIST, HMF_MAP]: item = deserialize0(data[:dlen], typ) elif typ == HMF_BOOL: bool_val = data[:dlen] if len(bool_val) > 0: - item = bool(ord(bool_val)) + item = bool(bool_val) else: item = None + elif typ == HMF_UUID: + item = data[:dlen] else: raise Exception('invalid data type %d' % typ) if islist: @@ -216,7 +213,7 @@ def __init__(self, fp, rec=False): self._rec = rec def __iter__(self): - print '__iter__()' + print('__iter__()') return self def _read(self, num): @@ -239,7 +236,7 @@ def next(self): self._fp = None raise StopIteration() num = bin2int(tmp) - data = '' + data = b'' while len(data) < num: tmp = self._read(num - len(data)) if not tmp: diff --git a/tvh/htsp.py b/tvh/htsp.py index df0e8df..6c84d78 100644 --- a/tvh/htsp.py +++ b/tvh/htsp.py @@ -22,8 +22,8 @@ with communicating with HTSP server """ -import log -import htsmsg +from .log import log; +from .htsmsg import serialize, deserialize; # ########################################################################### # HTSP Client @@ -35,7 +35,7 @@ # Create passwd digest def htsp_digest(user, passwd, chal): import hashlib - ret = hashlib.sha1(passwd + chal).digest() + ret = hashlib.sha1(passwd.encode() + chal).digest() return ret @@ -56,14 +56,14 @@ def __init__(self, addr, name='HTSP PyClient'): def send(self, func, args={}): args['method'] = func if self._user: args['username'] = self._user - if self._pass: args['digest'] = htsmsg.HMFBin(self._pass) + if self._pass: args['digest'] = self._pass log.debug('htsp tx:') log.debug(args, pretty=True) - self._sock.send(htsmsg.serialize(args)) + self._sock.send(serialize(args)) # Receive def recv(self): - ret = htsmsg.deserialize(self._sock, False) + ret = deserialize(self._sock, False) log.debug('htsp rx:') log.debug(ret, pretty=True) return ret diff --git a/tvh/log.py b/tvh/log.py index 520fca2..d3a739e 100644 --- a/tvh/log.py +++ b/tvh/log.py @@ -23,45 +23,45 @@ # DEBUG_LVL = None +class log: + def debug_init(lvl=None): + global DEBUG_LVL + DEBUG_LVL = lvl -def debug_init(lvl=None): - global DEBUG_LVL - DEBUG_LVL = lvl + # + # Output message + # + def out(pre, msg, **dargs): + import sys + import datetime, pprint + now = datetime.datetime.now() + if 'pretty' in dargs and dargs['pretty']: + ind = 2 + if 'indent' in dargs: ind = dargs['indent'] + msg = pprint.pformat(msg, indent=ind, width=70) + out = '%s %-6s: %s\n' % (now.strftime('%F %T'), pre, msg) + sys.stderr.write(out) -# -# Output message -# -def out(pre, msg, **dargs): - import sys - import datetime, pprint - now = datetime.datetime.now() - if 'pretty' in dargs and dargs['pretty']: - ind = 2 - if 'indent' in dargs: ind = dargs['indent'] - msg = pprint.pformat(msg, indent=ind, width=70) - out = '%s %-6s: %s\n' % (now.strftime('%F %T'), pre, msg) - sys.stderr.write(out) + # + # Debug + # + def debug(msg, lvl=1, **dargs): + if DEBUG_LVL and lvl <= DEBUG_LVL: + log.out('DEBUG', msg, **dargs) -# -# Debug -# -def debug(msg, lvl=1, **dargs): - if DEBUG_LVL and lvl <= DEBUG_LVL: - out('DEBUG', msg, **dargs) - -# -# Info -# -def info(msg, **dargs): - out('INFO', msg, **dargs) + # + # Info + # + def info(msg, **dargs): + log.out('INFO', msg, **dargs) -# -# Error -# -def error(msg, **dargs): - out('ERROR', msg, **dargs) + # + # Error + # + def error(msg, **dargs): + log.out('ERROR', msg, **dargs)