Skip to content

Commit e0a1c93

Browse files
committed
black code style
1 parent 3b5bc77 commit e0a1c93

9 files changed

Lines changed: 35 additions & 42 deletions

File tree

cstruct/abstract.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from enum import IntEnum, EnumMeta, _EnumDict
3333
from unicodedata import name
3434
from .base import STRUCTS, ENUMS, DEFAULT_ENUM_SIZE
35-
from .c_parser import parse_struct, parse_struct_def, parse_enum_def,parse_enum, Tokens
35+
from .c_parser import parse_struct, parse_struct_def, parse_enum_def, parse_enum, Tokens
3636
from .field import calculate_padding, FieldType
3737
from .exceptions import CStructException, CEnumException
3838

@@ -111,7 +111,7 @@ def parse(
111111
__name__: Optional[str] = None,
112112
__byte_order__: Optional[str] = None,
113113
__is_union__: Optional[bool] = False,
114-
**kargs: Dict[str, Any]
114+
**kargs: Dict[str, Any],
115115
) -> Type["AbstractCStruct"]:
116116
"""
117117
Return a new class mapping a C struct/union definition.
@@ -302,6 +302,7 @@ def size(cls) -> int:
302302
"Enum size (in bytes)"
303303
return cls.__size__
304304

305+
305306
class AbstractCEnum(IntEnum, metaclass=CEnumMeta):
306307
"""
307308
Abstract C enum to Python class
@@ -313,7 +314,7 @@ def parse(
313314
__enum__: Union[str, Tokens, Dict[str, Any]],
314315
__name__: Optional[str] = None,
315316
__size__: Optional[int] = None,
316-
**kargs: Dict[str, Any]
317+
**kargs: Dict[str, Any],
317318
) -> Type["AbstractCEnum"]:
318319
"""
319320
Return a new Python Enum class mapping a C enum definition
@@ -341,4 +342,4 @@ def parse(
341342
cls_kargs["__anonymous__"] = True
342343

343344
cls_kargs.update(cls_kargs["__constants__"])
344-
return cls(__name__, cls_kargs)
345+
return cls(__name__, cls_kargs)

cstruct/base.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@
9393
'uint64': 'Q',
9494
}
9595

96-
ENUM_SIZE_TO_C_TYPE: Dict[int, str] = {
97-
1: 'int8',
98-
2: 'int16',
99-
4: 'int32',
100-
8: 'int64'
101-
}
96+
ENUM_SIZE_TO_C_TYPE: Dict[int, str] = {1: 'int8', 2: 'int16', 4: 'int32', 8: 'int64'}
10297

10398
CHAR_ZERO = bytes('\0', 'ascii')
104-
DEFAULT_ENUM_SIZE = 4
99+
DEFAULT_ENUM_SIZE = 4

cstruct/c_parser.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def parse_type(tokens: Tokens, __cls__: Type['AbstractCStruct'], byte_order: Opt
137137
except KeyError:
138138
raise ParserError("Unknow {} {}".format(c_type, tail))
139139
elif c_type.startswith('enum'):
140-
#from .abstract import AbstractCEnum
140+
# from .abstract import AbstractCEnum
141141
from .cenum import CEnum
142142

143143
c_type, tail = c_type.split(' ', 1)
@@ -165,7 +165,7 @@ def parse_struct_def(
165165
__def__: Union[str, Tokens],
166166
__cls__: Type['AbstractCStruct'],
167167
__byte_order__: Optional[str] = None,
168-
**kargs: Any # Type['AbstractCStruct'],
168+
**kargs: Any, # Type['AbstractCStruct'],
169169
) -> Optional[Dict[str, Any]]:
170170
# naive C struct parsing
171171
if isinstance(__def__, Tokens):
@@ -188,10 +188,7 @@ def parse_struct_def(
188188
raise ParserError("{} definition expected".format(vtype))
189189

190190

191-
def parse_enum_def(
192-
__def__: Union[str, Tokens],
193-
**kargs: Any
194-
) -> Optional[Dict[str, Any]]:
191+
def parse_enum_def(__def__: Union[str, Tokens], **kargs: Any) -> Optional[Dict[str, Any]]:
195192
# naive C enum parsing
196193
if isinstance(__def__, Tokens):
197194
tokens = __def__
@@ -213,10 +210,7 @@ def parse_enum_def(
213210
raise ParserError(f"{vtype} definition expected")
214211

215212

216-
def parse_enum(
217-
__enum__: Union[str, Tokens],
218-
**kargs: Any
219-
) -> Optional[Dict[str, Any]]:
213+
def parse_enum(__enum__: Union[str, Tokens], **kargs: Any) -> Optional[Dict[str, Any]]:
220214
constants: Dict[str, int] = OrderedDict()
221215

222216
if isinstance(__enum__, Tokens):
@@ -265,9 +259,7 @@ def parse_enum(
265259
if next_token == "}":
266260
break
267261

268-
result = {
269-
'__constants__': constants
270-
}
262+
result = {'__constants__': constants}
271263
return result
272264

273265

@@ -276,7 +268,7 @@ def parse_struct(
276268
__cls__: Type['AbstractCStruct'],
277269
__is_union__: bool = False,
278270
__byte_order__: Optional[str] = None,
279-
**kargs: Any
271+
**kargs: Any,
280272
) -> Dict[str, Any]:
281273
# naive C struct parsing
282274
__is_union__ = bool(__is_union__)

cstruct/cenum.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .abstract import AbstractCEnum
22

3+
34
class CEnum(AbstractCEnum):
4-
...
5+
...

cstruct/exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
"EvalError",
3030
]
3131

32+
3233
class CEnumException(Exception):
3334
pass
3435

36+
3537
class CStructException(Exception):
3638
pass
3739

tests/test_cenum.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
from cstruct import CEnum
22

3+
34
class Dummy(CEnum):
4-
__enum__ = """
5+
__enum__ = """
56
A,
67
B,
78
C = 2,
89
D = 5 + 7,
910
E = 2
1011
"""
1112

