Add partial cache support to inject-links-pid #6

Open
msz wants to merge 1 commit from msz/query-rse-group:more-cache-and-tweaks into main

View file

@ -28,6 +28,7 @@ from query_rse_group import (
@click.option('--api-url', **stdargs['--api-url'])
@click.option('--token', **stdargs['--token'])
@click.option('--record-cache', **stdargs['--record-cache'])
@click.option('--only-from-cache', is_flag=True, help='Use only cached records, make no API requests.')
@click.option(
'-c', '--collection',
multiple=True,
@ -42,6 +43,7 @@ def cli(
api_url: str,
token: str,
record_cache: dict,
only_from_cache: bool,
collection=('public',),
):
"""Inject records that link an object via a given property
@ -57,6 +59,7 @@ def cli(
col,
rec['pid'],
token=token,
cache=record_cache if only_from_cache else None
))
target = rec.get(inject)
if target is None:
@ -79,12 +82,20 @@ def get_any_thing_that_links_pid_via_x(
collection: str,
pid: str,
token: str | None = None,
cache: dict | None = None,
):
def _pid_match(val: str | dict, pid: str, via: str):
if isinstance(val, dict):
val = val['object']
return pid == val
def _cn_match(record: dict, cn: str | None):
if cn is None:
return True
else:
record_cn = record.get('schema_type', '').rsplit(':', maxsplit=1)[-1]
return record_cn == cn
def _does_link(
rec: dict,
target_pid: str,
@ -103,6 +114,15 @@ def get_any_thing_that_links_pid_via_x(
for val in prop
)
if cache is not None:
return [
record
for record in cache.values()
if record is not None
and _cn_match(record, class_name)
and any(_does_link(record, pid, prop_id) for prop_id in link)
]
return [
res[0]
for res in collection_read_records_of_class(