Many changes, see bellow:

- Added
  - BEncoder
  - BDecoder
  - encode
  - decode
  - errors argument for TorrentFileParser and parse_torrent_file (#4)
- Changed
  - TorrentFileCreator rename to BEncoder
  - TorrentFileParser, BEncoder don't need outmost level is dict
- Updated
  - README
  - CHANGELOG
  - LICENSE

and tests for above changes.
This commit is contained in:
7sDream committed 2018-06-23 01:44:10 +08:00
1 parent 94b8f1eee0
commit ee3128b32b
12 files changed
+206 -54

No files matched your search

+3
View File
@@ -1,2 +1,5 @@
from .test_create import *
from .test_parse import *
from .test_encoding_error import *
from .test_encode import *
from .test_decode import *
+7 -3
View File
@@ -6,7 +6,7 @@ import io
import os.path
import unittest
from torrent_parser import TorrentFileParser, TorrentFileCreator
from torrent_parser import TorrentFileParser, BEncoder
class TestCreate(unittest.TestCase):
@@ -17,15 +17,19 @@ class TestCreate(unittest.TestCase):
data = collections.OrderedDict()
data['a'] = 1
data['b'] = 2
self.assertEqual(TorrentFileCreator(data).encode(), b'd1:ai1e1:bi2ee')
self.assertEqual(BEncoder(data).encode(), b'd1:ai1e1:bi2ee')
def test_same_output_if_no_edit(self):
with open(self.REAL_FILE, 'rb') as fp:
in_data = fp.read()
data = TorrentFileParser(io.BytesIO(in_data), True).parse()
out_data = TorrentFileCreator(data).encode()
out_data = BEncoder(data).encode()
m1 = hashlib.md5()
m1.update(in_data)
m2 = hashlib.md5()
m2.update(out_data)
self.assertEqual(m1.digest(), m2.digest())
def test_dont_need_dict_outmost(self):
data = 123456
self.assertEqual(BEncoder(data).encode(), b'i123456e')
+11
View File
@@ -0,0 +1,11 @@
from __future__ import unicode_literals
import unittest
from torrent_parser import decode
class TestDecode(unittest.TestCase):
def test_decode(self):
self.assertEqual(decode(b'i12345e'), 12345)
+11
View File
@@ -0,0 +1,11 @@
from __future__ import unicode_literals
import unittest
from torrent_parser import encode
class TestEncode(unittest.TestCase):
def test_encode(self):
self.assertEqual(encode(12345), b'i12345e')
+25
View File
@@ -0,0 +1,25 @@
from __future__ import unicode_literals
import os.path
import unittest
from torrent_parser import (
TorrentFileParser, parse_torrent_file, InvalidTorrentDataException
)
class TestDecodingError(unittest.TestCase):
TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), 'test_files')
FILE = os.path.join(TEST_FILES_DIR, 'utf8.encoding.error.torrent')
def test_default_option_will_raise_exception(self):
with self.assertRaises(InvalidTorrentDataException):
parse_torrent_file(self.FILE)
with self.assertRaises(InvalidTorrentDataException):
with open(self.FILE, 'rb') as f:
TorrentFileParser(f).parse()
def test_not_raise_exception_when_use_ignore(self):
parse_torrent_file(self.FILE, errors='ignore')
with open(self.FILE, 'rb') as f:
TorrentFileParser(f, errors='ignore').parse()
+1
View File
@@ -0,0 +1 @@
8:announce
Binary file not shown.
+5
View File
@@ -11,6 +11,7 @@ class TestParse(unittest.TestCase):
TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), 'test_files')
REAL_FILE = os.path.join(TEST_FILES_DIR, 'real.torrent')
NEG_FILE = os.path.join(TEST_FILES_DIR, 'neg.torrent')
STRING_FILE = os.path.join(TEST_FILES_DIR, 'outmost.string.torrent')
def test_parse_torrent_file_use_shortcut(self):
parse_torrent_file(self.REAL_FILE)
@@ -53,6 +54,10 @@ class TestParse(unittest.TestCase):
data = parse_torrent_file(self.NEG_FILE)
self.assertEqual(data['neg'], -1)
def test_dont_need_dict_outmost(self):
data = parse_torrent_file(self.STRING_FILE)
self.assertEqual(data, 'announce')
if __name__ == '__main__':
unittest.main()