13+
1214
def test_dummy():
13-
assert Dummy.A == 0
14-
assert Dummy.B == 1
15-
assert Dummy.C == 2
16-
assert Dummy.D == 12
17-
assert Dummy.E == 2
15+
assert Dummy.A == 0
16+
assert Dummy.B == 1
17+
assert Dummy.C == 2
18+
assert Dummy.D == 12
19+
assert Dummy.E == 2

tests/test_cstruct.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,5 @@ def test_null_compare():
261261

262262
def test_invalid_inline():
263263
with pytest.raises(ParserError):
264-
cstruct.MemCStruct.parse(
265-
'struct { unsigned char head; unsigned char head; }', __byte_order__=cstruct.LITTLE_ENDIAN
266-
)
264+
cstruct.MemCStruct.parse('struct { unsigned char head; unsigned char head; }', __byte_order__=cstruct.LITTLE_ENDIAN)
267265
assert False

tests/test_define.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,20 @@ class Invalid(cstruct.CStruct):
8787

8888
def test_invalid_define():
8989
with pytest.raises(ParserError):
90-
cstruct.parse("""
90+
cstruct.parse(
91+
"""
9192
#define xxx yyy zzz
92-
""")
93+
"""
94+
)
9395

9496

9597
def test_invalid_struct():
9698
with pytest.raises(ParserError):
97-
cstruct.parse("""
99+
cstruct.parse(
100+
"""
98101
struct {
99102
int a;
100103
int;
101104
}
102-
""")
105+
"""
106+
)

tests/test_memcstruct.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,5 @@ def test_null_compare():
255255

256256
def test_invalid_inline():
257257
with pytest.raises(ParserError):
258-
cstruct.MemCStruct.parse(
259-
'struct { unsigned char head; unsigned char head; }', __byte_order__=cstruct.LITTLE_ENDIAN
260-
)
258+
cstruct.MemCStruct.parse('struct { unsigned char head; unsigned char head; }', __byte_order__=cstruct.LITTLE_ENDIAN)
261259
assert False

0 commit comments

Comments
 (0)