From 5857a95ca0f1a795da623073db12d60ed4cf48fc Mon Sep 17 00:00:00 2001 From: Plom Heller Date: Wed, 22 Apr 2026 07:09:18 +0200 Subject: [PATCH] Add colors. --- bricksplom.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/bricksplom.py b/bricksplom.py index b42fb61..b5d4bdf 100755 --- a/bricksplom.py +++ b/bricksplom.py @@ -6,11 +6,14 @@ from typing import Optional, Self PATH_BOXES = 'boxes.txt' PATH_COLLECTIONS = 'collections.txt' +PATH_COLORS = 'colors.txt' PATH_DESIGNS = 'designs.txt' PATH_PIECES = 'pieces.txt' CHAR_NEWLINE = '\n' CHAR_SPACE = ' ' +CHAR_PLUS = '+' +CHAR_MINUS = '-' CHAR_COMMA = ',' CHAR_EQ = '=' CHAR_UNDER = '_' @@ -62,6 +65,38 @@ class Textfiled(ABC): 'Build from file at path.' +class Color(Textfiled): + 'Color incl. solidness/transparency field.' + + def __init__( + self, + id_: str, + solid: bool, + wavelength: str + ) -> None: + self.id_ = id_ + self.solid = solid + self.wavelength = wavelength + + @classmethod + def from_textfile( + cls, + path: str + ) -> dict[str, Self]: + collected = {} + for id_, desc in [cls.tokify(line, 2) for line in cls.lines_of(path)]: + assert len(desc) > 1 + assert desc[0] in {CHAR_PLUS, CHAR_MINUS} + collected[id_] = cls(id_, desc[0] == CHAR_PLUS, desc[1:]) + return collected + + def __str__( + self + ) -> str: + return (f'{self.id_:>3} ' + f'{CHAR_PLUS if self.solid else CHAR_MINUS}{self.wavelength}') + + class Design(Textfiled): 'Shape and texture configurations with descriptions and equalities.' @@ -219,19 +254,22 @@ class Collection(Textfiled): self, pieces: dict[str, 'Piece'], designs: dict[str, 'Design'], - boxes: dict[str, 'Box'] + boxes: dict[str, 'Box'], + colors: dict[str, 'Color'] ) -> None: 'Print helper for collecting from boxes all pieces of self.' lines = [] for count, piece_id, _ in self.piece_listings_flat(): piece = pieces[piece_id] + color = str(colors[piece.color_id]).split(CHAR_SPACE, + maxsplit=1)[1] design = Design.possibly_by_alt(piece.design_id, designs) assert design is not None description = design.description box = [b for b in boxes.values() if design.id_ in b.designs][0] idx_in_box = box.designs.index(design.id_) lines += [f'{box.id_:>2}:{idx_in_box:>2} ' - f'{count}× {piece.color_id:>3} {description}'] + f'{count}× {color} {description}'] for line in sorted(lines): print(line) @@ -263,6 +301,7 @@ class Box(Textfiled): def check_consistencies_between_tables( + colors: dict[str, Color], pieces: dict[str, Piece], collections: dict[str, Collection], designs: dict[str, Design], @@ -301,15 +340,22 @@ def check_consistencies_between_tables( break assert pieces_found, design_id + # check all pieces' colors are recorded + for color_id in [v.color_id for v in pieces.values()]: + assert color_id in colors, color_id + def main( ) -> None: + colors = Color.from_textfile(PATH_COLORS) pieces = Piece.from_textfile(PATH_PIECES) collections = Collection.from_textfile(PATH_COLLECTIONS) designs = Design.from_textfile(PATH_DESIGNS) boxes = Box.from_textfile(PATH_BOXES) - check_consistencies_between_tables(pieces, collections, designs, boxes) - for title, items in (('PIECES', pieces), + check_consistencies_between_tables(colors, pieces, collections, designs, + boxes) + for title, items in (('COLORS', colors), + ('PIECES', pieces), ('DESIGNS', designs), ('BOXES', boxes), ('COLLECTIONS', collections)): -- 2.30.2