import argparse import pydoc from pathlib import Path from typing import List import ee.tools from ee import tools, EeException from ee.part import Part, load_db, save_db from ee.project import Project def load_functions(function_names): functions = [] for fn in function_names: f = pydoc.locate(fn) if f is None: raise EeException("Could not load function: {}".format(fn)) if not callable(f): raise EeException("Not a callable: {}, is a {}".format(fn, type(f))) functions.append((fn, f)) return functions def work(in_path: Path, out_path: Path, report_path: Path, function_names: List[str], arguments: List[str]): factories = load_functions(function_names) in_parts = load_db(in_path) kwargs = {} for a in arguments: import re k, v = re.split(":", a, maxsplit=1) kwargs[k] = v functions = [[factory[0], factory[1](**kwargs)] for factory in factories] tools.mk_parents(report_path) with report_path.open("w") as rpt: if len(kwargs): print("Arguments:", file=rpt) print("", file=rpt) for key in sorted(kwargs): print(" * {}={}".format(key, kwargs[key]), file=rpt) print("", file=rpt) for xml in in_parts.iterparts(): part = Part(xml) for name, f in functions: part = f(part) save_db(out_path, in_parts) parser = argparse.ArgumentParser() ee.tools.add_default_argparse_group(parser) parser.add_argument("--in", dest="in_path", required=True, metavar="PART DB") parser.add_argument("--out", required=True, metavar="REQUIREMENTS") parser.add_argument("--function", required=True, nargs="*", metavar="FUNCTION") parser.add_argument("--argument", nargs="*", metavar="ARG") parser.add_argument("--execution", required=True) args = parser.parse_args() ee.tools.process_default_argparse_group(args) project = Project.load() report = project.report_dir / "apply-function" / (args.execution + ".rst") work(Path(args.in_path), Path(args.out), report, args.function, args.argument or [])