From: Plom Heller Date: Thu, 30 Apr 2026 01:31:47 +0000 (+0200) Subject: Collect data files location from environment BRICKSPLOM_DIR=. X-Git-Url: https://plomlompom.com/repos/booking/%22https:/validator.w3.org/%7B%7Bprefix%7D%7D?a=commitdiff_plain;h=9054fa531e0f488b6aaaad10419787ee1f516694;p=bricksplom Collect data files location from environment BRICKSPLOM_DIR=. --- diff --git a/bricksplom.py b/bricksplom.py index e0c395e..dafe491 100755 --- a/bricksplom.py +++ b/bricksplom.py @@ -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)