Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migration to python3 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions tvh/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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']
Expand Down
57 changes: 27 additions & 30 deletions tvh/htsmsg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -128,28 +122,29 @@ 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

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:
Expand All @@ -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:
Expand All @@ -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):
Expand All @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions tvh/htsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
with communicating with HTSP server
"""

import log
import htsmsg
from .log import log;
from .htsmsg import serialize, deserialize;

# ###########################################################################
# HTSP Client
Expand All @@ -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


Expand All @@ -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
Expand Down
66 changes: 33 additions & 33 deletions tvh/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)