cleanup pylint errors and py2/3 issues

This commit is contained in:
j 2016-06-08 15:32:46 +02:00
commit 77f8876fca
20 changed files with 232 additions and 197 deletions

View file

@ -14,8 +14,8 @@ else:
__all__ = ['create_torrent', 'get_info_hash', 'get_torrent_info', 'get_files', 'get_torrent_size']
def create_torrent(file, url, params = {}, flag = Event(),
progress = lambda x: None, progress_percent = 1):
def create_torrent(file, url, params={}, flag=Event(),
progress=lambda x: None, progress_percent=1):
"Creates a torrent for a given file, using url as tracker url"
from .makemetafile import make_meta_file
return make_meta_file(file, url, params, flag, progress, progress_percent)

View file

@ -11,7 +11,7 @@ def _decode_int(data):
"""
data = data[1:]
end = data.index(b'e')
return int(data[:end],10), data[end+1:]
return int(data[:end], 10), data[end+1:]
def _decode_str(data):
"""
@ -19,9 +19,9 @@ def _decode_str(data):
return string, remaining data
"""
start = data.index(b':')
l = int(data[:start].decode(),10)
l = int(data[:start].decode(), 10)
if l <= 0:
raise Exception('invalid string size: %d'%d)
raise Exception('invalid string size: %d' % l)
start += 1
ret = bytes(data[start:start+l])
data = data[start+l:]
@ -67,45 +67,45 @@ def _decode(data):
elif ch.isdigit():
return _decode_str(data)
else:
raise Exception('could not deserialize data: %s'%data)
raise Exception('could not deserialize data: %s' % data)
def bdecode(data):
"""
decode a bytearray
return deserialized object
"""
obj , data = _decode(data)
obj, data = _decode(data)
if len(data) > 0:
raise Exception('failed to deserialize, extra data: %s'%data)
raise Exception('failed to deserialize, extra data: %s' % data)
return obj
def _encode_str(s,buff):
def _encode_str(s, buff):
"""
encode string to a buffer
"""
s = bytearray(s)
l = len(s)
buff.append(bytearray(str(l)+':','utf-8'))
buff.append(bytearray(str(l)+':', 'utf-8'))
buff.append(s)
def _encode_int(i,buff):
def _encode_int(i, buff):
"""
encode integer to a buffer
"""
buff.append(b'i')
buff.append(bytearray(str(i),'ascii'))
buff.append(bytearray(str(i), 'ascii'))
buff.append(b'e')
def _encode_list(l,buff):
def _encode_list(l, buff):
"""
encode list of elements to a buffer
"""
buff.append(b'l')
for i in l:
_encode(i,buff)
_encode(i, buff)
buff.append(b'e')
def _encode_dict(d,buff):
def _encode_dict(d, buff):
"""
encode dict
"""
@ -113,30 +113,30 @@ def _encode_dict(d,buff):
l = list(d.keys())
l.sort()
for k in l:
_encode(str(k),buff)
_encode(d[k],buff)
_encode(str(k), buff)
_encode(d[k], buff)
buff.append(b'e')
def _encode(obj,buff):
def _encode(obj, buff):
"""
encode element obj to a buffer buff
"""
if isinstance(obj,str):
_encode_str(bytearray(obj,'utf-8'),buff)
elif isinstance(obj,bytes):
_encode_str(bytearray(obj),buff)
elif isinstance(obj,bytearray):
_encode_str(obj,buff)
if isinstance(obj, str):
_encode_str(bytearray(obj, 'utf-8'), buff)
elif isinstance(obj, bytes):
_encode_str(bytearray(obj), buff)
elif isinstance(obj, bytearray):
_encode_str(obj, buff)
elif str(obj).isdigit():
_encode_int(obj,buff)
elif isinstance(obj,list):
_encode_list(obj,buff)
elif hasattr(obj,'keys') and hasattr(obj,'values'):
_encode_dict(obj,buff)
elif str(obj) in ['True','False']:
_encode_int(int(obj and '1' or '0'),buff)
_encode_int(obj, buff)
elif isinstance(obj, list):
_encode_list(obj, buff)
elif hasattr(obj, 'keys') and hasattr(obj, 'values'):
_encode_dict(obj, buff)
elif str(obj) in ['True', 'False']:
_encode_int(int(obj and '1' or '0'), buff)
else:
raise Exception('non serializable object: %s'%obj)
raise Exception('non serializable object: %s' % obj)
def bencode(obj):
@ -144,8 +144,8 @@ def bencode(obj):
bencode element, return bytearray
"""
buff = []
_encode(obj,buff)
ret = bytearray()
_encode(obj, buff)
ret = bytearray()
for ba in buff:
ret += ba
ret += ba
return bytes(ret)