Skip to content

Commit 66848a4

Browse files
committed
Updated imports and removed unnecessary imports
1 parent fc6f65d commit 66848a4

13 files changed

Lines changed: 327 additions & 76 deletions

File tree

src/caterpillar/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
__author__ = "MatrixEditor"
2020

2121

22-
def native_support():
22+
def native_support() -> bool:
2323
"""Return True if native support is available."""
2424
try:
2525
# pylint: disable-next=import-outside-toplevel
@@ -31,4 +31,6 @@ def native_support():
3131

3232

3333
# Explicitly report deprecation warnings
34-
warnings.filterwarnings("default", module="caterpillar")
34+
warnings.filterwarnings("default", module="caterpillar")
35+
36+
__all__ = ["__version__", "__author__", "native_support"]

src/caterpillar/byteorder.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
from enum import Enum
1616
from typing import Optional
1717

18-
BYTEORDER_FIELD: str = ...
19-
2018
class ByteOrder:
2119
name: str
2220
ch: str

src/caterpillar/fields/__init__.py

Lines changed: 154 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# You should have received a copy of the GNU General Public License
1414
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1515
from ._base import Field, INVALID_DEFAULT, DEFAULT_OPTION, singleton
16-
from ._mixin import FieldMixin, FieldStruct, Chain, Operator
16+
from ._mixin import FieldMixin, FieldStruct, Chain, Operator, get_args, get_kwargs
1717
from .common import (
1818
PyStructFormattedField,
1919
Transformer,
@@ -54,6 +54,7 @@
5454
Aligned,
5555
align,
5656
Lazy,
57+
ENUM_STRICT,
5758
)
5859
from .varint import VarInt, VARINT_LSB, vint
5960
from .compression import (
@@ -63,9 +64,21 @@
6364
LZMACompressed,
6465
LZOCompressed,
6566
)
66-
from .crypto import Encrypted, Xor, Or, And
67-
from .net import IPv4Address, IPv6Address, MAC, MACAddress
68-
from .pointer import uintptr, intptr, offintptr, offuintptr, Pointer
67+
from .crypto import Encrypted, Xor, Or, And, KeyCipher
68+
from .net import MAC, MACAddress, IPv4Address, IPv6Address
69+
from .pointer import (
70+
uintptr,
71+
intptr,
72+
offintptr,
73+
offuintptr,
74+
Pointer,
75+
pointer,
76+
intptr_fn,
77+
PTR_STRICT,
78+
relative_pointer,
79+
RelativePointer,
80+
uintptr_fn,
81+
)
6982
from .conditional import ConditionalChain, If, Else, ElseIf
7083
from .hook import IOHook
7184
from .digest import (
@@ -110,4 +123,141 @@
110123
Crc32_Field,
111124
Adler_Algo,
112125
Adler_Field,
126+
CTX_DIGEST,
127+
CTX_DIGEST_ALGO,
128+
CTX_DIGEST_HOOK,
129+
CTX_DIGEST_OBJ,
130+
HMACAlgorithm,
113131
)
132+
133+
__all__ = [
134+
"Digest",
135+
"Algorithm",
136+
"Md5",
137+
"Sha1",
138+
"Sha2_256",
139+
"Sha2_224",
140+
"Sha2_384",
141+
"Sha2_512",
142+
"Sha3_224",
143+
"Sha3_256",
144+
"Sha3_384",
145+
"Sha3_512",
146+
"Crc32",
147+
"Adler",
148+
"HMAC",
149+
"DigestField",
150+
"DigestFieldAction",
151+
"Md5_Algo",
152+
"Md5_Field",
153+
"Sha1_Algo",
154+
"Sha1_Field",
155+
"Sha2_256_Algo",
156+
"Sha2_256_Field",
157+
"Sha2_224_Algo",
158+
"Sha2_224_Field",
159+
"Sha2_384_Algo",
160+
"Sha2_384_Field",
161+
"Sha2_512_Algo",
162+
"Sha2_512_Field",
163+
"Sha3_224_Algo",
164+
"Sha3_224_Field",
165+
"Sha3_256_Algo",
166+
"Sha3_256_Field",
167+
"Sha3_384_Algo",
168+
"Sha3_384_Field",
169+
"Sha3_512_Algo",
170+
"Sha3_512_Field",
171+
"Crc32_Algo",
172+
"Crc32_Field",
173+
"Adler_Algo",
174+
"Adler_Field",
175+
"CTX_DIGEST",
176+
"CTX_DIGEST_ALGO",
177+
"CTX_DIGEST_HOOK",
178+
"CTX_DIGEST_OBJ",
179+
"HMACAlgorithm",
180+
"uintptr",
181+
"intptr",
182+
"offintptr",
183+
"offuintptr",
184+
"Pointer",
185+
"pointer",
186+
"intptr_fn",
187+
"PTR_STRICT",
188+
"relative_pointer",
189+
"RelativePointer",
190+
"uintptr_fn",
191+
"Compressed",
192+
"ZLibCompressed",
193+
"Bz2Compressed",
194+
"LZMACompressed",
195+
"LZOCompressed",
196+
"PyStructFormattedField",
197+
"Transformer",
198+
"Const",
199+
"ConstBytes",
200+
"ConstString",
201+
"Enum",
202+
"String",
203+
"Bytes",
204+
"Memory",
205+
"Computed",
206+
"Pass",
207+
"CString",
208+
"Prefixed",
209+
"Int",
210+
"UInt",
211+
"padding",
212+
"char",
213+
"boolean",
214+
"int8",
215+
"uint8",
216+
"int16",
217+
"uint16",
218+
"int24",
219+
"uint24",
220+
"int32",
221+
"uint32",
222+
"int64",
223+
"uint64",
224+
"ssize_t",
225+
"size_t",
226+
"float16",
227+
"float32",
228+
"float64",
229+
"double",
230+
"void_ptr",
231+
"Uuid",
232+
"Aligned",
233+
"align",
234+
"Lazy",
235+
"ENUM_STRICT",
236+
"Field",
237+
"INVALID_DEFAULT",
238+
"DEFAULT_OPTION",
239+
"singleton",
240+
"FieldMixin",
241+
"FieldStruct",
242+
"Chain",
243+
"Operator",
244+
"get_args",
245+
"get_kwargs",
246+
"VarInt",
247+
"VARINT_LSB",
248+
"vint",
249+
"Encrypted",
250+
"Xor",
251+
"Or",
252+
"And",
253+
"KeyCipher",
254+
"MAC",
255+
"MACAddress",
256+
"IPv4Address",
257+
"IPv6Address",
258+
"ConditionalChain",
259+
"If",
260+
"Else",
261+
"ElseIf",
262+
"IOHook",
263+
]

