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
 | 
					from .. import rtutil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
PROGNAME = 'query-report'
 | 
					PROGNAME = 'query-report'
 | 
				
			||||||
SENTINEL = object()
 | 
					 | 
				
			||||||
logger = logging.getLogger('conservancy_beancount.reports.query')
 | 
					logger = logging.getLogger('conservancy_beancount.reports.query')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
CellFunc = Callable[[Any], odf.table.TableCell]
 | 
					CellFunc = Callable[[Any], odf.table.TableCell]
 | 
				
			||||||
| 
						 | 
					@ -154,6 +153,14 @@ class PostingContext:
 | 
				
			||||||
    store: Store
 | 
					    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):
 | 
					class MetaDocs(bc_query_env.AnyMeta):
 | 
				
			||||||
    """Return a list of document links from metadata."""
 | 
					    """Return a list of document links from metadata."""
 | 
				
			||||||
    def __init__(self, operands: List[bc_query_compile.EvalNode]) -> None:
 | 
					    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_key, meta_key, limit = self.eval_args(context)
 | 
				
			||||||
        rt_field = self._rt_key(rt_key)
 | 
					        rt_field = self._rt_key(rt_key)
 | 
				
			||||||
        meta_key = self._meta_key(meta_key)
 | 
					        meta_key = self._meta_key(meta_key)
 | 
				
			||||||
        if context.posting.meta is None:
 | 
					        if limit < 1:
 | 
				
			||||||
            meta_value: Any = SENTINEL
 | 
					            return set()
 | 
				
			||||||
        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 = ''
 | 
					 | 
				
			||||||
        ticket_ids: Set[str] = 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)
 | 
					            rt_id = rtutil.RT.parse(link_s)
 | 
				
			||||||
            if rt_id is not None:
 | 
					            if rt_id is not None:
 | 
				
			||||||
                ticket_ids.add(rt_id[0])
 | 
					                ticket_ids.add(rt_id[0])
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue