|  | 
|  | 1 | +"""Builder utility functions for arg/kwargs dicts.""" | 
|  | 2 | + | 
|  | 3 | +from typing import Any, Dict | 
|  | 4 | + | 
|  | 5 | +from tornado.escape import json_decode | 
|  | 6 | + | 
|  | 7 | +# local imports | 
|  | 8 | +from file_catalog.mongo import AllKeys | 
|  | 9 | + | 
|  | 10 | + | 
|  | 11 | +def build_limit(kwargs: Dict[str, Any], config: Dict[str, Any]) -> None: | 
|  | 12 | +    """Build the `"limit"` argument.""" | 
|  | 13 | +    if "limit" in kwargs: | 
|  | 14 | +        kwargs["limit"] = int(kwargs["limit"]) | 
|  | 15 | +        if kwargs["limit"] < 1: | 
|  | 16 | +            raise Exception("limit is not positive") | 
|  | 17 | + | 
|  | 18 | +        # check with config | 
|  | 19 | +        if kwargs["limit"] > config["FC_QUERY_FILE_LIST_LIMIT"]: | 
|  | 20 | +            kwargs["limit"] = config["FC_QUERY_FILE_LIST_LIMIT"] | 
|  | 21 | +    else: | 
|  | 22 | +        # if no limit has been defined, set max limit | 
|  | 23 | +        kwargs["limit"] = config["FC_QUERY_FILE_LIST_LIMIT"] | 
|  | 24 | + | 
|  | 25 | + | 
|  | 26 | +def build_start(kwargs: Dict[str, Any]) -> None: | 
|  | 27 | +    """Build the `"start"` argument.""" | 
|  | 28 | +    if "start" in kwargs: | 
|  | 29 | +        kwargs["start"] = int(kwargs["start"]) | 
|  | 30 | +        if kwargs["start"] < 0: | 
|  | 31 | +            raise Exception("start is negative") | 
|  | 32 | + | 
|  | 33 | + | 
|  | 34 | +def build_files_query(kwargs: Dict[str, Any]) -> None: | 
|  | 35 | +    """Build `"query"` dict with formatted/fully-named arguments. | 
|  | 36 | +
 | 
|  | 37 | +    Pop corresponding shortcut-keys from `kwargs`. | 
|  | 38 | +    """ | 
|  | 39 | +    if "query" in kwargs: | 
|  | 40 | +        # keep whatever was already in here, then add to it | 
|  | 41 | +        if isinstance(kwargs["query"], (str, bytes)): | 
|  | 42 | +            query = json_decode(kwargs.pop("query")) | 
|  | 43 | +        else: | 
|  | 44 | +            query = kwargs.pop("query") | 
|  | 45 | +    else: | 
|  | 46 | +        query = {} | 
|  | 47 | + | 
|  | 48 | +    if "locations.archive" not in query: | 
|  | 49 | +        query["locations.archive"] = None | 
|  | 50 | + | 
|  | 51 | +    # shortcut query params | 
|  | 52 | +    if "logical_name" in kwargs: | 
|  | 53 | +        query["logical_name"] = kwargs.pop("logical_name") | 
|  | 54 | +    if "run_number" in kwargs: | 
|  | 55 | +        query["run.run_number"] = kwargs.pop("run_number") | 
|  | 56 | +    if "dataset" in kwargs: | 
|  | 57 | +        query["iceprod.dataset"] = kwargs.pop("dataset") | 
|  | 58 | +    if "event_id" in kwargs: | 
|  | 59 | +        e = kwargs.pop("event_id") | 
|  | 60 | +        query["run.first_event"] = {"$lte": e} | 
|  | 61 | +        query["run.last_event"] = {"$gte": e} | 
|  | 62 | +    if "processing_level" in kwargs: | 
|  | 63 | +        query["processing_level"] = kwargs.pop("processing_level") | 
|  | 64 | +    if "season" in kwargs: | 
|  | 65 | +        query["offline_processing_metadata.season"] = kwargs.pop("season") | 
|  | 66 | + | 
|  | 67 | +    kwargs["query"] = query | 
|  | 68 | + | 
|  | 69 | + | 
|  | 70 | +def build_keys(kwargs: Dict[str, Any]) -> None: | 
|  | 71 | +    """Build `"keys"` list, potentially using `"all-keys"`. | 
|  | 72 | +
 | 
|  | 73 | +    Pop `"all-keys"`. | 
|  | 74 | +    """ | 
|  | 75 | +    use_all_keys = kwargs.pop("all-keys", None) in ["True", "true", 1] | 
|  | 76 | + | 
|  | 77 | +    if use_all_keys: | 
|  | 78 | +        kwargs["keys"] = AllKeys() | 
|  | 79 | +    elif "keys" in kwargs: | 
|  | 80 | +        kwargs["keys"] = kwargs["keys"].split("|") | 
0 commit comments