src/caterpillar/fields/common.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
import warnings
1717

1818
from io import BytesIO
19-
from typing import Sequence, Any, Optional, Union, List, Callable
20-
from types import EllipsisType, NoneType
19+
from typing import Any, Union
20+
from types import NoneType
2121
from functools import cached_property
2222
from enum import Enum as _EnumType
2323
from uuid import UUID
2424

2525
from caterpillar.abc import (
2626
_StructLike,
27-
_ContextLambda,
2827
_StreamType,
2928
_ContextLike,
3029
)
@@ -39,7 +38,6 @@
3938
from caterpillar import registry
4039
from caterpillar._common import WithoutContextVar
4140
from caterpillar.shared import getstruct
42-
from caterpillar.model import getbits
4341

4442
from ._base import Field, INVALID_DEFAULT, singleton
4543
from ._mixin import FieldStruct

src/caterpillar/fields/common.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class Const(Transformer[_IT, _IT, _IT, _IT]):
104104

105105
_EnumT = TypeVar("_EnumT")
106106

107-
class Enum(Generic[_EnumT, _IT], Transformer[_EnumT, _IT, _EnumT, _IT]):
107+
class Enum(Generic[_EnumT, _IT], Transformer[_EnumT, _IT, Union[_EnumT, _IT], _IT]):
108108
model: Type[_EnumT]
109109
default: _EnumT
110110
def __init__(

src/caterpillar/fields/net.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
import binascii
1717
import re
1818

19-
from typing import Union, Any, Optional
20-
21-
2219
from .common import Transformer, uint32, UInt, Bytes
2320
from ._base import singleton
2421

src/caterpillar/fields/net.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
#
1313
# You should have received a copy of the GNU General Public License
1414
# along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
import ipaddress
16+
1517
from re import Pattern
16-
from ipaddress import IPv4Address, IPv6Address
1718

1819
from ._base import singleton as singleton
1920
from .common import (
@@ -24,8 +25,8 @@ from .common import (
2425
)
2526
from caterpillar.abc import _ContextLike
2627

27-
IPv4Address = Transformer[IPv4Address, int, IPv4Address, int]
28-
IPv6Address = Transformer[IPv6Address, int, IPv6Address, int]
28+
IPv4Address: Transformer[ipaddress.IPv4Address, int, ipaddress.IPv4Address, int]
29+
IPv6Address: Transformer[ipaddress.IPv6Address, int, ipaddress.IPv6Address, int]
2930

3031
class MACAddress(Transformer[str | bytes, bytes, bytes, bytes]):
3132
DELIMITERS: Pattern

src/caterpillar/model/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,26 @@
2727
)
2828
from ._bitfield import BitField, bitfield, BitFieldGroup, issigned, getbits
2929
from ._template import istemplate, template, TemplateTypeVar, derive
30+
31+
__all__ = [
32+
"Sequence",
33+
"Struct",
34+
"struct",
35+
"UnionHook",
36+
"union",
37+
"unpack",
38+
"unpack_file",
39+
"pack",
40+
"pack_into",
41+
"pack_file",
42+
"sizeof",
43+
"BitField",
44+
"bitfield",
45+
"BitFieldGroup",
46+
"issigned",
47+
"getbits",
48+
"istemplate",
49+
"template",
50+
"TemplateTypeVar",
51+
"derive",
52+
]

src/caterpillar/model/__init__.pyi

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/caterpillar/model/_base.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
CTX_SEQ,
2626
)
2727
from caterpillar.byteorder import (
28-
BYTEORDER_FIELD,
2928
ByteOrder,
3029
SysNative,
3130
Arch,
@@ -46,7 +45,7 @@
4645
Const,
4746
)
4847
from caterpillar._common import unpack_seq, pack_seq
49-
from caterpillar.shared import ATTR_ACTION_PACK, ATTR_ACTION_UNPACK, Action
48+
from caterpillar.shared import ATTR_ACTION_PACK, ATTR_ACTION_UNPACK, Action, ATTR_BYTEORDER
5049
from caterpillar import registry
5150

5251

@@ -265,7 +264,7 @@ def _process_field(self, name: str, annotation, default) -> Field:
265264
field = None
266265
struct = None
267266

268-
order = getattr(annotation, BYTEORDER_FIELD, self.order or SysNative)
267+
order = getattr(annotation, ATTR_BYTEORDER, self.order or SysNative)
269268
arch = self.arch or system_arch
270269
result = self._process_annotation(annotation, default, order, arch)
271270
if isinstance(result, Field):

0 commit comments

Comments
 (0)