Skip to content

Commit b871198

Browse files
committed
Fixed formatting issues in [bencode/__init__.py]
1 parent 371ea8b commit b871198

File tree

1 file changed

+48
-47
lines changed

1 file changed

+48
-47
lines changed

bencode/__init__.py

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -45,42 +45,41 @@ def decode_int(x, f):
4545
newf = x.index(b'e', f)
4646
n = int(x[f:newf])
4747

48-
if x[f : f+1] == b'-':
49-
if x[f+1 : f+2] == b'0':
48+
if x[f:f + 1] == b'-':
49+
if x[f + 1:f + 2] == b'0':
5050
raise ValueError
51-
elif x[f : f+1] == b'0' and newf != f + 1:
51+
elif x[f:f + 1] == b'0' and newf != f + 1:
5252
raise ValueError
5353

5454
return n, newf + 1
5555

5656

5757
def decode_string(x, f, try_decode_utf8=True, force_decode_utf8=False):
58-
"""
59-
decode torrent bencoded 'string' in x starting at f
60-
61-
An attempt is made to convert the string to a python string from utf-8.
62-
However, both string and non-string binary data is intermixed in the
63-
torrent bencoding standard. So we have to guess whether the byte
64-
sequence is a string or just binary data. We make this guess by trying
65-
to decode (from utf-8), and if that fails, assuming it is binary data.
66-
There are some instances where the data SHOULD be a string though.
67-
You can check enforce this by setting force_decode_utf8 to True. If the
68-
decoding from utf-8 fails, an UnidcodeDecodeError is raised. Similarly,
69-
if you know it should not be a string, you can skip the decoding
70-
attempt by setting try_decode_utf8=False.
58+
"""Decode torrent bencoded 'string' in x starting at f.
59+
60+
An attempt is made to convert the string to a python string from utf-8.
61+
However, both string and non-string binary data is intermixed in the
62+
torrent bencoding standard. So we have to guess whether the byte
63+
sequence is a string or just binary data. We make this guess by trying
64+
to decode (from utf-8), and if that fails, assuming it is binary data.
65+
There are some instances where the data SHOULD be a string though.
66+
You can check enforce this by setting force_decode_utf8 to True. If the
67+
decoding from utf-8 fails, an UnidcodeDecodeError is raised. Similarly,
68+
if you know it should not be a string, you can skip the decoding
69+
attempt by setting try_decode_utf8=False.
7170
"""
7271
colon = x.index(b':', f)
7372
n = int(x[f:colon])
7473

75-
if x[f : f+1] == b'0' and colon != f + 1:
74+
if x[f:f + 1] == b'0' and colon != f + 1:
7675
raise ValueError
7776

7877
colon += 1
7978
s = x[colon:colon + n]
8079
if try_decode_utf8:
8180
try:
8281
s = s.decode('utf-8')
83-
except UnicodeDecodeError as e:
82+
except UnicodeDecodeError:
8483
if force_decode_utf8:
8584
raise
8685

@@ -90,8 +89,8 @@ def decode_string(x, f, try_decode_utf8=True, force_decode_utf8=False):
9089
def decode_list(x, f):
9190
r, f = [], f + 1
9291

93-
while x[f : f+1] != b'e':
94-
v, f = decode_func[x[f : f+1]](x, f)
92+
while x[f:f + 1] != b'e':
93+
v, f = decode_func[x[f:f + 1]](x, f)
9594
r.append(v)
9695

9796
return r, f + 1
@@ -108,25 +107,24 @@ def decode_dict_py26(x, f):
108107

109108

110109
def decode_dict(x, f, force_sort=True):
111-
"""
112-
decode bencoded data to an OrderedDict
113-
114-
The BitTorrent standard states that:
115-
Keys must be strings and appear in sorted order (sorted as raw
116-
strings, not alphanumerics)
117-
- http://www.bittorrent.org/beps/bep_0003.html
118-
119-
Therefore, this function will force the keys to be strings (decoded
120-
from utf-8), and by default the keys are (re)sorted after reading.
121-
Set force_sort to False to keep the order of the dictionary as
122-
represented in x, as many other encoders and decoders do not force this
123-
property.
110+
"""Decode bencoded data to an OrderedDict.
111+
112+
The BitTorrent standard states that:
113+
Keys must be strings and appear in sorted order (sorted as raw
114+
strings, not alphanumerics)
115+
- http://www.bittorrent.org/beps/bep_0003.html
116+
117+
Therefore, this function will force the keys to be strings (decoded
118+
from utf-8), and by default the keys are (re)sorted after reading.
119+
Set force_sort to False to keep the order of the dictionary as
120+
represented in x, as many other encoders and decoders do not force this
121+
property.
124122
"""
125123
r, f = OrderedDict(), f + 1
126124

127-
while x[f : f+1] != b'e':
125+
while x[f:f + 1] != b'e':
128126
k, f = decode_string(x, f, force_decode_utf8=True)
129-
r[k], f = decode_func[x[f : f+1]](x, f)
127+
r[k], f = decode_func[x[f:f + 1]](x, f)
130128

131129
if force_sort:
132130
r = OrderedDict(sorted(r.items()))
@@ -274,22 +272,26 @@ def bencode(value):
274272
:return: Bencode formatted string
275273
:rtype: str
276274
"""
277-
r = deque() # makes more sense for something with lots of appends
275+
r = deque() # makes more sense for something with lots of appends
276+
277+
# Encode provided value
278278
encode_func[type(value)](value, r)
279+
280+
# Join parts
279281
return b''.join(r)
280282

281283

282284
# Method proxies (for compatibility with other libraries)
283285
decode = bdecode
284286
encode = bencode
285287

288+
286289
def bread(fd):
287-
"""
288-
return bdecoded data from filename, file, or file-like object
290+
"""Return bdecoded data from filename, file, or file-like object.
289291
290-
if fd is a bytes/string or pathlib.Path-like object, it is opened and
291-
read, otherwise .read() is used. if read() not available, exception
292-
raised.
292+
if fd is a bytes/string or pathlib.Path-like object, it is opened and
293+
read, otherwise .read() is used. if read() not available, exception
294+
raised.
293295
"""
294296
if isinstance(fd, (bytes, str)):
295297
with open(fd, 'rb') as fd:
@@ -300,13 +302,13 @@ def bread(fd):
300302
else:
301303
return bdecode(fd.read())
302304

305+
303306
def bwrite(data, fd):
304-
"""
305-
write data in bencoded form to filename, file, or file-like object
307+
"""Write data in bencoded form to filename, file, or file-like object.
306308
307-
if fd is bytes/string or pathlib.Path-like object, it is opened and
308-
written to, otherwise .write() is used. if write() is not available,
309-
exception raised.
309+
if fd is bytes/string or pathlib.Path-like object, it is opened and
310+
written to, otherwise .write() is used. if write() is not available,
311+
exception raised.
310312
"""
311313
if isinstance(fd, (bytes, str)):
312314
with open(fd, 'wb') as fd:
@@ -316,4 +318,3 @@ def bwrite(data, fd):
316318
fd.write(bencode(data))
317319
else:
318320
fd.write(bencode(data))
319-

0 commit comments

Comments
 (0)