Browse Source

support specified encoding in torrent... drop debug print..

main
John-Mark Gurney 2 years ago
parent
commit
c2918b9c8a
1 changed files with 24 additions and 5 deletions
  1. +24
    -5
      __init__.py

+ 24
- 5
__init__.py View File

@@ -11,10 +11,11 @@ import tempfile
import unittest

class Storage:
def __init__(self, rootpath, files, piecelen):
def __init__(self, rootpath, files, piecelen, encoding='us-ascii'):
self._rootpath = pathlib.Path(rootpath)
self._files = files
self._piecelen = piecelen
self._encoding = encoding

self._buildindex()

@@ -30,7 +31,7 @@ class Storage:
try:
curfile = next(files)
fname = pathlib.Path(
*(x.decode('us-ascii') for x in
*(x.decode(self._encoding) for x in
curfile['path']))
curfilepath = self._rootpath / fname
except StopIteration:
@@ -61,11 +62,14 @@ def validate(torrent, basedir):

basedir = pathlib.Path(basedir)

print(repr(torrent))
try:
encoding = torrent['encoding'].decode('us-ascii')
except KeyError:
encoding = 'us-ascii'

torrentdir = basedir / info['name'].decode('us-ascii')
torrentdir = basedir / info['name'].decode(encoding)

stor = Storage(torrentdir, info['files'], info['piece length'])
stor = Storage(torrentdir, info['files'], info['piece length'], encoding)

pieces = info['pieces']
for num, i in enumerate(pieces[x:x+20] for x in range(0, len(pieces),
@@ -124,6 +128,21 @@ class _TestCases(unittest.TestCase):

validate(self.torrent, self.basetempdir)

# encoded names

sd = self.basetempdir / 'thai'
sd.mkdir()

self.make_files(sd, { 'thai - สวัสดี.txt': b'hello\n'
})

tor = importlib.resources.files(__name__)
tor = tor / 'fixtures' / 'thai.torrent'
with tor.open('rb') as fp:
torrent = bencode.bdecode(fp.read())

validate(torrent, self.basetempdir)

def test_verification(self):
# Testing for "missing" files
# piece size 2 (aka 4 bytes)


Loading…
Cancel
Save