import argparse import sys from pathlib import Path import yaml from ee.part import PartDb, save_db from ee.xml import types def import_parts_yaml(in_file: Path, out_dir: Path): with in_file.open("r") as f: doc = yaml.safe_load(f) if not isinstance(doc, list): print("Bad YAML document, the root must be a list", file=sys.stderr) parts = PartDb() for item in doc: if not isinstance(item, dict): print("Bad YAML document, each part must be a dict", file=sys.stderr) return part_number_list = types.PartNumberList() mpn = item.get("mpn") assert isinstance(mpn, str) part_number_list.part_number.append(types.PartNumber(value=mpn)) id_ = mpn part = types.Part(id=id_) part.part_numbersProp = part_number_list parts.add_entry(part, True) print("Imported {} parts".format(parts.size())) save_db(out_dir, parts) parser = argparse.ArgumentParser() parser.add_argument("--in", dest="in_path", required=True, metavar="PARTS_YAML") parser.add_argument("--out", required=True, metavar="PART DB") args = parser.parse_args() import_parts_yaml(Path(args.in_path), Path(args.out))