Skip to content

Commit 8d52267

Browse files
committed
Black code style
1 parent f7b0372 commit 8d52267

17 files changed

+257
-256
lines changed

cstruct/__init__.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
# IN THE SOFTWARE.
2323
#
2424

25-
__author__ = 'Andrea Bonomi <andrea.bonomi@gmail.com>'
26-
__license__ = 'MIT'
27-
__version__ = '4.0'
28-
__date__ = '15 August 2013'
25+
__author__ = "Andrea Bonomi <andrea.bonomi@gmail.com>"
26+
__license__ = "MIT"
27+
__version__ = "4.0"
28+
__date__ = "15 August 2013"
2929

3030
from typing import Any, Dict, Optional, Type, Union
3131
from .base import (
@@ -46,20 +46,20 @@
4646
from .native_types import get_native_type
4747

4848
__all__ = [
49-
'LITTLE_ENDIAN',
50-
'BIG_ENDIAN',
51-
'NATIVE_ORDER',
52-
'CHAR_ZERO',
53-
'CStruct',
54-
'MemCStruct',
55-
'CEnum',
56-
'define',
57-
'undef',
58-
'getdef',
59-
'typedef',
60-
'get_type',
61-
'sizeof',
62-
'parse',
49+
"LITTLE_ENDIAN",
50+
"BIG_ENDIAN",
51+
"NATIVE_ORDER",
52+
"CHAR_ZERO",
53+
"CStruct",
54+
"MemCStruct",
55+
"CEnum",
56+
"define",
57+
"undef",
58+
"getdef",
59+
"typedef",
60+
"get_type",
61+
"sizeof",
62+
"parse",
6363
]
6464

6565

@@ -150,14 +150,14 @@ def get_type(type_: str) -> Any:
150150
type_ = TYPEDEFS[type_]
151151
if isinstance(type_, CStructMeta):
152152
return type_
153-
elif type_.startswith('struct ') or type_.startswith('union '):
154-
kind, type_ = type_.split(' ', 1)
153+
elif type_.startswith("struct ") or type_.startswith("union "):
154+
kind, type_ = type_.split(" ", 1)
155155
try:
156156
return STRUCTS[type_]
157157
except KeyError:
158158
raise KeyError(f"Unknown {kind} `{type_}`")
159-
elif type_.startswith('enum '):
160-
kind, type_ = type_.split(' ', 1)
159+
elif type_.startswith("enum "):
160+
kind, type_ = type_.split(" ", 1)
161161
try:
162162
return ENUMS[type_]
163163
except KeyError:
@@ -222,4 +222,4 @@ def parse(
222222
cls_def = parse_struct_def(__struct__, __cls__=__cls__, process_muliple_definition=True, **kargs)
223223
if cls_def is None:
224224
return None
225-
return cls_def['__cls__'].parse(cls_def, **kargs)
225+
return cls_def["__cls__"].parse(cls_def, **kargs)

cstruct/abstract.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,27 @@
3737
from .native_types import get_native_type
3838
from .exceptions import CStructException, ParserError
3939

40-
__all__ = ['CStructMeta', 'AbstractCStruct', 'CEnumMeta', 'AbstractCEnum']
40+
__all__ = ["CStructMeta", "AbstractCStruct", "CEnumMeta", "AbstractCEnum"]
4141

4242

4343
class CStructMeta(ABCMeta):
4444
__size__: int = 0
4545

4646
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
4949
# 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)):
5252
namespace.update(parse_struct(**namespace))
5353
__struct__ = True
54-
if '__def__' in namespace:
54+
if "__def__" in namespace:
5555
namespace.update(parse_struct_def(**namespace))
5656
__struct__ = True
5757
# Create the new class
5858
new_class: Type[Any] = super().__new__(metacls, name, bases, namespace)
5959
# 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__"):
6161
STRUCTS[name] = new_class
6262
return new_class
6363

@@ -128,23 +128,23 @@ def parse(
128128
"""
129129
cls_kargs: Dict[str, Any] = dict(kargs)
130130
if __byte_order__ is not None:
131-
cls_kargs['__byte_order__'] = __byte_order__
131+
cls_kargs["__byte_order__"] = __byte_order__
132132
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__
135135
if isinstance(__struct__, (str, Tokens)):
136-
del cls_kargs['__struct__']
136+
del cls_kargs["__struct__"]
137137
cls_kargs.update(parse_struct_def(__struct__, __cls__=cls, **cls_kargs))
138-
cls_kargs['__struct__'] = None
138+
cls_kargs["__struct__"] = None
139139
elif isinstance(__struct__, dict):
140-
del cls_kargs['__struct__']
140+
del cls_kargs["__struct__"]
141141
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__
144144
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__
148148
return type(__name__, (cls,), cls_kargs)
149149

150150
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
173173
flexible_array_length: flexible array length
174174
"""
175175
self.set_flexible_array_length(flexible_array_length)
176-
if hasattr(buffer, 'read'):
176+
if hasattr(buffer, "read"):
177177
buffer = buffer.read(self.size) # type: ignore
178178
if not buffer:
179179
return False
@@ -237,19 +237,19 @@ def inspect(self, start_addr: Optional[int] = None, end_addr: Optional[int] = No
237237
end_addr: end address
238238
"""
239239
buffer = StringIO()
240-
if hasattr(self, '__mem__'):
240+
if hasattr(self, "__mem__"):
241241
mem = self.__mem__
242242
else:
243243
mem = self.pack()
244244
for i in range(start_addr or 0, end_addr or self.size, 16):
245245
row = mem[i : i + 16]
246246
buffer.write(f"{i:08x} ")
247247
for j, c in enumerate(row):
248-
separator = ' ' if j == 7 else ''
248+
separator = " " if j == 7 else ""
249249
buffer.write(f" {c:02x}{separator}")
250250
buffer.write(" |")
251251
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 ".")
253253
buffer.write("|")
254254
buffer.write("\n")
255255
buffer.seek(0, 0)
@@ -368,16 +368,16 @@ def parse(
368368
"""
369369
cls_kargs: Dict[str, Any] = dict(kargs)
370370
if __size__ is not None:
371-
cls_kargs['__size__'] = __size__
371+
cls_kargs["__size__"] = __size__
372372
if __native_format__ is not None:
373-
cls_kargs['__native_format__'] = __native_format__
373+
cls_kargs["__native_format__"] = __native_format__
374374

375375
if isinstance(__enum__, (str, Tokens)):
376376
cls_kargs.update(parse_enum_def(__enum__, __cls__=cls, **cls_kargs))
377377
elif isinstance(__enum__, dict):
378378
cls_kargs.update(__enum__)
379379

380-
__name__ = cls_kargs.get('__name__') or __name__
380+
__name__ = cls_kargs.get("__name__") or __name__
381381
if __name__ is None:
382382
__name__ = cls.__name__ + "_" + hashlib.sha1(str(__enum__).encode("utf-8")).hexdigest()
383383
cls_kargs["__anonymous__"] = True

cstruct/base.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@
2828
from .abstract import AbstractCStruct, AbstractCEnum
2929

3030
__all__ = [
31-
'LITTLE_ENDIAN',
32-
'BIG_ENDIAN',
33-
'NATIVE_ORDER',
34-
'CHAR_ZERO',
35-
'STRUCTS',
36-
'DEFINES',
37-
'TYPEDEFS',
38-
'CHAR_ZERO',
39-
'DEFAULT_ENUM_SIZE',
31+
"LITTLE_ENDIAN",
32+
"BIG_ENDIAN",
33+
"NATIVE_ORDER",
34+
"CHAR_ZERO",
35+
"STRUCTS",
36+
"DEFINES",
37+
"TYPEDEFS",
38+
"CHAR_ZERO",
39+
"DEFAULT_ENUM_SIZE",
4040
]
4141

42-
LITTLE_ENDIAN = '<'
42+
LITTLE_ENDIAN = "<"
4343
"Little-endian, std. size & alignment"
44-
BIG_ENDIAN = '>'
44+
BIG_ENDIAN = ">"
4545
"Big-endian, std. size & alignment"
46-
NATIVE_ORDER = '@'
46+
NATIVE_ORDER = "@"
4747
"Native order, size & alignment"
4848

4949
STRUCTS: Dict[str, Type["AbstractCStruct"]] = {}
@@ -53,23 +53,23 @@
5353
DEFINES: Dict[str, Any] = {}
5454

5555
TYPEDEFS: Dict[str, str] = {
56-
'short int': 'short',
57-
'unsigned short int': 'unsigned short',
58-
'ushort': 'unsigned short',
59-
'long int': 'long',
60-
'unsigned long int': 'unsigned long',
61-
'int8_t': 'int8',
62-
'uint8_t': 'uint8',
63-
'int16_t': 'int16',
64-
'uint16_t': 'uint16',
65-
'int32_t': 'int32',
66-
'uint32_t': 'uint32',
67-
'int64_t': 'int64',
68-
'uint64_t': 'uint64',
56+
"short int": "short",
57+
"unsigned short int": "unsigned short",
58+
"ushort": "unsigned short",
59+
"long int": "long",
60+
"unsigned long int": "unsigned long",
61+
"int8_t": "int8",
62+
"uint8_t": "uint8",
63+
"int16_t": "int16",
64+
"uint16_t": "uint16",
65+
"int32_t": "int32",
66+
"uint32_t": "uint32",
67+
"int64_t": "int64",
68+
"uint64_t": "uint64",
6969
}
7070

71-
ENUM_SIZE_TO_C_TYPE: Dict[int, str] = {1: 'int8', 2: 'int16', 4: 'int32', 8: 'int64'}
71+
ENUM_SIZE_TO_C_TYPE: Dict[int, str] = {1: "int8", 2: "int16", 4: "int32", 8: "int64"}
7272

73-
CHAR_ZERO = bytes('\0', 'ascii')
73+
CHAR_ZERO = bytes("\0", "ascii")
7474

7575
DEFAULT_ENUM_SIZE = 4

cstruct/c_expr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
if TYPE_CHECKING:
3232
from .abstract import AbstractCStruct
3333

34-
__all__ = ['c_eval']
34+
__all__ = ["c_eval"]
3535

3636

3737
def c_eval(expr: str) -> Union[int, float]:

0 commit comments

Comments
 (0)