aboutsummaryrefslogtreecommitdiff
path: root/src/ee/order
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-03-14 06:27:16 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2019-03-15 08:22:02 +0100
commit8d17fb5bc4b0dae0758e01a44d77d87acf2e686a (patch)
treea13ed043962b8a8da355bba956dc9e77b7fb217e /src/ee/order
parent2315d1a34cb777f1731368619a4ca14f46125bb4 (diff)
downloadee-python-8d17fb5bc4b0dae0758e01a44d77d87acf2e686a.tar.gz
ee-python-8d17fb5bc4b0dae0758e01a44d77d87acf2e686a.tar.bz2
ee-python-8d17fb5bc4b0dae0758e01a44d77d87acf2e686a.tar.xz
ee-python-8d17fb5bc4b0dae0758e01a44d77d87acf2e686a.zip
o Adding module for searching on element14.
o Starting on functionality create orders. Very WIP. o Adding a concept of an "ee project". Can load a gitconfig-like config file. o Adding a tool to import a yaml file into a part xml file.
Diffstat (limited to 'src/ee/order')
-rw-r--r--src/ee/order/__init__.py88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/ee/order/__init__.py b/src/ee/order/__init__.py
new file mode 100644
index 0000000..7c815b4
--- /dev/null
+++ b/src/ee/order/__init__.py
@@ -0,0 +1,88 @@
+from functools import total_ordering
+from pathlib import Path
+from typing import List, Tuple
+
+from ee.part import PartDb, load_db, save_db
+from ee.xml import bomFile, bom_file_utils
+
+__all__ = ["create_order"]
+
+
+@total_ordering
+class PartInfo(object):
+ def __init__(self, part: bomFile.Part):
+ self.part = part
+ self.id = part.id
+ self.pn = bom_file_utils.find_pn(part)
+ self.available_from: List[Tuple[str, bomFile.Part]] = []
+
+ def __lt__(self, other: "PartInfo"):
+ return self.part.idProp == other.part.idProp
+
+
+def create_order(schematic_dir: Path, out_dir: Path, part_db_dirs: List[Path], fail_on_missing_parts: bool):
+ sch_db = load_db(schematic_dir)
+
+ dbs = [(path.parent.name, load_db(path)) for path in part_db_dirs]
+
+ out_parts = PartDb()
+
+ infos = [PartInfo(sch) for sch in sch_db.iterparts()]
+
+ for info in infos:
+ print("Resolving {}".format(info.id))
+
+ for distributor, db in dbs:
+ for p in db.iterparts(sort=True):
+ if info.pn:
+ p_pn: str = bom_file_utils.find_pn(p)
+
+ if info.pn == p_pn:
+ info.available_from.append((distributor, p))
+
+ for sch_pn_ in bom_file_utils.part_numbers(info.part):
+ sch_pn: bomFile.PartNumber = sch_pn_
+
+ for p_pn_ in bom_file_utils.part_numbers(p):
+ p_pn: bomFile.PartNumber = p_pn_
+
+ if sch_pn.distributorProp == p_pn.distributorProp and sch_pn.value == p_pn.value:
+ if p.idProp not in info.available_from:
+ info.available_from.append((distributor, p))
+
+ for info in infos:
+ print("Resolved {} to {}".format(info.id, info.available_from))
+
+ warnings = []
+ has_missing_parts = False
+ for info in infos:
+ if len(info.available_from) == 0:
+ has_missing_parts = True
+ warnings.append("Could not find {} in any database, part numbers: {}".
+ format(info.id, ", ".join([p.value for p in bom_file_utils.part_numbers(info.part)])))
+
+ for w in sorted(warnings):
+ print(w)
+
+ if has_missing_parts and fail_on_missing_parts:
+ print("has missing parts")
+ return False
+
+ for info in infos:
+ part = bomFile.Part(id=info.part.id,
+ schema_reference=info.part.schema_reference,
+ part_numbers=bomFile.PartNumberList())
+
+ part_numbers = part.part_numbersProp.part_number
+
+ if len(info.available_from) == 0:
+ continue
+
+ distributor, distributor_part = info.available_from[0]
+
+ part_numbers.append(bomFile.PartNumber(value=distributor_part.id, distributor=distributor))
+
+ out_parts.add_entry(part, True)
+
+ print("Saving {} work parts".format(out_parts.size()))
+ save_db(out_dir, out_parts)