import argparse import csv import os.path from pathlib import Path import ee.tools from ee.part import load_db, Part def run(args): print_header = False part_db = Path(args.part_db) out_dir = part_db.parent / "{}.{}".format(os.path.splitext(part_db.name)[0], "souffle") if not out_dir.is_dir(): ee.tools.mk_dirs(out_dir) with open(out_dir / "facts.dl", "w") as f: print(".decl part(uri:symbol, ref:symbol, supplier:symbol)", file=f) print(".input part", file=f) print("", file=f) print(".decl fact(part_uri:symbol, key:symbol, value:symbol)", file=f) print(".input fact", file=f) with open(out_dir / "part.facts", "w") as part_f: with open(out_dir / "fact.facts", "w") as fact_f: part_csv = csv.writer(part_f, dialect="excel-tab") if print_header: part_csv.writerow(["uri", "ref", "supplier"]) fact_csv = csv.writer(fact_f, dialect="excel-tab") if print_header: fact_csv.writerow(["part_uri", "key", "value"]) for xml in load_db(part_db).iterparts(): p = Part(xml) sch_ref = p.get_only_schematic_reference() part_csv.writerow([p.uri, sch_ref.referenceProp if sch_ref else None, p.supplier]) for fact in p.get_facts(): fact_csv.writerow([p.uri, fact.keyProp, fact.valueProp]) import ee.tools parser = argparse.ArgumentParser() ee.tools.add_default_argparse_group(parser) parser.add_argument("--part-db", required=True, metavar="PART DB") args = parser.parse_args() ee.tools.process_default_argparse_group(args) run(args)