summaryrefslogtreecommitdiff
path: root/libmat/bencode
diff options
context:
space:
mode:
authorjvoisin2015-07-25 17:14:23 +0200
committerjvoisin2015-07-25 17:14:23 +0200
commit6ba3e3f20d7d52895bc44f9fc35b068cfce47133 (patch)
tree15df2aca17d56d941c6376ef729e0c1fea4c396f /libmat/bencode
parent85e6279d16af063e5150c7cf4bd491185b8ae788 (diff)
_MASSIVE_ pep8 revamp
Thank you so much PyCharm
Diffstat (limited to 'libmat/bencode')
-rw-r--r--libmat/bencode/__init__.py1
-rw-r--r--libmat/bencode/bencode.py33
2 files changed, 16 insertions, 18 deletions
diff --git a/libmat/bencode/__init__.py b/libmat/bencode/__init__.py
index 8b13789..e69de29 100644
--- a/libmat/bencode/__init__.py
+++ b/libmat/bencode/__init__.py
@@ -1 +0,0 @@
1
diff --git a/libmat/bencode/bencode.py b/libmat/bencode/bencode.py
index a0cc99a..a7967fc 100644
--- a/libmat/bencode/bencode.py
+++ b/libmat/bencode/bencode.py
@@ -21,18 +21,18 @@
21# THE SOFTWARE. 21# THE SOFTWARE.
22# 22#
23 23
24''' 24"""
25 A quick (and also nice) lib to bencode/bdecode torrent files 25 A quick (and also nice) lib to bencode/bdecode torrent files
26''' 26"""
27 27
28 28
29class BTFailure(Exception): 29class BTFailure(Exception):
30 '''Custom Exception''' 30 """Custom Exception"""
31 pass 31 pass
32 32
33 33
34class Bencached(object): 34class Bencached(object):
35 '''Custom type : cached string''' 35 """Custom type : cached string"""
36 __slots__ = ['bencoded'] 36 __slots__ = ['bencoded']
37 37
38 def __init__(self, string): 38 def __init__(self, string):
@@ -40,10 +40,10 @@ class Bencached(object):
40 40
41 41
42def decode_int(x, f): 42def decode_int(x, f):
43 '''decode an int''' 43 """decode an int"""
44 f += 1 44 f += 1
45 newf = x.index('e', f) 45 newf = x.index('e', f)
46 if x[f:f+1] == '-0': 46 if x[f:f + 1] == '-0':
47 raise ValueError 47 raise ValueError
48 elif x[f] == '0' and newf != f + 1: 48 elif x[f] == '0' and newf != f + 1:
49 raise ValueError 49 raise ValueError
@@ -51,7 +51,7 @@ def decode_int(x, f):
51 51
52 52
53def decode_string(x, f): 53def decode_string(x, f):
54 '''decode a string''' 54 """decode a string"""
55 colon = x.index(':', f) 55 colon = x.index(':', f)
56 if x[f] == '0' and colon != f + 1: 56 if x[f] == '0' and colon != f + 1:
57 raise ValueError 57 raise ValueError
@@ -61,7 +61,7 @@ def decode_string(x, f):
61 61
62 62
63def decode_list(x, f): 63def decode_list(x, f):
64 '''decode a list''' 64 """decode a list"""
65 result = [] 65 result = []
66 f += 1 66 f += 1
67 while x[f] != 'e': 67 while x[f] != 'e':
@@ -71,7 +71,7 @@ def decode_list(x, f):
71 71
72 72
73def decode_dict(x, f): 73def decode_dict(x, f):
74 '''decode a dict''' 74 """decode a dict"""
75 result = {} 75 result = {}
76 f += 1 76 f += 1
77 while x[f] != 'e': 77 while x[f] != 'e':
@@ -81,24 +81,24 @@ def decode_dict(x, f):
81 81
82 82
83def encode_bool(x, r): 83def encode_bool(x, r):
84 '''bencode a boolean''' 84 """bencode a boolean"""
85 encode_int(1 if r else 0, r) 85 encode_int(1 if r else 0, r)
86 86
87 87
88def encode_int(x, r): 88def encode_int(x, r):
89 '''bencode an integer/float''' 89 """bencode an integer/float"""
90 r.extend(('i', str(x), 'e')) 90 r.extend(('i', str(x), 'e'))
91 91
92 92
93def encode_list(x, r): 93def encode_list(x, r):
94 '''bencode a list/tuple''' 94 """bencode a list/tuple"""
95 r.append('l') 95 r.append('l')
96 [ENCODE_FUNC[type(item)](item, r) for item in x] 96 [ENCODE_FUNC[type(item)](item, r) for item in x]
97 r.append('e') 97 r.append('e')
98 98
99 99
100def encode_dict(x, result): 100def encode_dict(x, result):
101 '''bencode a dict''' 101 """bencode a dict"""
102 result.append('d') 102 result.append('d')
103 ilist = list(x.items()) 103 ilist = list(x.items())
104 ilist.sort() 104 ilist.sort()
@@ -108,12 +108,11 @@ def encode_dict(x, result):
108 result.append('e') 108 result.append('e')
109 109
110 110
111DECODE_FUNC = {str(x):decode_string for x in range(9)} 111DECODE_FUNC = {str(x): decode_string for x in range(9)}
112DECODE_FUNC['l'] = decode_list 112DECODE_FUNC['l'] = decode_list
113DECODE_FUNC['d'] = decode_dict 113DECODE_FUNC['d'] = decode_dict
114DECODE_FUNC['i'] = decode_int 114DECODE_FUNC['i'] = decode_int
115 115
116
117ENCODE_FUNC = {} 116ENCODE_FUNC = {}
118ENCODE_FUNC[Bencached] = lambda x, r: r.append(x.bencoded) 117ENCODE_FUNC[Bencached] = lambda x, r: r.append(x.bencoded)
119ENCODE_FUNC[int] = encode_int 118ENCODE_FUNC[int] = encode_int
@@ -126,14 +125,14 @@ ENCODE_FUNC[bool] = encode_bool
126 125
127 126
128def bencode(string): 127def bencode(string):
129 '''bencode $string''' 128 """bencode $string"""
130 table = [] 129 table = []
131 ENCODE_FUNC[type(string)](string, table) 130 ENCODE_FUNC[type(string)](string, table)
132 return ''.join(table) 131 return ''.join(table)
133 132
134 133
135def bdecode(string): 134def bdecode(string):
136 '''decode $string''' 135 """decode $string"""
137 try: 136 try:
138 result, lenght = DECODE_FUNC[string[0]](string, 0) 137 result, lenght = DECODE_FUNC[string[0]](string, 0)
139 except (IndexError, KeyError, ValueError): 138 except (IndexError, KeyError, ValueError):