query: Add ContextMeta function.
Deduplicate metadata lookup code.
This commit is contained in:
parent
ef03893bfe
commit
f0a5116429
1 changed files with 11 additions and 10 deletions
|
@ -108,7 +108,6 @@ from .. import data
|
|||
from .. import rtutil
|
||||
|
||||
PROGNAME = 'query-report'
|
||||
SENTINEL = object()
|
||||
logger = logging.getLogger('conservancy_beancount.reports.query')
|
||||
|
||||
CellFunc = Callable[[Any], odf.table.TableCell]
|
||||
|
@ -154,6 +153,14 @@ class PostingContext:
|
|||
store: Store
|
||||
|
||||
|
||||
def ContextMeta(context: PostingContext) -> data.PostingMeta:
|
||||
"""Build a read-only PostingMeta object from the query context"""
|
||||
# We use sys.maxsize as the index because using a constant is fast, and
|
||||
# that helps keep the object read-only: if it ever tries to manipulate
|
||||
# the transaction, it'll get an IndexError.
|
||||
return data.PostingMeta(context.entry, sys.maxsize, context.posting).detached()
|
||||
|
||||
|
||||
class MetaDocs(bc_query_env.AnyMeta):
|
||||
"""Return a list of document links from metadata."""
|
||||
def __init__(self, operands: List[bc_query_compile.EvalNode]) -> None:
|
||||
|
@ -266,16 +273,10 @@ class RTTicket(bc_query_compile.EvalFunction):
|
|||
rt_key, meta_key, limit = self.eval_args(context)
|
||||
rt_field = self._rt_key(rt_key)
|
||||
meta_key = self._meta_key(meta_key)
|
||||
if context.posting.meta is None:
|
||||
meta_value: Any = SENTINEL
|
||||
else:
|
||||
meta_value = context.posting.meta.get(meta_key, SENTINEL)
|
||||
if meta_value is SENTINEL:
|
||||
meta_value = context.entry.meta.get(meta_key)
|
||||
if not isinstance(meta_value, str) or limit < 1:
|
||||
meta_value = ''
|
||||
if limit < 1:
|
||||
return set()
|
||||
ticket_ids: Set[str] = set()
|
||||
for link_s in meta_value.split():
|
||||
for link_s in ContextMeta(context).report_links(meta_key):
|
||||
rt_id = rtutil.RT.parse(link_s)
|
||||
if rt_id is not None:
|
||||
ticket_ids.add(rt_id[0])
|
||||
|
|
Loading…
Reference in a new issue