diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2019-05-15 22:08:37 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2019-05-24 07:57:00 +0200 |
commit | 4afac7dc4c743284e5243428f00928aa7eaacfdc (patch) | |
tree | d2e65dbced03e310e15509a239eb096f41d047f4 | |
parent | d8b6719c628c7dfb4537ad2303c016884e9312f3 (diff) | |
download | ee-python-4afac7dc4c743284e5243428f00928aa7eaacfdc.tar.gz ee-python-4afac7dc4c743284e5243428f00928aa7eaacfdc.tar.bz2 ee-python-4afac7dc4c743284e5243428f00928aa7eaacfdc.tar.xz ee-python-4afac7dc4c743284e5243428f00928aa7eaacfdc.zip |
ee.project: Making sure all projects have an UUID.
kicad-make-bom: Using the project's UUID to generate an URL for all
parts.
-rw-r--r-- | src/ee/kicad/make_bom.py | 18 | ||||
-rw-r--r-- | src/ee/project/__init__.py | 12 | ||||
-rw-r--r-- | src/ee/tools/init.py | 3 | ||||
-rw-r--r-- | src/ee/tools/kicad_make_bom.py | 8 | ||||
-rw-r--r-- | src/ee/tools/templates/build.ninja.j2 | 3 | ||||
-rw-r--r-- | src/ee/xml/uris.py | 7 |
6 files changed, 44 insertions, 7 deletions
diff --git a/src/ee/kicad/make_bom.py b/src/ee/kicad/make_bom.py index 5242832..2faae31 100644 --- a/src/ee/kicad/make_bom.py +++ b/src/ee/kicad/make_bom.py @@ -1,4 +1,6 @@ +import uuid from pathlib import Path +from uuid import UUID from xml.dom import minidom from xml.etree import ElementTree @@ -13,7 +15,7 @@ __all__ = [ ] -def work(sch, out: Path, new_mode, pretty): +def work(sch, out: Path, project_uuid: UUID, new_mode, pretty): def strip(s): s = (s or "").strip() @@ -30,8 +32,16 @@ def work(sch, out: Path, new_mode, pretty): else: parts = PartDb() components = to_bom(sch, require_ref=False) + count = 0 for c in components: - xml = types.Part() + count = count + 1 + + if c.has_ref_num: + uri_ref = c.ref + else: + uri_ref = str(uuid.uuid5(uuid.NAMESPACE_DNS, str(count))) + + xml = types.Part(uri=uris.make_schematic_part_uri(project_uuid, uri_ref)) part = Part(xml) if c.has_ref_num: @@ -64,7 +74,7 @@ def work(sch, out: Path, new_mode, pretty): save_db(out, parts) -def make_bom(sch_file: Path, out_dir: Path, new_mode: bool, pretty: bool): +def make_bom(sch_file: Path, out_dir: Path, project_uuid: UUID, new_mode: bool, pretty: bool): sch = read_schematics(str(sch_file)) - work(sch, out_dir, new_mode, pretty) + work(sch, out_dir, project_uuid, new_mode, pretty) diff --git a/src/ee/project/__init__.py b/src/ee/project/__init__.py index 0857e7c..6c04c07 100644 --- a/src/ee/project/__init__.py +++ b/src/ee/project/__init__.py @@ -1,4 +1,5 @@ import configparser +import uuid from pathlib import Path from ee import EeException @@ -34,6 +35,13 @@ class Project(object): self.project_dir = project_dir self._cfg = cfg + if "project" not in cfg: + cfg.add_section("project") + + project = cfg["project"] + if "uuid" not in project: + project["uuid"] = str(uuid.uuid4()) + # TODO: read from config self._suppliers = [] digikey_store = DigikeyStore.from_store_code("us") @@ -55,6 +63,10 @@ class Project(object): def cfg(self): return self._cfg + @property + def uuid(self): + return uuid.UUID(hex=self._cfg["project"]["uuid"]) + @classmethod def load(cls): project_dir = Path(".") diff --git a/src/ee/tools/init.py b/src/ee/tools/init.py index 1e01dc7..711e623 100644 --- a/src/ee/tools/init.py +++ b/src/ee/tools/init.py @@ -1,5 +1,6 @@ import argparse import configparser +import uuid from pathlib import Path from typing import List @@ -19,6 +20,8 @@ def init_kicad_project(basedir: Path, cfg, args): sch_file: Path = pro_file.parent / (pro_file.name[0:-4] + ".sch") pcb_file: Path = pro_file.parent / (pro_file.name[0:-4] + ".kicad_pcb") + cfg["kicad-project"]["uuid"] = str(uuid.uuid4()) + if sch_file.is_file(): print("Found KiCAD project and schematic") cfg["kicad-project"]["sch"] = str(sch_file) diff --git a/src/ee/tools/kicad_make_bom.py b/src/ee/tools/kicad_make_bom.py index 0e36c6d..925cad6 100644 --- a/src/ee/tools/kicad_make_bom.py +++ b/src/ee/tools/kicad_make_bom.py @@ -1,4 +1,5 @@ import argparse +import uuid from pathlib import Path from ee.kicad.make_bom import make_bom @@ -13,13 +14,18 @@ parser.add_argument("--sch", help="The schematic to read") parser.add_argument("--out", + required=True, metavar="PART DB") parser.add_argument("--strategy", metavar="FUNC") +parser.add_argument("--uuid", + required=True, + metavar="UUID") + args = parser.parse_args() new_mode = True -make_bom(Path(args.sch), Path(args.out), new_mode, pretty) +make_bom(Path(args.sch), Path(args.out), uuid.UUID(args.uuid), new_mode, pretty) diff --git a/src/ee/tools/templates/build.ninja.j2 b/src/ee/tools/templates/build.ninja.j2 index 522b010..0706b11 100644 --- a/src/ee/tools/templates/build.ninja.j2 +++ b/src/ee/tools/templates/build.ninja.j2 @@ -1,5 +1,6 @@ {% set reports=[] -%} ee = {{ ee }} +uuid = {{ project.uuid }} report_dir = {{ project.report_dir }} {%- if sch is defined %} sch = {{ sch | ninja_path }} @@ -20,7 +21,7 @@ rule kicad-gerber rule kicad-make-bom description = kicad-make-bom $out - command = $ee kicad-make-bom --sch $sch --out $out + command = $ee kicad-make-bom --sch $sch --out $out --uuid $uuid rule pn-part-search-list description = pn-part-search-list supplier: $supplier diff --git a/src/ee/xml/uris.py b/src/ee/xml/uris.py index bb961df..355df83 100644 --- a/src/ee/xml/uris.py +++ b/src/ee/xml/uris.py @@ -1,4 +1,5 @@ -from typing import Optional +import uuid +from typing import Optional, Union # Values for `..#ee-component-type` facts CAPACITOR = "http://purl.org/ee/part-type#capacitor" @@ -32,3 +33,7 @@ _FACT_KEY_PREFIX = "http://purl.org/trygvis/ee/fact-key#" def make_fact_key(name: str) -> str: return "{}{}".format(_FACT_KEY_PREFIX, name) + + +def make_schematic_part_uri(project: Union[str, uuid.UUID], schematic_reference: str) -> str: + return "http://purl.org/trygvis/ee/project/{}#{}".format(project, schematic_reference) |