|
12 | 12 |
|
13 | 13 | """bencode.py - bencode encoder + decoder.""" |
14 | 14 |
|
15 | | -from bencode.common import Bencached |
16 | | -from bencode.decoder import BencodeDecoder |
17 | | -from bencode.encoder import BencodeEncoder |
| 15 | +from bencode.BTL import BTFailure |
18 | 16 | from bencode.exceptions import BencodeDecodeError |
19 | | - |
20 | | -try: |
21 | | - import pathlib |
22 | | -except ImportError: |
23 | | - pathlib = None |
| 17 | +from bencodepy import Bencached, Bencode |
24 | 18 |
|
25 | 19 | __all__ = ( |
| 20 | + 'BTFailure', |
26 | 21 | 'Bencached', |
27 | | - 'Bencode', |
28 | | - 'BencodeDecoder', |
29 | 22 | 'BencodeDecodeError', |
30 | | - 'BencodeEncoder', |
31 | 23 | 'bencode', |
32 | 24 | 'bdecode', |
33 | 25 | 'bread', |
|
37 | 29 | ) |
38 | 30 |
|
39 | 31 |
|
40 | | -class Bencode(object): |
41 | | - def __init__(self): |
42 | | - self.decoder = BencodeDecoder() |
43 | | - self.encoder = BencodeEncoder() |
44 | | - |
45 | | - def decode(self, value): |
46 | | - return self.decoder.decode(value) |
47 | | - |
48 | | - def encode(self, value): |
49 | | - return self.encoder.encode(value) |
50 | | - |
51 | | - |
52 | | -DEFAULT = Bencode() |
53 | | - |
54 | | - |
55 | | -def bencode(value): |
56 | | - # type: (Union[Tuple, List, OrderedDict, Dict, bool, int, str, bytes]) -> bytes |
57 | | - """ |
58 | | - Encode ``value`` into the bencode format. |
59 | | -
|
60 | | - :param value: Value |
61 | | - :type value: object |
62 | | -
|
63 | | - :return: Bencode formatted string |
64 | | - :rtype: str |
65 | | - """ |
66 | | - return DEFAULT.encode(value) |
67 | | - |
68 | | - |
69 | | -def bdecode(value): |
70 | | - # type: (bytes) -> Union[Tuple, List, OrderedDict, bool, int, str, bytes] |
71 | | - """ |
72 | | - Decode bencode formatted byte string ``value``. |
73 | | -
|
74 | | - :param value: Bencode formatted string |
75 | | - :type value: bytes |
76 | | -
|
77 | | - :return: Decoded value |
78 | | - :rtype: object |
79 | | - """ |
80 | | - return DEFAULT.decode(value) |
81 | | - |
82 | | - |
83 | | -def bread(fd): |
84 | | - # type: (Union[bytes, str, pathlib.Path, pathlib.PurePath, TextIO, BinaryIO]) -> bytes |
85 | | - """Return bdecoded data from filename, file, or file-like object. |
86 | | -
|
87 | | - if fd is a bytes/string or pathlib.Path-like object, it is opened and |
88 | | - read, otherwise .read() is used. if read() not available, exception |
89 | | - raised. |
90 | | - """ |
91 | | - if isinstance(fd, (bytes, str)): |
92 | | - with open(fd, 'rb') as fd: |
93 | | - return bdecode(fd.read()) |
94 | | - elif pathlib is not None and isinstance(fd, (pathlib.Path, pathlib.PurePath)): |
95 | | - with open(str(fd), 'rb') as fd: |
96 | | - return bdecode(fd.read()) |
97 | | - else: |
98 | | - return bdecode(fd.read()) |
99 | | - |
100 | | - |
101 | | -def bwrite(data, # type: Union[Tuple, List, OrderedDict, Dict, bool, int, str, bytes] |
102 | | - fd # type: Union[bytes, str, pathlib.Path, pathlib.PurePath, TextIO, BinaryIO] |
103 | | - ): |
104 | | - # type: (...) -> None |
105 | | - """Write data in bencoded form to filename, file, or file-like object. |
106 | | -
|
107 | | - if fd is bytes/string or pathlib.Path-like object, it is opened and |
108 | | - written to, otherwise .write() is used. if write() is not available, |
109 | | - exception raised. |
110 | | - """ |
111 | | - if isinstance(fd, (bytes, str)): |
112 | | - with open(fd, 'wb') as fd: |
113 | | - fd.write(bencode(data)) |
114 | | - elif pathlib is not None and isinstance(fd, (pathlib.Path, pathlib.PurePath)): |
115 | | - with open(str(fd), 'wb') as fd: |
116 | | - fd.write(bencode(data)) |
117 | | - else: |
118 | | - fd.write(bencode(data)) |
| 32 | +DEFAULT = Bencode( |
| 33 | + encoding='utf-8', |
| 34 | + encoding_fallback='value', |
| 35 | + dict_ordered=True, |
| 36 | + dict_ordered_sort=True |
| 37 | +) |
119 | 38 |
|
| 39 | +bdecode = DEFAULT.decode |
| 40 | +bencode = DEFAULT.encode |
| 41 | +bread = DEFAULT.read |
| 42 | +bwrite = DEFAULT.write |
120 | 43 |
|
121 | | -# Compatibility Proxies |
122 | | -encode = bencode |
123 | 44 | decode = bdecode |
| 45 | +encode = bencode |
0 commit comments