home · contact · privacy
Add colors. master
authorPlom Heller <plom@plomlompom.com>
Wed, 22 Apr 2026 05:09:18 +0000 (07:09 +0200)
committerPlom Heller <plom@plomlompom.com>
Wed, 22 Apr 2026 05:09:18 +0000 (07:09 +0200)
bricksplom.py

index b42fb6124bc7f624c0ac944269d2878448fdf778..b5d4bdf0b947a68532695fde7766a5e18264d2e4 100755 (executable)
@@ -6,11 +6,14 @@ from typing import Optional, Self
 
 PATH_BOXES = 'boxes.txt'
 PATH_COLLECTIONS = 'collections.txt'
 
 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 = ' '
 PATH_DESIGNS = 'designs.txt'
 PATH_PIECES = 'pieces.txt'
 
 CHAR_NEWLINE = '\n'
 CHAR_SPACE = ' '
+CHAR_PLUS = '+'
+CHAR_MINUS = '-'
 CHAR_COMMA = ','
 CHAR_EQ = '='
 CHAR_UNDER = '_'
 CHAR_COMMA = ','
 CHAR_EQ = '='
 CHAR_UNDER = '_'
@@ -62,6 +65,38 @@ class Textfiled(ABC):
         'Build from file at path.'
 
 
         '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.'
 
 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'],
             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]
             ) -> 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} '
             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)
 
         for line in sorted(lines):
             print(line)
 
@@ -263,6 +301,7 @@ class Box(Textfiled):
 
 
 def check_consistencies_between_tables(
 
 
 def check_consistencies_between_tables(
+        colors: dict[str, Color],
         pieces: dict[str, Piece],
         collections: dict[str, Collection],
         designs: dict[str, Design],
         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
 
             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:
 
 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)
     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)):
                          ('DESIGNS', designs),
                          ('BOXES', boxes),
                          ('COLLECTIONS', collections)):