From f1b3ab5e7223d76634878e1231ae0906b5bdbb36 Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 18 Jul 2024 11:35:25 +0200 Subject: kicad: Improve compatibility with old symbol files --- gerbonara/cad/kicad/base_types.py | 2 +- gerbonara/cad/kicad/sexp_mapper.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'gerbonara/cad') diff --git a/gerbonara/cad/kicad/base_types.py b/gerbonara/cad/kicad/base_types.py index a4eba70..c5c04e9 100644 --- a/gerbonara/cad/kicad/base_types.py +++ b/gerbonara/cad/kicad/base_types.py @@ -345,7 +345,7 @@ class Justify: @sexp_type('effects') class TextEffect: font: FontSpec = field(default_factory=FontSpec) - hide: OmitDefault(Named(YesNoAtom())) = False + hide: OmitDefault(Named(LegacyCompatibleFlag())) = False justify: OmitDefault(Justify) = field(default_factory=Justify) diff --git a/gerbonara/cad/kicad/sexp_mapper.py b/gerbonara/cad/kicad/sexp_mapper.py index d2d9d30..c197765 100644 --- a/gerbonara/cad/kicad/sexp_mapper.py +++ b/gerbonara/cad/kicad/sexp_mapper.py @@ -202,6 +202,24 @@ class YesNoAtom: yield self.yes if value else self.no +class LegacyCompatibleFlag: + '''Variant of YesNoAtom that accepts both the `(flag )` variant and the bare `flag` variant for compatibility.''' + + def __init__(self, yes=Atom.yes, no=Atom.no, value_when_empty=True): + self.yes, self.no = yes, no + self.value_when_empty = value_when_empty + + def __map__(self, value, parent=None): + if value == []: + return self.value_when_empty + + value, = value + return value == self.yes + + def __sexp__(self, value): + yield self.yes if value else self.no + + class Wrap(WrapperType): def __map__(self, value, parent=None): value, = value -- cgit