#!/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'
@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
@abstractmethod
def from_textfile(
cls,
- path: str
+ path: tuple[str, str]
) -> dict[str, Self]:
'Build from file at path.'
@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)]:
@classmethod
def from_textfile(
cls,
- path: str
+ path: tuple[str, str]
) -> dict[str, Self]:
collected = {}
alts: dict[str, set[str]] = {}
@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)]:
@classmethod
def from_textfile(
cls,
- path: str
+ path: tuple[str, str]
) -> dict[str, Self]:
collected: dict[str, tuple[Optional[bool],
str,
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)