Watch
1
0
Fork
You've already forked query-rse-group
0
query-rse-group/query_things/list_records.py
Michael Hanke 48e67f022b
rf(list): extract record listing into core function
Move the list command logic into the list_records() function in a new
query_things/list_records module, with a thin click wrapper in
query_things/cli. This replaces the old list.py module.
2026-08-31 20:14:38 +02:00

32 lines
961 B
Python

from collections.abc import Iterator
from query_things.common import RecordCache
def list_records(
cache: RecordCache,
*,
pid: tuple[str, ...] = (),
cls: tuple[str, ...] = (),
) -> Iterator[dict]:
"""Yield records from `cache`, optionally constrained by PIDs or classes.
When PIDs are given, they take precedence over classes. A KeyError is
raised if any of the given PIDs are not present in the cache; nothing
is yielded in that case. Otherwise all cached records are yielded,
constrained to the given classes via `schema_type`, if any are given.
"""
if pid:
missing = [p for p in pid if cache.get(p) is None]
if missing:
raise KeyError(missing)
for p in pid:
yield cache.get(p)
return
for rec in cache.values():
if rec is None:
continue
if cls and rec.get('schema_type') not in cls:
continue
yield rec