summaryrefslogtreecommitdiff
path: root/MAT/bencode
diff options
context:
space:
mode:
authorjvoisin2013-10-30 14:06:42 +0000
committerjvoisin2013-10-30 14:06:42 +0000
commitde28b830e9490a1283b9a3b68de4bf48c11d944c (patch)
tree09e46f70e1b9ece619d7df832e5a8929fe0c5a1d /MAT/bencode
parentf4fc75514881c7442776f0ce4f7925c5ea9872ff (diff)
Clean up bencode.py's code
Diffstat (limited to 'MAT/bencode')
-rw-r--r--MAT/bencode/bencode.py43
1 files changed, 17 insertions, 26 deletions
diff --git a/MAT/bencode/bencode.py b/MAT/bencode/bencode.py
index 739ffe5..a0cc99a 100644
--- a/MAT/bencode/bencode.py
+++ b/MAT/bencode/bencode.py
@@ -26,9 +26,6 @@
26''' 26'''
27 27
28 28
29import types
30
31
32class BTFailure(Exception): 29class BTFailure(Exception):
33 '''Custom Exception''' 30 '''Custom Exception'''
34 pass 31 pass
@@ -46,23 +43,21 @@ def decode_int(x, f):
46 '''decode an int''' 43 '''decode an int'''
47 f += 1 44 f += 1
48 newf = x.index('e', f) 45 newf = x.index('e', f)
49 n = int(x[f:newf]) 46 if x[f:f+1] == '-0':
50 if x[f] == '-': 47 raise ValueError
51 if x[f + 1] == '0':
52 raise ValueError
53 elif x[f] == '0' and newf != f + 1: 48 elif x[f] == '0' and newf != f + 1:
54 raise ValueError 49 raise ValueError
55 return (n, newf + 1) 50 return int(x[f:newf]), newf + 1
56 51
57 52
58def decode_string(x, f): 53def decode_string(x, f):
59 '''decode a string''' 54 '''decode a string'''
60 colon = x.index(':', f) 55 colon = x.index(':', f)
61 n = int(x[f:colon])
62 if x[f] == '0' and colon != f + 1: 56 if x[f] == '0' and colon != f + 1:
63 raise ValueError 57 raise ValueError
58 n = int(x[f:colon])
64 colon += 1 59 colon += 1
65 return (x[colon:colon + n], colon + n) 60 return x[colon:colon + n], colon + n
66 61
67 62
68def decode_list(x, f): 63def decode_list(x, f):
@@ -72,7 +67,7 @@ def decode_list(x, f):
72 while x[f] != 'e': 67 while x[f] != 'e':
73 v, f = DECODE_FUNC[x[f]](x, f) 68 v, f = DECODE_FUNC[x[f]](x, f)
74 result.append(v) 69 result.append(v)
75 return (result, f + 1) 70 return result, f + 1
76 71
77 72
78def decode_dict(x, f): 73def decode_dict(x, f):
@@ -82,15 +77,12 @@ def decode_dict(x, f):
82 while x[f] != 'e': 77 while x[f] != 'e':
83 k, f = decode_string(x, f) 78 k, f = decode_string(x, f)
84 result[k], f = DECODE_FUNC[x[f]](x, f) 79 result[k], f = DECODE_FUNC[x[f]](x, f)
85 return (result, f + 1) 80 return result, f + 1
86 81
87 82
88def encode_bool(x, r): 83def encode_bool(x, r):
89 '''bencode a boolean''' 84 '''bencode a boolean'''
90 if x: 85 encode_int(1 if r else 0, r)
91 encode_int(1, r)
92 else:
93 encode_int(0, r)
94 86
95 87
96def encode_int(x, r): 88def encode_int(x, r):
@@ -108,7 +100,7 @@ def encode_list(x, r):
108def encode_dict(x, result): 100def encode_dict(x, result):
109 '''bencode a dict''' 101 '''bencode a dict'''
110 result.append('d') 102 result.append('d')
111 ilist = x.items() 103 ilist = list(x.items())
112 ilist.sort() 104 ilist.sort()
113 for k, v in ilist: 105 for k, v in ilist:
114 result.extend((str(len(k)), ':', k)) 106 result.extend((str(len(k)), ':', k))
@@ -116,8 +108,7 @@ def encode_dict(x, result):
116 result.append('e') 108 result.append('e')
117 109
118 110
119DECODE_FUNC = {} 111DECODE_FUNC = {str(x):decode_string for x in range(9)}
120DECODE_FUNC.update(dict([(str(x), decode_string) for x in xrange(9)]))
121DECODE_FUNC['l'] = decode_list 112DECODE_FUNC['l'] = decode_list
122DECODE_FUNC['d'] = decode_dict 113DECODE_FUNC['d'] = decode_dict
123DECODE_FUNC['i'] = decode_int 114DECODE_FUNC['i'] = decode_int
@@ -125,13 +116,13 @@ DECODE_FUNC['i'] = decode_int
125 116
126ENCODE_FUNC = {} 117ENCODE_FUNC = {}
127ENCODE_FUNC[Bencached] = lambda x, r: r.append(x.bencoded) 118ENCODE_FUNC[Bencached] = lambda x, r: r.append(x.bencoded)
128ENCODE_FUNC[types.IntType] = encode_int 119ENCODE_FUNC[int] = encode_int
129ENCODE_FUNC[types.LongType] = encode_int 120ENCODE_FUNC[int] = encode_int
130ENCODE_FUNC[types.StringType] = lambda x, r: r.extend((str(len(x)), ':', x)) 121ENCODE_FUNC[bytes] = lambda x, r: r.extend((str(len(x)), ':', x))
131ENCODE_FUNC[types.ListType] = encode_list 122ENCODE_FUNC[list] = encode_list
132ENCODE_FUNC[types.TupleType] = encode_list 123ENCODE_FUNC[tuple] = encode_list
133ENCODE_FUNC[types.DictType] = encode_dict 124ENCODE_FUNC[dict] = encode_dict
134ENCODE_FUNC[types.BooleanType] = encode_bool 125ENCODE_FUNC[bool] = encode_bool
135 126
136 127
137def bencode(string): 128def bencode(string):