import argparse import csv from pathlib import Path import ee.tools from ee import EeException from ee.db import ObjDb from ee.logging import log from ee.part import Part, load_db, save_db, PartDb def work(in_path: Path, out_path: Path, work_dir: Path): dialect = "excel-tab" in_parts: ObjDb[Part] = ObjDb[Part]() uri_idx = in_parts.add_unique_index("uri", lambda p: p.uri) in_db = load_db(in_path) for entry in in_db.parts: in_parts.add(Part(entry.part)) out_parts = PartDb() out_parts.categories.extend(in_db.categories) for part in in_parts: out_parts.add_entry(part, False) out_dir = work_dir / "out" # Sort the output to keep changes in the file to a minimum when the rules change. for file in out_dir.iterdir(): if file.name.endswith(".csv"): with file.open("r") as f: reader = csv.reader(f, dialect=dialect) rows = sorted(reader) tmp = Path(str(file) + ".tmp") with tmp.open("w") as out: writer = csv.writer(out, dialect=dialect) writer.writerows(rows) tmp.rename(file) mpn_csv = out_dir / "mpn.csv" new_facts = 0 with (out_dir / "add_fact.csv").open("r") as f: reader = csv.reader(f, dialect=dialect) for uri, key, value in reader: try: part = uri_idx.get_single(uri) except KeyError: raise EeException("Unknown part found in output: uri={}".format(uri)) part.facts.add(key, value) if mpn_csv.is_file(): with mpn_csv.open("r") as f: reader = csv.reader(f, dialect=dialect) for uri, mpn in reader: try: part = uri_idx.get_single(uri) except KeyError: raise EeException("Unknown part found in output: uri={}".format(uri)) part.add_mpn(mpn) log.summary("New facts found: {}".format(new_facts)) save_db(out_path, out_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="PART DB") parser.add_argument("--work", required=True, metavar="DIR") args = parser.parse_args() ee.tools.process_default_argparse_group(args) work(Path(args.in_path), Path(args.out), Path(args.work))