diff options
| author | jvoisin | 2015-07-25 17:14:23 +0200 |
|---|---|---|
| committer | jvoisin | 2015-07-25 17:14:23 +0200 |
| commit | 6ba3e3f20d7d52895bc44f9fc35b068cfce47133 (patch) | |
| tree | 15df2aca17d56d941c6376ef729e0c1fea4c396f /libmat/bencode/bencode.py | |
| parent | 85e6279d16af063e5150c7cf4bd491185b8ae788 (diff) | |
_MASSIVE_ pep8 revamp
Thank you so much PyCharm
Diffstat (limited to '')
| -rw-r--r-- | libmat/bencode/bencode.py | 33 |
1 files changed, 16 insertions, 17 deletions
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 | ||
| 29 | class BTFailure(Exception): | 29 | class BTFailure(Exception): |
| 30 | '''Custom Exception''' | 30 | """Custom Exception""" |
| 31 | pass | 31 | pass |
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | class Bencached(object): | 34 | class 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 | ||
| 42 | def decode_int(x, f): | 42 | def 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 | ||
| 53 | def decode_string(x, f): | 53 | def 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 | ||
| 63 | def decode_list(x, f): | 63 | def 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 | ||
| 73 | def decode_dict(x, f): | 73 | def 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 | ||
| 83 | def encode_bool(x, r): | 83 | def 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 | ||
| 88 | def encode_int(x, r): | 88 | def 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 | ||
| 93 | def encode_list(x, r): | 93 | def 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 | ||
| 100 | def encode_dict(x, result): | 100 | def 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 | ||
| 111 | DECODE_FUNC = {str(x):decode_string for x in range(9)} | 111 | DECODE_FUNC = {str(x): decode_string for x in range(9)} |
| 112 | DECODE_FUNC['l'] = decode_list | 112 | DECODE_FUNC['l'] = decode_list |
| 113 | DECODE_FUNC['d'] = decode_dict | 113 | DECODE_FUNC['d'] = decode_dict |
| 114 | DECODE_FUNC['i'] = decode_int | 114 | DECODE_FUNC['i'] = decode_int |
| 115 | 115 | ||
| 116 | |||
| 117 | ENCODE_FUNC = {} | 116 | ENCODE_FUNC = {} |
| 118 | ENCODE_FUNC[Bencached] = lambda x, r: r.append(x.bencoded) | 117 | ENCODE_FUNC[Bencached] = lambda x, r: r.append(x.bencoded) |
| 119 | ENCODE_FUNC[int] = encode_int | 118 | ENCODE_FUNC[int] = encode_int |
| @@ -126,14 +125,14 @@ ENCODE_FUNC[bool] = encode_bool | |||
| 126 | 125 | ||
| 127 | 126 | ||
| 128 | def bencode(string): | 127 | def 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 | ||
| 135 | def bdecode(string): | 134 | def 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): |
