Order norm finder collections by source code
This commit is contained in:
+14
-8
@@ -87,15 +87,16 @@ def catalog() -> list[dict]:
|
||||
|
||||
|
||||
@lru_cache(maxsize=32)
|
||||
def _section_paths(db_key: tuple[str, int, int], base: str, base_type: str) -> tuple[str, ...]:
|
||||
"""Cache the section index; a rebuilt SQLite file gets a new cache key."""
|
||||
def _section_paths(db_key: tuple[str, int, int], base: str, base_type: str) -> tuple[tuple[str, str], ...]:
|
||||
"""Cache section paths and their first norm codes; a rebuilt DB gets a new key."""
|
||||
con = fsnb_ref._connect()
|
||||
if con is None:
|
||||
raise RuntimeError("Нормативная база недоступна")
|
||||
try:
|
||||
return tuple(row[0] for row in con.execute(
|
||||
"SELECT DISTINCT section_path FROM works WHERE base=? AND base_type=? "
|
||||
"AND section_path IS NOT NULL AND section_path != '' ORDER BY section_path",
|
||||
return tuple((row[0], row[1]) for row in con.execute(
|
||||
"SELECT section_path, MIN(code) FROM works WHERE base=? AND base_type=? "
|
||||
"AND section_path IS NOT NULL AND section_path != '' "
|
||||
"GROUP BY section_path ORDER BY section_path",
|
||||
(base, base_type)))
|
||||
finally:
|
||||
con.close()
|
||||
@@ -117,10 +118,15 @@ def browse(base: str, base_type: str, path: str = "", limit: int = 40,
|
||||
raise RuntimeError("Нормативная база недоступна") from exc
|
||||
paths = _section_paths((str(db), stat.st_mtime_ns, stat.st_size), BASES[base], base_type)
|
||||
prefix = path + " > " if path else ""
|
||||
if path and not any(p == path or p.startswith(prefix) for p in paths):
|
||||
if path and not any(p == path or p.startswith(prefix) for p, _ in paths):
|
||||
raise ValueError("Раздел не найден в выбранном сборнике")
|
||||
children = sorted({p[len(prefix):].split(" > ", 1)[0] for p in paths
|
||||
if p.startswith(prefix) and p != path})
|
||||
child_codes = {}
|
||||
for section, code in paths:
|
||||
if section.startswith(prefix) and section != path:
|
||||
name = section[len(prefix):].split(" > ", 1)[0]
|
||||
if name not in child_codes or code < child_codes[name]:
|
||||
child_codes[name] = code
|
||||
children = sorted(child_codes, key=lambda name: (child_codes[name], name))
|
||||
con = fsnb_ref._connect()
|
||||
if con is None:
|
||||
raise RuntimeError("Нормативная база недоступна")
|
||||
|
||||
Reference in New Issue
Block a user