_SubRefs = dict[str, LdrawRefsTree]
+_N_TOKS_PER_REFLINE = 15
+
+
class LdrawDb:
'Connection to local LDRAW archive.'
def _walk_tree(
self,
filename: str,
- collect: Callable[[Callable[[str], Any], str, list[str]], Any],
+ collect: Callable[[Callable[[], Any], str, list[str]], Any],
result: Callable[[Path, list[Any]], Any],
) -> Any:
path = self.path(filename)
collected = []
for line in path.read_text(encoding='utf8').split(CHAR_NEWLINE):
- toks = line.lstrip().split(maxsplit=14)
+ toks = line.lstrip().split(maxsplit=_N_TOKS_PER_REFLINE - 1)
if not (toks and toks[0] == '1'):
continue
- assert len(toks) == 15
+ assert len(toks) == _N_TOKS_PER_REFLINE
+ referenced = toks.pop().replace('\\', '/')
collected += [
- collect(lambda ref: self._walk_tree(ref, collect, result),
- toks.pop().replace('\\', '/'),
+ collect(lambda: self._walk_tree(referenced, collect, result),
+ referenced,
toks)
]
return result(path, collected)
'Produce LdrawRefsTree for filename.'
return self._walk_tree(
filename,
- collect=lambda walker, ref, _: (ref, walker(ref)),
+ collect=lambda walk, ref, _: (ref, walk()),
result=lambda path, coll: (path, {t[0]: t[1] for t in coll}))
def count_studs(
) -> int:
'On item at {._root}/p(art)/{filename} count studs.'
def count_studs(
- walker: Callable[[str], int],
+ walk: Callable[[], int],
ref: str,
toks: list[str]
) -> int:
if jdx != idx and row[jdx] != 0]:
do_ignore = True
break
- return (int((not do_ignore) and ref in LDRAW_STUDS)
- or walker(ref))
+ return int((not do_ignore) and ref in LDRAW_STUDS) or walk()
return self._walk_tree(filename,
collect=count_studs,