From 26c2460490b6e64790c94e00be848465a6a5fa96 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sat, 29 Apr 2023 17:25:32 +0200 Subject: Fix remaining unit tests --- gerbonara/aperture_macros/parse.py | 19 +++++++++++-------- gerbonara/aperture_macros/primitive.py | 12 ++++++------ 2 files changed, 17 insertions(+), 14 deletions(-) (limited to 'gerbonara/aperture_macros') diff --git a/gerbonara/aperture_macros/parse.py b/gerbonara/aperture_macros/parse.py index 5dda931..d38a83d 100644 --- a/gerbonara/aperture_macros/parse.py +++ b/gerbonara/aperture_macros/parse.py @@ -49,18 +49,21 @@ def _parse_expression(expr): @dataclass(frozen=True, slots=True) class ApertureMacro: - name: str = None + name: str = field(default=None, hash=False, compare=False) primitives: tuple = () variables: tuple = () - comments: tuple = () + comments: tuple = field(default=(), hash=False, compare=False) def __post_init__(self): if self.name is None or re.match(r'GNX[0-9A-F]{16}', self.name): # We can't use field(default_factory=...) here because that factory doesn't get a reference to the instance. - object.__setattr__(self, 'name', f'GNX{hash(self)&0xffffffffffffffff:016X}') + self._reset_name() + + def _reset_name(self): + object.__setattr__(self, 'name', f'GNX{hash(self)&0xffffffffffffffff:016X}') @classmethod - def parse_macro(cls, name, body, unit): + def parse_macro(kls, name, body, unit): comments = [] variables = {} primitives = [] @@ -86,11 +89,10 @@ class ApertureMacro: else: # primitive primitive, *args = block.split(',') args = [ _parse_expression(arg) for arg in args ] - primitive = ap.PRIMITIVE_CLASSES[int(primitive)](unit=unit, args=args) - primitives.append(primitive) + primitives.append(ap.PRIMITIVE_CLASSES[int(primitive)].from_arglist(unit, args)) - variables = [variables.get(i+1) for i in range(max(variables.keys()))] - return kls(name, tuple(primitives), tuple(variables), tuple(primitives)) + variables = [variables.get(i+1) for i in range(max(variables.keys(), default=0))] + return kls(name, tuple(primitives), tuple(variables), tuple(comments)) def __str__(self): return f'' @@ -110,6 +112,7 @@ class ApertureMacro: return replace(self, primitives=tuple(new_primitives)) def to_gerber(self, unit=None): + """ Serialize this macro's content (without the name) into Gerber using the given file unit """ comments = [ str(c) for c in self.comments ] variable_defs = [ f'${var}={str(expr)[1:-1]}' for var, expr in enumerate(self.variables, start=1) if expr is not None ] primitive_defs = [ prim.to_gerber(unit) for prim in self.primitives ] diff --git a/gerbonara/aperture_macros/primitive.py b/gerbonara/aperture_macros/primitive.py index a12a33c..f575b0c 100644 --- a/gerbonara/aperture_macros/primitive.py +++ b/gerbonara/aperture_macros/primitive.py @@ -58,8 +58,8 @@ class Primitive: return str(self) @classmethod - def from_arglist(kls, arglist): - return kls(*arglist) + def from_arglist(kls, unit, arglist): + return kls(unit, *arglist) class Calculator: def __init__(self, instance, variable_binding={}, unit=None): @@ -267,11 +267,11 @@ class Outline(Primitive): yield x, y @classmethod - def from_arglist(kls, arglist): - if len(arglist[3:]) % 2 == 0: - return kls(unit=arglist[0], exposure=arglist[1], length=arglist[2], coords=arglist[3:], rotation=0) + def from_arglist(kls, unit, arglist): + if len(arglist[2:]) % 2 == 0: + return kls(unit=unit, exposure=arglist[0], length=arglist[1], coords=arglist[2:], rotation=0) else: - return kls(unit=arglist[0], exposure=arglist[1], length=arglist[2], coords=arglist[3:-1], rotation=arglist[-1]) + return kls(unit=unit, exposure=arglist[0], length=arglist[1], coords=arglist[2:-1], rotation=arglist[-1]) def __str__(self): return f'' -- cgit