def get_recipe_contexts(recipe): """ Extract list of recipe context definition from the recipe file and related query files Args: recipe (str): name of recipe Returns: result (dict): definition of all contexts needed for recipe """ mod = get_module(recipe) # try to extract name of query and run contexts automatically from run function queries, run_contexts = context.extract_arguments(mod.run, "run_query") specific_contexts = collections.OrderedDict() if hasattr(mod, 'RUN_CONTEXTS'): context_info = mod.RUN_CONTEXTS for item in context_info: specific_contexts.update(item) # get full definition of all contexts needed for recipe recipe_context_def = collections.OrderedDict() for query_name in set(queries): query_context_def = load_query_context(query_name) recipe_context_def.update(query_context_def) run_context_def = context.get_context_definitions(run_contexts, specific_contexts) recipe_context_def.update(run_context_def) return recipe_context_def
def load_query_context(name, add_contexts=[]): """ Get query context from yaml file. Args: name (str): name of query add_contexts (list): additional contexts if needed Returns: query_contexts (list): mixed array of strings (name of common contexts) and dictionaries (full definition of specific contexts) """ with open(sources.get(name, query=True)) as fh: query = yaml.load(fh, Loader=yaml.SafeLoader) # Extract query and context specific_contexts = query.pop("context") if "context" in query else {} contexts = context.extract_context_names(query) contexts.update(add_contexts) query_contexts = context.get_context_definitions(contexts, specific_contexts) return query_contexts
def test_get_definitions(contexts, expected_definitions): definitions = context.get_context_definitions(contexts) assert expected_definitions == definitions