|
37 | 37 | from .native_types import get_native_type |
38 | 38 | from .exceptions import CStructException, ParserError |
39 | 39 |
|
40 | | -__all__ = ['CStructMeta', 'AbstractCStruct', 'CEnumMeta', 'AbstractCEnum'] |
| 40 | +__all__ = ["CStructMeta", "AbstractCStruct", "CEnumMeta", "AbstractCEnum"] |
41 | 41 |
|
42 | 42 |
|
43 | 43 | class CStructMeta(ABCMeta): |
44 | 44 | __size__: int = 0 |
45 | 45 |
|
46 | 46 | def __new__(metacls: Type[type], name: str, bases: Tuple[str], namespace: Dict[str, Any]) -> Type[Any]: |
47 | | - __struct__ = namespace.get('__struct__', None) |
48 | | - namespace['__cls__'] = bases[0] if bases else None |
| 47 | + __struct__ = namespace.get("__struct__", None) |
| 48 | + namespace["__cls__"] = bases[0] if bases else None |
49 | 49 | # Parse the struct |
50 | | - if '__struct__' in namespace: |
51 | | - if isinstance(namespace['__struct__'], (str, Tokens)): |
| 50 | + if "__struct__" in namespace: |
| 51 | + if isinstance(namespace["__struct__"], (str, Tokens)): |
52 | 52 | namespace.update(parse_struct(**namespace)) |
53 | 53 | __struct__ = True |
54 | | - if '__def__' in namespace: |
| 54 | + if "__def__" in namespace: |
55 | 55 | namespace.update(parse_struct_def(**namespace)) |
56 | 56 | __struct__ = True |
57 | 57 | # Create the new class |
58 | 58 | new_class: Type[Any] = super().__new__(metacls, name, bases, namespace) |
59 | 59 | # Register the class |
60 | | - if __struct__ is not None and not namespace.get('__anonymous__'): |
| 60 | + if __struct__ is not None and not namespace.get("__anonymous__"): |
61 | 61 | STRUCTS[name] = new_class |
62 | 62 | return new_class |
63 | 63 |
|
@@ -128,23 +128,23 @@ def parse( |
128 | 128 | """ |
129 | 129 | cls_kargs: Dict[str, Any] = dict(kargs) |
130 | 130 | if __byte_order__ is not None: |
131 | | - cls_kargs['__byte_order__'] = __byte_order__ |
| 131 | + cls_kargs["__byte_order__"] = __byte_order__ |
132 | 132 | if __is_union__ is not None: |
133 | | - cls_kargs['__is_union__'] = __is_union__ |
134 | | - cls_kargs['__struct__'] = __struct__ |
| 133 | + cls_kargs["__is_union__"] = __is_union__ |
| 134 | + cls_kargs["__struct__"] = __struct__ |
135 | 135 | if isinstance(__struct__, (str, Tokens)): |
136 | | - del cls_kargs['__struct__'] |
| 136 | + del cls_kargs["__struct__"] |
137 | 137 | cls_kargs.update(parse_struct_def(__struct__, __cls__=cls, **cls_kargs)) |
138 | | - cls_kargs['__struct__'] = None |
| 138 | + cls_kargs["__struct__"] = None |
139 | 139 | elif isinstance(__struct__, dict): |
140 | | - del cls_kargs['__struct__'] |
| 140 | + del cls_kargs["__struct__"] |
141 | 141 | cls_kargs.update(__struct__) |
142 | | - cls_kargs['__struct__'] = None |
143 | | - __name__ = cls_kargs.get('__name__') or __name__ |
| 142 | + cls_kargs["__struct__"] = None |
| 143 | + __name__ = cls_kargs.get("__name__") or __name__ |
144 | 144 | if __name__ is None: # Anonymous struct |
145 | | - __name__ = cls.__name__ + '_' + hashlib.sha1(str(__struct__).encode('utf-8')).hexdigest() |
146 | | - cls_kargs['__anonymous__'] = True |
147 | | - cls_kargs['__name__'] = __name__ |
| 145 | + __name__ = cls.__name__ + "_" + hashlib.sha1(str(__struct__).encode("utf-8")).hexdigest() |
| 146 | + cls_kargs["__anonymous__"] = True |
| 147 | + cls_kargs["__name__"] = __name__ |
148 | 148 | return type(__name__, (cls,), cls_kargs) |
149 | 149 |
|
150 | 150 | def set_flexible_array_length(self, flexible_array_length: Optional[int]) -> None: |
@@ -173,7 +173,7 @@ def unpack(self, buffer: Optional[Union[bytes, BinaryIO]], flexible_array_length |
173 | 173 | flexible_array_length: flexible array length |
174 | 174 | """ |
175 | 175 | self.set_flexible_array_length(flexible_array_length) |
176 | | - if hasattr(buffer, 'read'): |
| 176 | + if hasattr(buffer, "read"): |
177 | 177 | buffer = buffer.read(self.size) # type: ignore |
178 | 178 | if not buffer: |
179 | 179 | return False |
@@ -237,19 +237,19 @@ def inspect(self, start_addr: Optional[int] = None, end_addr: Optional[int] = No |
237 | 237 | end_addr: end address |
238 | 238 | """ |
239 | 239 | buffer = StringIO() |
240 | | - if hasattr(self, '__mem__'): |
| 240 | + if hasattr(self, "__mem__"): |
241 | 241 | mem = self.__mem__ |
242 | 242 | else: |
243 | 243 | mem = self.pack() |
244 | 244 | for i in range(start_addr or 0, end_addr or self.size, 16): |
245 | 245 | row = mem[i : i + 16] |
246 | 246 | buffer.write(f"{i:08x} ") |
247 | 247 | for j, c in enumerate(row): |
248 | | - separator = ' ' if j == 7 else '' |
| 248 | + separator = " " if j == 7 else "" |
249 | 249 | buffer.write(f" {c:02x}{separator}") |
250 | 250 | buffer.write(" |") |
251 | 251 | for c in row: |
252 | | - buffer.write(chr(c) if c >= 32 and c < 127 else '.') |
| 252 | + buffer.write(chr(c) if c >= 32 and c < 127 else ".") |
253 | 253 | buffer.write("|") |
254 | 254 | buffer.write("\n") |
255 | 255 | buffer.seek(0, 0) |
@@ -368,16 +368,16 @@ def parse( |
368 | 368 | """ |
369 | 369 | cls_kargs: Dict[str, Any] = dict(kargs) |
370 | 370 | if __size__ is not None: |
371 | | - cls_kargs['__size__'] = __size__ |
| 371 | + cls_kargs["__size__"] = __size__ |
372 | 372 | if __native_format__ is not None: |
373 | | - cls_kargs['__native_format__'] = __native_format__ |
| 373 | + cls_kargs["__native_format__"] = __native_format__ |
374 | 374 |
|
375 | 375 | if isinstance(__enum__, (str, Tokens)): |
376 | 376 | cls_kargs.update(parse_enum_def(__enum__, __cls__=cls, **cls_kargs)) |
377 | 377 | elif isinstance(__enum__, dict): |
378 | 378 | cls_kargs.update(__enum__) |
379 | 379 |
|
380 | | - __name__ = cls_kargs.get('__name__') or __name__ |
| 380 | + __name__ = cls_kargs.get("__name__") or __name__ |
381 | 381 | if __name__ is None: |
382 | 382 | __name__ = cls.__name__ + "_" + hashlib.sha1(str(__enum__).encode("utf-8")).hexdigest() |
383 | 383 | cls_kargs["__anonymous__"] = True |
|
0 commit comments