From: Plom Heller Date: Wed, 10 Jun 2026 20:26:00 +0000 (+0200) Subject: Improve design db file syntax. X-Git-Url: https://plomlompom.com/repos/booking/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/edit?a=commitdiff_plain;ds=sidebyside;p=bricksplom Improve design db file syntax. --- diff --git a/bricksplom.py b/bricksplom.py index a8fe53c..914285b 100755 --- a/bricksplom.py +++ b/bricksplom.py @@ -16,7 +16,9 @@ CHAR_NEWLINE = '\n' CHAR_SEP_TOKEN = ' ' CHAR_COMMENT = '#' CHAR_DESIGN_ALT = '=' -CHAR_DESIGN_DESC = '_' +CHAR_ATTR_EQ = '=' +SEP_DESIGN_DESC = ' #' +SEP_DESIGN_ATTR = ' |' CHAR_COLL_INDENT = ' ' CHAR_COLL_IN = '+' CHAR_COLL_OUT = '-' @@ -296,16 +298,21 @@ class BrickDesign(Textfiled, Lookupable): for line in cls.lines_of(path)]: assert design_id not in collected, design_id assert len(body) > 1 - char_type, body = body[0], body[1:] - assert char_type in {CHAR_DESIGN_ALT, CHAR_DESIGN_DESC} - if char_type == CHAR_DESIGN_ALT: - alts[body] = alts.get(body, set()) - alts[body].add(design_id) + if body[0] == CHAR_DESIGN_ALT: + alt_id = body[1:] + alts[alt_id] = alts.get(alt_id, set()) + alts[alt_id].add(design_id) collected[design_id] = cls(design_id) else: - n_studs_str, desc = cls.tokify(body, 2) - n_studs = int(n_studs_str) if n_studs_str.isdigit() else -1 - collected[design_id] = cls(design_id, n_studs, desc) + assert SEP_DESIGN_DESC in body + metadata, desc = body.split(SEP_DESIGN_DESC, maxsplit=1) + kwargs: dict[str, str | int] = {'description': desc} + for attr in metadata.split(SEP_DESIGN_ATTR): + assert CHAR_ATTR_EQ in attr + a_key, a_val_str = attr.split(CHAR_ATTR_EQ, maxsplit=1) + assert a_val_str.isdigit() + kwargs[a_key] = int(a_val_str) + collected[design_id] = cls(design_id, **kwargs) for id_, alternate_ids in alts.items(): collected[id_].alternate_ids = alternate_ids for alt_id in alternate_ids: @@ -315,11 +322,12 @@ class BrickDesign(Textfiled, Lookupable): def raw( self ) -> str: - return (f'{self.id_indented()} ' - + (f'={self.alternate_to.id_}' if self.alternate_to - else (CHAR_DESIGN_DESC - + ('?' if self._n_studs < 0 else str(self._n_studs)) - + f' {self._description}'))) + return ( + f'{self.id_indented()} ' + + (f'{CHAR_DESIGN_ALT}{self.alternate_to.id_}' if self.alternate_to + else (('' if self._n_studs < 0 + else f'n_studs{CHAR_ATTR_EQ}{self._n_studs}') + + f'{SEP_DESIGN_DESC}{self._description}'))) class Brick(Textfiled, WithDb, Lookupable):