Skip to content

Commit ffae9a4

Browse files
author
GuangChen2333
committed
feat(pack): Support bytes
1 parent a32cc01 commit ffae9a4

3 files changed

Lines changed: 15 additions & 9 deletions

File tree

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ authors = [
77
{ name = "GuangChen23333", email = "[email protected]" }
88
]
99
readme = "README.md"
10+
requires-python = ">=3.8"
11+
dynamic = ["dependencies"]
1012

1113
[tool.poetry]
1214
packages = [
1315
{ include = "structovo", from = "src" }
1416
]
1517

1618
[tool.poetry.dependencies]
17-
python = "^3.12"
19+
python = ">=3.8, <4.0"
1820

1921
[build-system]
2022
requires = ["poetry-core"]

src/structovo/_enums.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from enum import StrEnum
1+
from enum import Enum
22

3-
4-
class Endianness(StrEnum):
3+
class Endianness(Enum):
54
BIG = '>'
65
LITTLE = '<'
76
NETWORK = '!'

src/structovo/_pack.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TypeVar
1+
from typing import TypeVar, Optional
22
from ._types import BaseType, FixedString
33
from ._enums import Endianness
44

@@ -7,21 +7,26 @@
77

88
class Pack:
99
@classmethod
10-
def build(cls, endianness: Endianness = Endianness.NATIVE) -> bytes:
10+
def build(cls, endianness: Optional[Endianness] = Endianness.NATIVE) -> bytes:
1111
anns = cls.__annotations__
1212
data = cls.__dict__
1313

1414
result_list = []
1515

1616
for key, type_class in anns.items():
17-
if not issubclass(type_class, BaseType):
18-
raise ValueError(f"{type_class} is not an instance of structovo.BaseType")
17+
if not issubclass(type_class, BaseType) and not type_class is bytes:
18+
raise ValueError(f"{type_class} is not an instance of bytes or structovo.BaseType")
1919

20-
if type_class is FixedString:
20+
if type_class is bytes:
21+
result_list.append(data.get(key))
22+
continue
23+
24+
elif type_class is FixedString:
2125
try:
2226
value: FixedString = FixedString(data[key][0], data[key][1])
2327
except IndexError:
2428
raise ValueError('Using x: FixedString = (value: bytes, length: int)')
29+
2530
else:
2631
value: BaseType = type_class(data.get(key))
2732

0 commit comments

Comments
 (0)