From d7ff80f2f459bb93e15e4f019814cd25149db5c5 Mon Sep 17 00:00:00 2001 From: Plom Heller Date: Sun, 4 Oct 2026 19:45:32 +0200 Subject: [PATCH] =?utf8?q?Depend=20=5FTextfiled=20on=20=5FLookupable=20for?= =?utf8?q?=20its=20new=20cls(db=3D=E2=80=A6)=20requirement.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/bricksplom/misc.py | 100 ++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/src/bricksplom/misc.py b/src/bricksplom/misc.py index 61a1d9d..0091b07 100644 --- a/src/bricksplom/misc.py +++ b/src/bricksplom/misc.py @@ -55,56 +55,6 @@ _PageColumn = tuple[_BrickListing, ...] _Page = tuple[_PageColumn, ...] -class _Textfiled(ABC): - 'Table to be read from textfile, and compatible output in .raw().' - - @staticmethod - def lines_of( - 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 joined_path.read_text(encoding='utf8' - ).split(CHAR_NEWLINE) - if line.strip() and not line.startswith(_CHAR_COMMENT)) - - @staticmethod - def tokify( - body: str, - len_expected: int - ) -> tuple[str, ...]: - 'Body parsed into left-stripped tokens of len_expected count.' - collected: list[str] = [] - while len(collected) < len_expected: - body = body.lstrip() - if len(collected) == len_expected - 1: - tok = body - else: - assert _CHAR_SEP_TOKEN in body, body - tok, body = body.split(_CHAR_SEP_TOKEN, maxsplit=1) - collected += [tok] - return tuple(collected) - - @classmethod - @abstractmethod - def from_textfile( - cls, - path: tuple[str, str], - db: 'BricksDb' - ) -> dict[str, Self]: - 'Build from file at path.' - - @abstractmethod - def raw(self) -> str: - 'Output in format used for reading in.' - - def __str__( - self - ) -> str: - return self.raw() - - class _Lookupable: 'Adds .db, provides methods for BricksDb.lookup and .id_ padding.' _id_indent: int = 0 @@ -224,6 +174,56 @@ class _Lookupable: } +class _Textfiled(ABC, _Lookupable): + 'Table to be read from textfile, and compatible output in .raw().' + + @staticmethod + def lines_of( + 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 joined_path.read_text(encoding='utf8' + ).split(CHAR_NEWLINE) + if line.strip() and not line.startswith(_CHAR_COMMENT)) + + @staticmethod + def tokify( + body: str, + len_expected: int + ) -> tuple[str, ...]: + 'Body parsed into left-stripped tokens of len_expected count.' + collected: list[str] = [] + while len(collected) < len_expected: + body = body.lstrip() + if len(collected) == len_expected - 1: + tok = body + else: + assert _CHAR_SEP_TOKEN in body, body + tok, body = body.split(_CHAR_SEP_TOKEN, maxsplit=1) + collected += [tok] + return tuple(collected) + + @classmethod + @abstractmethod + def from_textfile( + cls, + path: tuple[str, str], + db: 'BricksDb' + ) -> dict[str, Self]: + 'Build from file at path.' + + @abstractmethod + def raw(self) -> str: + 'Output in format used for reading in.' + + def __str__( + self + ) -> str: + return self.raw() + + class _BrickColor(_Textfiled, _Lookupable): 'Color incl. solidness/transparency field.' _id_indent = 3 -- 2.30.2