bcoder 1.0__tar.gz → 2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of bcoder might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: bcoder
3
- Version: 1.0
3
+ Version: 2.0
4
4
  Summary: Tool to decode and encode .torrent files
5
5
  Author-email: Mykola Bezrukyi <kolya.bezrukiy.03@gmail.com>
6
6
  License: Copyright (c) 2025 Mykola Bezrukyi
@@ -11,9 +11,11 @@ License: Copyright (c) 2025 Mykola Bezrukyi
11
11
 
12
12
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13
13
 
14
+ Project-URL: Homepage, https://github.com/mbezrukyi/bcoder
14
15
  Requires-Python: >=3.13
15
16
  Description-Content-Type: text/markdown
16
17
  License-File: LICENSE
18
+ Dynamic: license-file
17
19
 
18
20
  # bcoder
19
21
 
@@ -52,16 +54,16 @@ encoder = BEncoder()
52
54
 
53
55
  data = {
54
56
  b"announce-list": [
55
- ["udp:..."],
56
- ["udp:..."],
57
+ [b"udp:..."],
58
+ [b"udp:..."],
57
59
  ],
58
- "info": {
59
- "piece": b"xfsasf",
60
- "piece length": 123,
60
+ b"info": {
61
+ b"piece": b"xfsasf",
62
+ b"piece length": 123,
61
63
  },
62
64
  }
63
65
 
64
66
  encoded = encoder.encode(data)
65
67
  ```
66
68
 
67
- __NOTE__: `BEncoder` can handle mixed types of Python strings - `bytes` and `str`.
69
+ __NOTE__: `BEncoder` is spec-strict (BEP 3). Only `bytes`, `int`, `list`, and `dict` with `bytes` keys are accepted. Pass `str` through `.encode("utf-8")` yourself if you need to encode text.
@@ -35,16 +35,16 @@ encoder = BEncoder()
35
35
 
36
36
  data = {
37
37
  b"announce-list": [
38
- ["udp:..."],
39
- ["udp:..."],
38
+ [b"udp:..."],
39
+ [b"udp:..."],
40
40
  ],
41
- "info": {
42
- "piece": b"xfsasf",
43
- "piece length": 123,
41
+ b"info": {
42
+ b"piece": b"xfsasf",
43
+ b"piece length": 123,
44
44
  },
45
45
  }
46
46
 
47
47
  encoded = encoder.encode(data)
48
48
  ```
49
49
 
50
- __NOTE__: `BEncoder` can handle mixed types of Python strings - `bytes` and `str`.
50
+ __NOTE__: `BEncoder` is spec-strict (BEP 3). Only `bytes`, `int`, `list`, and `dict` with `bytes` keys are accepted. Pass `str` through `.encode("utf-8")` yourself if you need to encode text.
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "bcoder"
7
- version = "1.0"
7
+ version = "2.0"
8
8
  authors = [
9
9
  {name = "Mykola Bezrukyi", email = "kolya.bezrukiy.03@gmail.com"},
10
10
  ]
@@ -13,5 +13,8 @@ readme = "README.md"
13
13
  requires-python = ">=3.13"
14
14
  license = {file = "LICENSE"}
15
15
 
16
+ [project.urls]
17
+ "Homepage" = "https://github.com/mbezrukyi/bcoder"
18
+
16
19
  [tool.setuptools.packages.find]
17
20
  where = ["src"]
@@ -0,0 +1,86 @@
1
+ from enum import Enum
2
+ from typing import Union
3
+
4
+ from .errors import DecodeError
5
+
6
+ BDecodeType = Union[
7
+ dict[bytes, "BDecodeType"],
8
+ bytes,
9
+ list["BDecodeType"],
10
+ int,
11
+ ]
12
+
13
+
14
+ class DecodeType(Enum):
15
+ DICTIONARY = b"d"
16
+ STRING = b"1234567890"
17
+ LIST = b"l"
18
+ INTEGER = b"i"
19
+
20
+ @classmethod
21
+ def from_code(cls, code: int) -> "DecodeType":
22
+ return next(filter(lambda m: code in m.value, cls))
23
+
24
+
25
+ class BDecoder:
26
+ def __init__(self):
27
+ self._decoders = {
28
+ DecodeType.DICTIONARY: self._decode_dict,
29
+ DecodeType.STRING: self._decode_str,
30
+ DecodeType.LIST: self._decode_list,
31
+ DecodeType.INTEGER: self._decode_int,
32
+ }
33
+
34
+ def decode(self, data: bytes) -> BDecodeType:
35
+ value, _ = self._decode(data, 0)
36
+
37
+ return value
38
+
39
+ def _decode(self, data: bytes, i: int) -> tuple[BDecodeType, int]:
40
+ return self._decoders[DecodeType.from_code(data[i])](data, i)
41
+
42
+ def _decode_dict(self, data: bytes, i: int) -> tuple[dict[bytes, BDecodeType], int]:
43
+ result = {}
44
+
45
+ i += 1
46
+
47
+ while data[i] != ord("e"):
48
+ key, i = self._decode_str(data, i)
49
+ result[key], i = self._decode(data, i)
50
+
51
+ return result, i + 1
52
+
53
+ def _decode_str(self, data: bytes, i: int) -> tuple[bytes, int]:
54
+ if data[i] == ord("0") and data[i + 1] != ord(":"):
55
+ raise DecodeError(f"Invalid leading '0' value for str in {i} position")
56
+
57
+ colon = data.index(b":", i)
58
+ length = int(data[i:colon])
59
+
60
+ start = colon + 1
61
+ end = start + length
62
+
63
+ return data[start:end], end
64
+
65
+ def _decode_list(self, data: bytes, i: int) -> tuple[list[BDecodeType], int]:
66
+ result = []
67
+
68
+ i += 1
69
+
70
+ while data[i] != ord("e"):
71
+ value, i = self._decode(data, i)
72
+ result.append(value)
73
+
74
+ return result, i + 1
75
+
76
+ def _decode_int(self, data: bytes, i: int) -> tuple[int, int]:
77
+ i += 1
78
+
79
+ if data[i] == ord("-") and data[i + 1] == ord("0"):
80
+ raise DecodeError(f"Invalid '-0' value for int in {i} position")
81
+ if data[i] == ord("0") and data[i + 1] != ord("e"):
82
+ raise DecodeError(f"Invalid leading '0' value for int in {i} position")
83
+
84
+ end = data.index(b"e", i)
85
+
86
+ return int(data[i:end]), end + 1
@@ -2,8 +2,7 @@ from enum import Enum
2
2
  from typing import Callable, Union
3
3
 
4
4
  BEncodeType = Union[
5
- dict[Union[str, bytes], "BEncodeType"],
6
- str,
5
+ dict[bytes, "BEncodeType"],
7
6
  list["BEncodeType"],
8
7
  int,
9
8
  bytes,
@@ -12,7 +11,6 @@ BEncodeType = Union[
12
11
 
13
12
  class EncodeType(Enum):
14
13
  DICTIONARY = dict
15
- STRING = str
16
14
  LIST = list
17
15
  INTEGER = int
18
16
  BYTES = bytes
@@ -23,12 +21,9 @@ class EncodeType(Enum):
23
21
 
24
22
 
25
23
  class BEncoder:
26
- def __init__(self, encoding: str = "utf-8"):
27
- self._encoding = encoding
28
-
24
+ def __init__(self):
29
25
  self._encoders = {
30
26
  EncodeType.DICTIONARY: self._encode_dict,
31
- EncodeType.STRING: self._encode_str,
32
27
  EncodeType.LIST: self._encode_list,
33
28
  EncodeType.INTEGER: self._encode_int,
34
29
  EncodeType.BYTES: self._encode_bytes,
@@ -40,32 +35,23 @@ class BEncoder:
40
35
  def _get_encoder(self, value: BEncodeType) -> Callable[["BEncoder", BEncodeType], bytes]:
41
36
  return self._encoders[EncodeType.from_value(value)]
42
37
 
43
- def _encode_dict(self, d: dict[str, BEncodeType]) -> bytes:
38
+ def _encode_dict(self, d: dict[bytes, BEncodeType]) -> bytes:
44
39
  encoded_dict = b"".join(
45
- self._get_encoder(k)(k) + self._get_encoder(v)(v)
46
- for k, v in d.items()
40
+ self._encode_bytes(k) + self._get_encoder(v)(v)
41
+ for k, v in sorted(d.items())
47
42
  )
48
43
 
49
44
  return b"d" + encoded_dict + b"e"
50
45
 
51
- def _encode_str(self, s: str) -> bytes:
52
- length = str(len(s)).encode(self._encoding)
53
- encoded_str = s.encode(self._encoding)
54
-
55
- return length + b":" + encoded_str
56
-
57
46
  def _encode_list(self, l: list) -> bytes:
58
47
  encoded_values = b"".join(
59
48
  self._get_encoder(v)(v)
60
49
  for v in l
61
50
  )
62
-
63
51
  return b"l" + encoded_values + b"e"
64
52
 
65
53
  def _encode_int(self, i: int) -> bytes:
66
- return b"i" + str(i).encode(self._encoding) + b"e"
54
+ return b"i" + str(i).encode() + b"e"
67
55
 
68
56
  def _encode_bytes(self, b: bytes) -> bytes:
69
- length = str(len(b)).encode(self._encoding)
70
-
71
- return length + b":" + b
57
+ return str(len(b)).encode() + b":" + b
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: bcoder
3
- Version: 1.0
3
+ Version: 2.0
4
4
  Summary: Tool to decode and encode .torrent files
5
5
  Author-email: Mykola Bezrukyi <kolya.bezrukiy.03@gmail.com>
6
6
  License: Copyright (c) 2025 Mykola Bezrukyi
@@ -11,9 +11,11 @@ License: Copyright (c) 2025 Mykola Bezrukyi
11
11
 
12
12
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13
13
 
14
+ Project-URL: Homepage, https://github.com/mbezrukyi/bcoder
14
15
  Requires-Python: >=3.13
15
16
  Description-Content-Type: text/markdown
16
17
  License-File: LICENSE
18
+ Dynamic: license-file
17
19
 
18
20
  # bcoder
19
21
 
@@ -52,16 +54,16 @@ encoder = BEncoder()
52
54
 
53
55
  data = {
54
56
  b"announce-list": [
55
- ["udp:..."],
56
- ["udp:..."],
57
+ [b"udp:..."],
58
+ [b"udp:..."],
57
59
  ],
58
- "info": {
59
- "piece": b"xfsasf",
60
- "piece length": 123,
60
+ b"info": {
61
+ b"piece": b"xfsasf",
62
+ b"piece length": 123,
61
63
  },
62
64
  }
63
65
 
64
66
  encoded = encoder.encode(data)
65
67
  ```
66
68
 
67
- __NOTE__: `BEncoder` can handle mixed types of Python strings - `bytes` and `str`.
69
+ __NOTE__: `BEncoder` is spec-strict (BEP 3). Only `bytes`, `int`, `list`, and `dict` with `bytes` keys are accepted. Pass `str` through `.encode("utf-8")` yourself if you need to encode text.
@@ -1,98 +0,0 @@
1
- from enum import Enum
2
- from typing import Callable, Union
3
-
4
- from .errors import DecodeError
5
-
6
- BDecodeType = Union[
7
- dict[bytes, "BDecodeType"],
8
- bytes,
9
- list["BDecodeType"],
10
- int,
11
- ]
12
-
13
-
14
- class DecodeType(Enum):
15
- DICTIONARY = b"d"
16
- STRING = b"1234567890"
17
- LIST = b"l"
18
- INTEGER = b"i"
19
-
20
- @classmethod
21
- def from_code(cls, code: int) -> "DecodeType":
22
- return next(filter(lambda m: code in m.value, cls))
23
-
24
-
25
- class BDecoder:
26
- def __init__(self):
27
- self._data = None
28
- self._i = None
29
-
30
- self._decoders = {
31
- DecodeType.DICTIONARY: self._decode_dict,
32
- DecodeType.STRING: self._decode_str,
33
- DecodeType.LIST: self._decode_list,
34
- DecodeType.INTEGER: self._decode_int,
35
- }
36
-
37
- def decode(self, data: bytes) -> BDecodeType:
38
- self._data = data
39
- self._i = 0
40
-
41
- return self._get_decoder()()
42
-
43
- def _get_decoder(self) -> Callable[["BDecoder"], BDecodeType]:
44
- return self._decoders[DecodeType.from_code(self._data[self._i])]
45
-
46
- def _decode_dict(self) -> dict[bytes, BDecodeType]:
47
- result = {}
48
-
49
- self._i += 1
50
-
51
- while self._data[self._i] != ord("e"):
52
- key = self._decode_str()
53
- result[key] = self._get_decoder()()
54
-
55
- self._i += 1
56
-
57
- return result
58
-
59
- def _decode_str(self) -> bytes:
60
- if self._data[self._i] == ord("0") and self._data[self._i + 1] != ord(":"):
61
- raise DecodeError(f"Invalid leading '0' value for str in {self._i} position")
62
-
63
- colon = self._data.index(b":", self._i)
64
- length = int(self._data[self._i: colon])
65
-
66
- start = colon + 1
67
- end = start + length
68
-
69
- self._i = end
70
-
71
- return self._data[start:end]
72
-
73
- def _decode_list(self) -> list[BDecodeType]:
74
- result = []
75
-
76
- self._i += 1
77
-
78
- while self._data[self._i] != ord("e"):
79
- result.append(self._get_decoder()())
80
-
81
- self._i += 1
82
-
83
- return result
84
-
85
- def _decode_int(self) -> int:
86
- self._i += 1
87
-
88
- if self._data[self._i] == ord("-") and self._data[self._i + 1] == ord("0"):
89
- raise DecodeError(f"Invalid '-0' value for int in {self._i} position")
90
- if self._data[self._i] == ord("0") and self._data[self._i + 1] != ord("e"):
91
- raise DecodeError(f"Invalid leading '0' value for int in {self._i} position")
92
-
93
- start = self._i
94
- end = self._data.index(b"e", self._i)
95
-
96
- self._i = end + 1
97
-
98
- return int(self._data[start:end])
File without changes
File without changes
File without changes
File without changes