Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b8a9176d6 |
+18
-38
@@ -22,33 +22,20 @@ import io
|
|||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
try:
|
|
||||||
FileNotFoundError
|
|
||||||
except NameError:
|
|
||||||
# Python 2 do not have FileNotFoundError, use IOError instead
|
|
||||||
# noinspection PyShadowingBuiltins
|
|
||||||
FileNotFoundError = IOError
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'InvalidTorrentDataException',
|
'InvalidTorrentFileException',
|
||||||
'parse_torrent_file',
|
'parse_torrent_file',
|
||||||
'TorrentFileParser',
|
'TorrentFileParser',
|
||||||
]
|
]
|
||||||
|
|
||||||
__version__ = '0.1.3'
|
__version__ = '0.1.2'
|
||||||
|
|
||||||
|
|
||||||
class InvalidTorrentDataException(Exception):
|
class InvalidTorrentFileException(Exception):
|
||||||
def __init__(self, pos, msg=None):
|
def __init__(self, pos, msg=None):
|
||||||
msg = msg or "Invalid torrent format when read at pos {pos}"
|
msg = msg or "Invalid torrent format when reading at pos " + str(pos)
|
||||||
msg = msg.format(pos=pos)
|
super(InvalidTorrentFileException, self).__init__(msg)
|
||||||
super(InvalidTorrentDataException, self).__init__(msg)
|
|
||||||
|
|
||||||
|
|
||||||
class __EndCls(object):
|
|
||||||
pass
|
|
||||||
|
|
||||||
_END = __EndCls()
|
|
||||||
|
|
||||||
|
|
||||||
class TorrentFileParser(object):
|
class TorrentFileParser(object):
|
||||||
@@ -101,7 +88,7 @@ class TorrentFileParser(object):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
c = self._read_byte(1, True)
|
c = self._read_byte(1, True)
|
||||||
raise InvalidTorrentDataException(
|
raise InvalidTorrentFileException(
|
||||||
0, 'Expect EOF, but get [{}] at pos {}'.format(c, self._pos)
|
0, 'Expect EOF, but get [{}] at pos {}'.format(c, self._pos)
|
||||||
)
|
)
|
||||||
except EOFError: # expect EOF
|
except EOFError: # expect EOF
|
||||||
@@ -110,7 +97,7 @@ class TorrentFileParser(object):
|
|||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
raise InvalidTorrentDataException('Outermost element is not a dict')
|
raise InvalidTorrentFileException('Outermost element is not a dict')
|
||||||
|
|
||||||
def _read_byte(self, count=1, raise_eof=False):
|
def _read_byte(self, count=1, raise_eof=False):
|
||||||
assert count >= 0
|
assert count >= 0
|
||||||
@@ -118,7 +105,7 @@ class TorrentFileParser(object):
|
|||||||
if count != 0 and len(gotten) == 0:
|
if count != 0 and len(gotten) == 0:
|
||||||
if raise_eof:
|
if raise_eof:
|
||||||
raise EOFError()
|
raise EOFError()
|
||||||
raise InvalidTorrentDataException(
|
raise InvalidTorrentFileException(
|
||||||
self._pos,
|
self._pos,
|
||||||
'Unexpected EOF when reading torrent file'
|
'Unexpected EOF when reading torrent file'
|
||||||
)
|
)
|
||||||
@@ -134,8 +121,9 @@ class TorrentFileParser(object):
|
|||||||
|
|
||||||
def _dict_items_generator(self):
|
def _dict_items_generator(self):
|
||||||
while True:
|
while True:
|
||||||
|
try:
|
||||||
k = self._next_element()
|
k = self._next_element()
|
||||||
if k is _END:
|
except InvalidTorrentFileException:
|
||||||
return
|
return
|
||||||
if k == 'pieces':
|
if k == 'pieces':
|
||||||
v = self._next_hash()
|
v = self._next_hash()
|
||||||
@@ -157,8 +145,9 @@ class TorrentFileParser(object):
|
|||||||
|
|
||||||
def _list_items_generator(self):
|
def _list_items_generator(self):
|
||||||
while True:
|
while True:
|
||||||
|
try:
|
||||||
element = self._next_element()
|
element = self._next_element()
|
||||||
if element is _END:
|
except InvalidTorrentFileException:
|
||||||
return
|
return
|
||||||
yield element
|
yield element
|
||||||
|
|
||||||
@@ -171,7 +160,7 @@ class TorrentFileParser(object):
|
|||||||
while char != end:
|
while char != end:
|
||||||
# noinspection PyTypeChecker
|
# noinspection PyTypeChecker
|
||||||
if not b'0' <= char <= b'9':
|
if not b'0' <= char <= b'9':
|
||||||
raise InvalidTorrentDataException(self._pos - 1)
|
raise InvalidTorrentFileException(self._pos)
|
||||||
value = value * 10 + int(char) - int(b'0')
|
value = value * 10 + int(char) - int(b'0')
|
||||||
char = self._read_byte(1)
|
char = self._read_byte(1)
|
||||||
return value
|
return value
|
||||||
@@ -180,13 +169,7 @@ class TorrentFileParser(object):
|
|||||||
length = self._next_int(b':')
|
length = self._next_int(b':')
|
||||||
raw = self._read_byte(length)
|
raw = self._read_byte(length)
|
||||||
if decode:
|
if decode:
|
||||||
try:
|
|
||||||
string = raw.decode(self._encoding)
|
string = raw.decode(self._encoding)
|
||||||
except UnicodeDecodeError as e:
|
|
||||||
raise InvalidTorrentDataException(
|
|
||||||
self._pos - length + e.start,
|
|
||||||
"Fail to decode string at pos {pos} using " + e.encoding
|
|
||||||
)
|
|
||||||
return string
|
return string
|
||||||
return raw
|
return raw
|
||||||
|
|
||||||
@@ -197,9 +180,7 @@ class TorrentFileParser(object):
|
|||||||
def _next_hash(self, p_len=20, need_list=True):
|
def _next_hash(self, p_len=20, need_list=True):
|
||||||
raw = self._next_string(decode=False)
|
raw = self._next_string(decode=False)
|
||||||
if len(raw) % p_len != 0:
|
if len(raw) % p_len != 0:
|
||||||
raise InvalidTorrentDataException(
|
raise InvalidTorrentFileException(self._pos)
|
||||||
self._pos - len(raw), "Hash bit length not match at pos {pos}"
|
|
||||||
)
|
|
||||||
res = [
|
res = [
|
||||||
''.join([self.__to_hex(c) for c in h])
|
''.join([self.__to_hex(c) for c in h])
|
||||||
for h in (raw[x:x+p_len] for x in range(0, len(raw), p_len))
|
for h in (raw[x:x+p_len] for x in range(0, len(raw), p_len))
|
||||||
@@ -210,9 +191,8 @@ class TorrentFileParser(object):
|
|||||||
return res[0]
|
return res[0]
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@staticmethod
|
def _next_end(self):
|
||||||
def _next_end():
|
raise InvalidTorrentFileException(self._pos)
|
||||||
return _END
|
|
||||||
|
|
||||||
def _next_type(self):
|
def _next_type(self):
|
||||||
for (element_type, indicator) in self.TYPES:
|
for (element_type, indicator) in self.TYPES:
|
||||||
@@ -221,7 +201,7 @@ class TorrentFileParser(object):
|
|||||||
if indicator == char:
|
if indicator == char:
|
||||||
return element_type
|
return element_type
|
||||||
self._seek_back(indicator_length)
|
self._seek_back(indicator_length)
|
||||||
raise InvalidTorrentDataException(self._pos)
|
raise InvalidTorrentFileException(self._pos)
|
||||||
|
|
||||||
def _type_to_func(self, t):
|
def _type_to_func(self, t):
|
||||||
return getattr(self, '_next_' + t)
|
return getattr(self, '_next_' + t)
|
||||||
@@ -276,7 +256,7 @@ def __main():
|
|||||||
else:
|
else:
|
||||||
target_file = open(args.file, 'rb')
|
target_file = open(args.file, 'rb')
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
sys.stderr.write('File "{}" not exist\n'.format(args.file))
|
sys.stderr.write('Unable to find file {}\n'.format(args.file))
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
# noinspection PyUnboundLocalVariable
|
# noinspection PyUnboundLocalVariable
|
||||||
|
|||||||
Reference in new issue
Block a user