diff options
| author | jvoisin | 2013-10-30 14:06:42 +0000 |
|---|---|---|
| committer | jvoisin | 2013-10-30 14:06:42 +0000 |
| commit | de28b830e9490a1283b9a3b68de4bf48c11d944c (patch) | |
| tree | 09e46f70e1b9ece619d7df832e5a8929fe0c5a1d /MAT/bencode/bencode.py | |
| parent | f4fc75514881c7442776f0ce4f7925c5ea9872ff (diff) | |
Clean up bencode.py's code
Diffstat (limited to 'MAT/bencode/bencode.py')
| -rw-r--r-- | MAT/bencode/bencode.py | 43 |
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 | ||
| 29 | import types | ||
| 30 | |||
| 31 | |||
| 32 | class BTFailure(Exception): | 29 | class 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 | ||
| 58 | def decode_string(x, f): | 53 | def 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 | ||
| 68 | def decode_list(x, f): | 63 | def 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 | ||
| 78 | def decode_dict(x, f): | 73 | def 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 | ||
| 88 | def encode_bool(x, r): | 83 | def 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 | ||
| 96 | def encode_int(x, r): | 88 | def encode_int(x, r): |
| @@ -108,7 +100,7 @@ def encode_list(x, r): | |||
| 108 | def encode_dict(x, result): | 100 | def 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 | ||
| 119 | DECODE_FUNC = {} | 111 | DECODE_FUNC = {str(x):decode_string for x in range(9)} |
| 120 | DECODE_FUNC.update(dict([(str(x), decode_string) for x in xrange(9)])) | ||
| 121 | DECODE_FUNC['l'] = decode_list | 112 | DECODE_FUNC['l'] = decode_list |
| 122 | DECODE_FUNC['d'] = decode_dict | 113 | DECODE_FUNC['d'] = decode_dict |
| 123 | DECODE_FUNC['i'] = decode_int | 114 | DECODE_FUNC['i'] = decode_int |
| @@ -125,13 +116,13 @@ DECODE_FUNC['i'] = decode_int | |||
| 125 | 116 | ||
| 126 | ENCODE_FUNC = {} | 117 | ENCODE_FUNC = {} |
| 127 | ENCODE_FUNC[Bencached] = lambda x, r: r.append(x.bencoded) | 118 | ENCODE_FUNC[Bencached] = lambda x, r: r.append(x.bencoded) |
| 128 | ENCODE_FUNC[types.IntType] = encode_int | 119 | ENCODE_FUNC[int] = encode_int |
| 129 | ENCODE_FUNC[types.LongType] = encode_int | 120 | ENCODE_FUNC[int] = encode_int |
| 130 | ENCODE_FUNC[types.StringType] = lambda x, r: r.extend((str(len(x)), ':', x)) | 121 | ENCODE_FUNC[bytes] = lambda x, r: r.extend((str(len(x)), ':', x)) |
| 131 | ENCODE_FUNC[types.ListType] = encode_list | 122 | ENCODE_FUNC[list] = encode_list |
| 132 | ENCODE_FUNC[types.TupleType] = encode_list | 123 | ENCODE_FUNC[tuple] = encode_list |
| 133 | ENCODE_FUNC[types.DictType] = encode_dict | 124 | ENCODE_FUNC[dict] = encode_dict |
| 134 | ENCODE_FUNC[types.BooleanType] = encode_bool | 125 | ENCODE_FUNC[bool] = encode_bool |
| 135 | 126 | ||
| 136 | 127 | ||
| 137 | def bencode(string): | 128 | def bencode(string): |
