home · contact · privacy
Collect data files location from environment BRICKSPLOM_DIR=.
authorPlom Heller <plom@plomlompom.com>
Thu, 30 Apr 2026 01:31:47 +0000 (03:31 +0200)
committerPlom Heller <plom@plomlompom.com>
Thu, 30 Apr 2026 01:31:47 +0000 (03:31 +0200)
bricksplom.py

index e0c395ebf7b6af5241266f0696ee2cf4d7ee23e0..dafe49104495fbb8613cde7da622878dbde5727b 100755 (executable)
@@ -1,9 +1,11 @@
 #!/usr/bin/env python3
 'Data structures for managing/sorting bricks of a certain kind.'
 from abc import ABC, abstractmethod
+from os import environ
 from pathlib import Path
 from typing import Callable, Optional, Self
 
+NAME_ENV_DIRNAME = 'BRICKSPLOM_DIR'
 PATH_COLLECTIONS = 'collections.txt'
 PATH_COLORS = 'colors.txt'
 PATH_DESIGNS = 'designs.txt'
@@ -36,12 +38,13 @@ class Textfiled(ABC):
 
     @staticmethod
     def lines_of(
-            path: str
+            path: tuple[str, str]
             ) -> tuple[str, ...]:
         'Non-empty right-stripped lines of file at path.'
+        joined_path = Path(path[0]).joinpath(path[1])
         return tuple(line.rstrip()
-                     for line in Path(path).read_text(encoding='utf8'
-                                                      ).split(CHAR_NEWLINE)
+                     for line in joined_path.read_text(encoding='utf8'
+                                                       ).split(CHAR_NEWLINE)
                      if line.strip() and not line.startswith(CHAR_COMMENT))
 
     @staticmethod
@@ -65,7 +68,7 @@ class Textfiled(ABC):
     @abstractmethod
     def from_textfile(
             cls,
-            path: str
+            path: tuple[str, str]
             ) -> dict[str, Self]:
         'Build from file at path.'
 
@@ -86,7 +89,7 @@ class Color(Textfiled):
     @classmethod
     def from_textfile(
             cls,
-            path: str
+            path: tuple[str, str]
             ) -> dict[str, Self]:
         collected = {}
         for id_, desc in [cls.tokify(line, 2) for line in cls.lines_of(path)]:
@@ -117,7 +120,7 @@ class Design(Textfiled):
     @classmethod
     def from_textfile(
             cls,
-            path: str
+            path: tuple[str, str]
             ) -> dict[str, Self]:
         collected = {}
         alts: dict[str, set[str]] = {}
@@ -174,7 +177,7 @@ class Piece(Textfiled):
     @classmethod
     def from_textfile(
             cls,
-            path: str
+            path: tuple[str, str]
             ) -> dict[str, Self]:
         collected = {}
         for toks in [cls.tokify(line, 3) for line in cls.lines_of(path)]:
@@ -209,7 +212,7 @@ class Collection(Textfiled):
     @classmethod
     def from_textfile(
             cls,
-            path: str
+            path: tuple[str, str]
             ) -> dict[str, Self]:
         collected: dict[str, tuple[Optional[bool],
                                    str,
@@ -432,10 +435,11 @@ def check_consistencies_between_tables(
 
 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)
+    dir_tables = environ.get(NAME_ENV_DIRNAME, '.')
+    colors = Color.from_textfile((dir_tables, PATH_COLORS))
+    pieces = Piece.from_textfile((dir_tables, PATH_PIECES))
+    collections = Collection.from_textfile((dir_tables, PATH_COLLECTIONS))
+    designs = Design.from_textfile((dir_tables, PATH_DESIGNS))
     boxes = Box.from_collections(collections, pieces, designs)
     check_consistencies_between_tables(colors, pieces, collections, designs,
                                        boxes)