diff options
Diffstat (limited to 'src/ee/tools')
-rw-r--r-- | src/ee/tools/create_order.py | 29 | ||||
-rw-r--r-- | src/ee/tools/element14_search_parts.py | 30 | ||||
-rw-r--r-- | src/ee/tools/import_parts_yaml.py | 52 | ||||
-rw-r--r-- | src/ee/tools/ninja.py | 50 | ||||
-rw-r--r-- | src/ee/tools/templates/build.ninja.j2 | 51 |
5 files changed, 190 insertions, 22 deletions
diff --git a/src/ee/tools/create_order.py b/src/ee/tools/create_order.py new file mode 100644 index 0000000..e0bed58 --- /dev/null +++ b/src/ee/tools/create_order.py @@ -0,0 +1,29 @@ +import argparse +import sys +from pathlib import Path + +from ee.order import create_order + +parser = argparse.ArgumentParser() + +parser.add_argument("--schematic", + required=True, + metavar="DIR") + +parser.add_argument("--out", + required=True, + metavar="DIR") + +parser.add_argument("--part-db", + nargs="*", + required=True, + metavar="PART DB") + +args = parser.parse_args() + +part_db_dirs = [Path(part_db) for part_db in args.part_db] +fail_on_missing_parts = False + +ret = create_order(Path(args.schematic), Path(args.out), part_db_dirs, fail_on_missing_parts) + +sys.exit(1 if ret is False else 0) diff --git a/src/ee/tools/element14_search_parts.py b/src/ee/tools/element14_search_parts.py new file mode 100644 index 0000000..a4793c5 --- /dev/null +++ b/src/ee/tools/element14_search_parts.py @@ -0,0 +1,30 @@ +import argparse +from pathlib import Path + +from ee import project +from ee.element14 import Element14Config +from ee.element14.search_parts import search_parts + +parser = argparse.ArgumentParser() + +parser.add_argument("--in", + dest="in_", + required=True, + metavar="FILE") + +parser.add_argument("--out", + required=True, + metavar="FILE") + +args = parser.parse_args() + +cache_dir = ".ee/cache/element14" + +config = project.load_config(Path(".")) + +e14_config = Element14Config( + store=config.get("element14", "store", fallback=None), + api_key=config.get("element14", "api-key", fallback=None) +) + +search_parts(Path(args.in_), Path(args.out), Path(cache_dir), e14_config) diff --git a/src/ee/tools/import_parts_yaml.py b/src/ee/tools/import_parts_yaml.py new file mode 100644 index 0000000..81a23f8 --- /dev/null +++ b/src/ee/tools/import_parts_yaml.py @@ -0,0 +1,52 @@ +import argparse +import sys +import yaml +from pathlib import Path + +from ee.part import PartDb, save_db +from ee.xml import bomFile + + +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 = bomFile.PartNumberList() + + mpn = item.get("mpn") + assert isinstance(mpn, str) + part_number_list.part_number.append(bomFile.PartNumber(value=mpn)) + + id_ = mpn + + part = bomFile.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_", + 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(args.out)) diff --git a/src/ee/tools/ninja.py b/src/ee/tools/ninja.py index cc090a0..ed8e91a 100644 --- a/src/ee/tools/ninja.py +++ b/src/ee/tools/ninja.py @@ -1,4 +1,5 @@ import argparse +import os.path import sys from pathlib import Path from typing import List, Union, Optional @@ -8,7 +9,10 @@ from jinja2 import Environment, PackageLoader, select_autoescape from ee.kicad import read_schematics -def ninja_path_filter(s: Union[str, List[str]]) -> str: +def ninja_path_filter(s: Union[Path, str, List[str]]) -> str: + if isinstance(s, Path): + s = str(s) + if isinstance(s, str): return s. \ replace("$", "$$"). \ @@ -23,8 +27,16 @@ def ninja_path_filter(s: Union[str, List[str]]) -> str: raise Exception("Unsupported argument type: {}".format(type(s))) -def parent_dir_filter(s: str) -> str: - return str(Path(s).parent) +def parent_dir_filter(s: str) -> Path: + return Path(s).parent + + +def basename_filter(s: Union[str, Path]) -> str: + return os.path.basename(str(s)) + + +def noext_filter(s: Union[str, Path]) -> str: + return os.path.splitext(os.path.basename(str(s)))[0] def generate(sch_path: Path, kicad_bom_strategy: Optional[str]): @@ -36,6 +48,8 @@ def generate(sch_path: Path, kicad_bom_strategy: Optional[str]): ) e.filters["ninja_path"] = ninja_path_filter e.filters["parent_dir"] = parent_dir_filter + e.filters["basename"] = basename_filter + e.filters["noext"] = noext_filter return e gerber_zip = "prod/gerber.zip" @@ -44,20 +58,30 @@ def generate(sch_path: Path, kicad_bom_strategy: Optional[str]): sch_files = sorted([s.path for s in sch.schematics]) - params = {} - import os.path - params["ee"] = "{} -m ee".format(os.path.relpath(sys.executable, Path("."))) - params["sch"] = sch_path - params["sch_files"] = sch_files - - params["kicad_bom_strategy"] = kicad_bom_strategy + part_dbs = [] + params = { + "ee": "{} -m ee".format(os.path.relpath(sys.executable, Path("."))), "sch": sch_path, + "sch_files": sch_files, "kicad_bom_strategy": kicad_bom_strategy, + "pcb": str(sch_path).replace(".sch", ".kicad_pcb"), + "part_dbs": part_dbs, + } - params["pcb"] = str(sch_path).replace(".sch", ".kicad_pcb") + # TODO: read from config + distributors = ["digikey"] + params["distributors"] = distributors if gerber_zip is not None: params["gerber_zip"] = gerber_zip - build_ninja = Path("build.ninja") + build_ninja = sch_path.parent / "build.ninja" + + ee_dir = sch_path.parent / "ee" + parts_yaml_files = [path for path in ee_dir.iterdir() if str(path).endswith("-parts.yaml")] + params["parts_yaml_files"] = parts_yaml_files + + # Local part databases first + part_dbs.extend([parent_dir_filter(p) / noext_filter(p) for p in parts_yaml_files]) + part_dbs.extend([Path("ee") / d / "normalized" for d in distributors]) with build_ninja.open("w") as f: env = _create_env() @@ -77,4 +101,4 @@ parser.add_argument("--kicad-bom-strategy", args = parser.parse_args() -generate(args.sch, args.kicad_bom_strategy) +generate(Path(args.sch), args.kicad_bom_strategy) diff --git a/src/ee/tools/templates/build.ninja.j2 b/src/ee/tools/templates/build.ninja.j2 index 79a2bc1..e91de6c 100644 --- a/src/ee/tools/templates/build.ninja.j2 +++ b/src/ee/tools/templates/build.ninja.j2 @@ -28,6 +28,22 @@ rule digikey-normalize-facts description = digikey-normalize-facts command = $ee digikey-normalize-facts --in $in_dir --out $out_dir +rule element14-search-parts + description = element14-search-parts + command = $ee element14-search-parts --in $in_dir --out $out_dir + +rule element14-normalize-facts + description = element14-normalize-facts + command = $ee element14-normalize-facts --in $in_dir --out $out_dir + +rule create-order + description = create-order + command = $ee create-order --schematic $sch_dir --part-db $part_dbs --out $out_dir + +rule import-parts-yaml + description = import-parts-yaml $in + command = $ee import-parts-yaml --in $in --out $out_dir + {% if gerber_zip is defined %} build gerbers: phony {{ gerber_zip }} build {{ gerber_zip }}: kicad-gerber $pcb @@ -38,16 +54,33 @@ build ee/sch/index.xml: kicad-make-bom $sch out_dir = ee/sch strategy = {{ "--strategy " + kicad_bom_strategy if kicad_bom_strategy else "" }} -build ee/digikey/search-list/index.xml: part-create-distributor-search-list ee/sch/index.xml +{%- for d in distributors %} +# Distributor {{ d }} +build ee/{{ d }}/search-list/index.xml: part-create-distributor-search-list ee/sch/index.xml in_dir = ee/sch - out_dir = ee/digikey/search-list + out_dir = ee/{{ d }}/search-list + +build ee/{{ d }}/downloaded/index.xml: {{ d }}-search-parts ee/{{ d }}/search-list/index.xml + in_dir = ee/{{ d }}/search-list + out_dir = ee/{{ d }}/downloaded + +build ee/{{ d }}/normalized/index.xml: {{ d }}-normalize-facts ee/{{ d }}/downloaded/index.xml + in_dir = ee/{{ d }}/downloaded + out_dir = ee/{{ d }}/normalized + +default ee/{{ d }}/normalized/index.xml +{%- endfor %} -build ee/digikey/downloaded/index.xml: digikey-search-parts ee/digikey/search-list/index.xml - in_dir = ee/digikey/search-list - out_dir = ee/digikey/downloaded +{%- for f in parts_yaml_files %} +{% set out=(f | parent_dir) / (f | noext) / "index.xml" %} +build {{ out }}: import-parts-yaml {{ f }} + out_dir = {{ f | parent_dir }}/{{ f | noext }} +# default {{ out }} +{% endfor %} -build ee/digikey/normalized/index.xml: digikey-normalize-facts ee/digikey/downloaded/index.xml - in_dir = ee/digikey/downloaded - out_dir = ee/digikey/normalized +build ee/order/index.xml: create-order ee/sch/index.xml {%- for p in part_dbs %} {{ p / "index.xml" }}{% endfor %} + sch_dir = ee/sch + out_dir = ee/order + part_dbs ={%- for p in part_dbs %} {{ p }}{% endfor %} -default ee/digikey/normalized/index.xml +default ee/order/index.xml |