improve how defaults are handled and print everything into the XML

This commit is contained in:
Jan Petykiewicz 2024-02-27 22:05:47 -08:00
parent 3e0a4a9da2
commit 06a66605d8

51
k2t.py
View File

@ -7,6 +7,11 @@ from dataclasses import dataclass, fields
from xml.etree import ElementTree from xml.etree import ElementTree
# Used when value is `None`, except on groups where they're left unset
DEFAULT_LINESTYLE = 'I0'
DEFAULT_DITHER = 'I1'
def k2t( def k2t(
kfile: str, kfile: str,
tfile: str, tfile: str,
@ -101,16 +106,16 @@ def t2k(tfile: str, kfile: str) -> None:
@dataclass(slots=True) @dataclass(slots=True)
class Entry: class Entry:
name: str name: str | None = None
source: str | None source: str | None = None
frame_color: str | None frame_color: str | None = None
fill_color: str | None fill_color: str | None = None
members: list | None = None members: list | None = None
expanded: bool = False expanded: bool = False
dither_pattern: str = 'C1' dither_pattern: str | None = None
line_style: str = 'C0' line_style: str | None = None
frame_brightness: int = 0 frame_brightness: int = 0
fill_brightness: int = 0 fill_brightness: int = 0
width: int = 1 width: int = 1
@ -127,11 +132,7 @@ class Entry:
assert el.tag in ('properties', 'group-members') assert el.tag in ('properties', 'group-members')
members = [] members = []
args: dict[str, Any] = { args: dict[str, Any] = {}
'frame_color': None,
'fill_color': None,
'source': None,
}
for child in el: for child in el:
if not child.text: if not child.text:
continue continue
@ -157,6 +158,8 @@ class Entry:
if not members: if not members:
members = None members = None
args.setdefault('line_style', DEFAULT_LINESTYLE)
args.setdefault('dither_pattern', DEFAULT_DITHER)
return cls(members=members, **args) return cls(members=members, **args)
@ -165,22 +168,18 @@ class Entry:
el = ElementTree.Element(main_tag) el = ElementTree.Element(main_tag)
for field in fields(self): for field in fields(self):
val = getattr(self, field.name) val = getattr(self, field.name)
if val is None or field.name == 'members':
continue
if (val == field.default if field.name == 'members':
and (field not in ('dither-pattern', 'line-style')
or self.source is None
)):
continue continue
tag = field.name.replace('_', '-') tag = field.name.replace('_', '-')
if tag in ('frame-color', 'fill-color'): el2 = ElementTree.Element(tag)
val = '#' + val if val is not None:
if not isinstance(val, str): if not isinstance(val, str):
val = str(val).lower() val = str(val).lower()
el2 = ElementTree.Element(tag)
el2.text = val el2.text = val
elif tag == 'source':
el2.text = '*/*@*'
el.append(el2) el.append(el2)
if self.members: if self.members:
@ -195,7 +194,9 @@ class Entry:
s = '[[' + '.'.join(parts) + ']]\n' s = '[[' + '.'.join(parts) + ']]\n'
for field in fields(self): for field in fields(self):
val = getattr(self, field.name) val = getattr(self, field.name)
if val == field.default or val is None: if val == field.default:
continue
if field.name == 'members':
continue continue
if isinstance(val, str): if isinstance(val, str):
@ -219,11 +220,7 @@ class Entry:
@classmethod @classmethod
def from_toml(cls: Type[Self], tree: dict) -> Self: def from_toml(cls: Type[Self], tree: dict) -> Self:
members = [] members = []
args: dict[str, Any] = { args: dict[str, Any] = {}
'frame_color': None,
'fill_color': None,
'source': None,
}
for key, val in tree.items(): for key, val in tree.items():
if key != 'members': if key != 'members':
args[key.replace('-', '_')] = val args[key.replace('-', '_')] = val
@ -234,6 +231,8 @@ class Entry:
if not members: if not members:
members = None members = None
args.setdefault('line_style', DEFAULT_LINESTYLE)
args.setdefault('dither_pattern', DEFAULT_DITHER)
return cls(members=members, **args) return cls(members=members, **args)