import argparse from pathlib import Path from typing import List, Tuple import ee.tools from ee.db import ObjDb from ee.logging import log from ee.part import Part, load_db, save_db, PartDb from ee.part.bom import load_bom, check_bom class OrderPart(object): def __init__(self, order_part: Part, part: Part): self.order_part = order_part self.part = part def split_bom_by_supplier(bom_path: Path, part_files: List[Path], supplier_list: List[Tuple[str, str, Path]]): # key, url, file bom_parts, supplier_parts = load_bom(bom_path, part_files) check_bom(bom_parts, supplier_parts) uri_idx = supplier_parts.index("uri") # for supplier, uri, path in supplier_list: # print(supplier, uri, path) supplier_info = {uri: path for _, uri, path in supplier_list} part_dbs = {uri: PartDb() for _, uri, _ in supplier_list} for bom_part in bom_parts.values: sch = bom_part.get_exactly_one_schematic_reference().referenceProp part_reference = bom_part.get_only_part_reference() if not part_reference: # log.warn("Missing part for {}, allowing for now".format(sch)) continue # print("{}: {}".format(sch, part_reference.part_uriProp)) part = uri_idx.get_single(part_reference.part_uriProp) # print("{}: {}".format(sch, part.uri)) # print("{}: {}".format(sch, part.supplier)) part_dbs[part.supplier].add_entry(bom_part, new=False) for uri, path in supplier_info.items(): part_db = part_dbs[uri] save_db(path, part_db) parser = argparse.ArgumentParser() ee.tools.add_default_argparse_group(parser) parser.add_argument("--bom", required=True, metavar="PART DB") parser.add_argument("--part-db", nargs="*", required=True, metavar="PART DB") parser.add_argument("--supplier", required=True, nargs="*", metavar="SUPPLIER") parser.add_argument("--order", required=True, nargs="*", metavar="PART DB") args = parser.parse_args() ee.tools.process_default_argparse_group(args) ss = [] order_files = {o.split("=", maxsplit=1)[0]: o.split("=", maxsplit=1)[1] for o in args.order} for s in args.supplier: key, url = s.split("=", maxsplit=1) file = order_files[key] ss.append((key, url, Path(file))) split_bom_by_supplier(Path(args.bom), [Path(path) for path in args.part_db], ss)