[docs]defguids_from_dbs(db_paths:Iterable[Path],)->tuple[dict[Path,list[str]],dict[str,Path]]:""" Extract all guids from the supplied database paths. Args: db_paths: Path or str or directory where to search Returns: Tuple of Dictionary mapping paths to lists of guids as strings and Dictionary mapping guids to db paths. """dbdict={}forpindb_paths:conn=Nonetry:conn=connect(str(p))dbdict[p]=get_guids_by_run_spec(conn=conn)except(RuntimeError,DatabaseError)ase:print(e)finally:ifconnisnotNone:conn.close()gc.collect()guiddict={}fordbpath,guidsindbdict.items():guiddict.update({guid:dbpathforguidinguids})returndbdict,guiddict
[docs]defguids_from_dir(basepath:Path|str,)->tuple[dict[Path,list[str]],dict[str,Path]]:""" Recursively find all db files under basepath and extract guids. Args: basepath: Path or str or directory where to search Returns: Tuple of Dictionary mapping paths to lists of guids as strings and Dictionary mapping guids to db paths. """returnguids_from_dbs(Path(basepath).glob("**/*.db"))
[docs]defguids_from_list_str(s:str)->tuple[str,...]|None:""" Get tuple of guids from a python/json string representation of a list. Extracts the guids from a string representation of a list, tuple, or set of guids or a single guid. Args: s: input string Returns: Extracted guids as a tuple of strings. If a provided string does not match the format, `None` will be returned. For an empty list/tuple/set or empty string an empty tuple is returned. Examples: >>> guids_from_str( "['07fd7195-c51e-44d6-a085-fa8274cf00d6', \ '070d7195-c51e-44d6-a085-fa8274cf00d6']") will return ('07fd7195-c51e-44d6-a085-fa8274cf00d6', '070d7195-c51e-44d6-a085-fa8274cf00d6') """ifs=="":returntuple()try:validate_guid_format(s)return(s,)exceptValueError:passtry:parsed_expression=ast.parse(s,mode="eval")exceptSyntaxError:returnNoneifnothasattr(parsed_expression,"body"):returnNoneparsed=parsed_expression.bodyifisinstance(parsed,ast.Constant):iflen(parsed.value)>0:return(parsed.value,)else:returntuple()ifnotisinstance(parsed,(ast.List,ast.Tuple,ast.Set)):returnNoneifnotall(isinstance(e,ast.Constant)foreinparsed.elts):returnNonestr_elts=cast(tuple[ast.Constant,...],tuple(parsed.elts))returntuple(s.valueforsinstr_elts)