diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2019-08-23 13:13:06 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2019-08-23 13:14:53 +0200 |
commit | 9ebdc35df067d63297376814f0bba981246fa44f (patch) | |
tree | 225dc4461ea800c0c5dfc8b486915799b3e88f7a /src | |
parent | 6b7c6e96c727d8f1885b0653cdcbc21730eaf3cb (diff) | |
download | ee-python-9ebdc35df067d63297376814f0bba981246fa44f.tar.gz ee-python-9ebdc35df067d63297376814f0bba981246fa44f.tar.bz2 ee-python-9ebdc35df067d63297376814f0bba981246fa44f.tar.xz ee-python-9ebdc35df067d63297376814f0bba981246fa44f.zip |
ee.part.bom: Creating a proper Bom class to keep BomLines.
digikey-create-bom: Adding support for --quantity.
Diffstat (limited to 'src')
-rw-r--r-- | src/ee/digikey/bom.py | 19 | ||||
-rw-r--r-- | src/ee/part/bom.py | 41 | ||||
-rw-r--r-- | src/ee/tools/digikey_create_bom.py | 4 |
3 files changed, 38 insertions, 26 deletions
diff --git a/src/ee/digikey/bom.py b/src/ee/digikey/bom.py index 398e03b..cb02f05 100644 --- a/src/ee/digikey/bom.py +++ b/src/ee/digikey/bom.py @@ -1,4 +1,5 @@ import csv +from math import ceil from pathlib import Path from typing import List @@ -7,18 +8,22 @@ from ee.part.bom import load_bom, check_bom, generate_bom, join_refs __all__ = ["create_bom"] -def create_bom(bom_path: Path, part_files: List[Path], out_path: Path, allow_incomplete): +def create_bom(bom_path: Path, part_files: List[Path], out_path: Path, allow_incomplete, quantity: float): bom_parts, supplier_parts = load_bom(bom_path, part_files) check_bom(bom_parts, supplier_parts) - lines = generate_bom(allow_incomplete, bom_parts, supplier_parts) + bom = generate_bom(allow_incomplete, bom_parts, supplier_parts) - if lines is None: + if bom is None: return with out_path.open("w") as f: w = csv.writer(f) - w.writerow(["Quantity", "Digi-Key Part Number", "Customer Reference"]) - - for line in lines: - w.writerows([str(len(line.refs)), line.part.get_exactly_one_spn().valueProp, join_refs(line.refs)]) + w.writerow(["Digi-Key Part Number", "Quantity", "Reference Designator"]) + + for line in bom.lines: + line = [ + line.part.get_exactly_one_spn().valueProp, + int(ceil(len(line.refs) * quantity)), + join_refs(line.refs)] + w.writerow([str(cell) for cell in line]) diff --git a/src/ee/part/bom.py b/src/ee/part/bom.py index 0bf284a..fe0bb12 100644 --- a/src/ee/part/bom.py +++ b/src/ee/part/bom.py @@ -25,19 +25,30 @@ class BomLine(object): def __init__(self, uri, part: Part): self.uri = uri self.part = part - self.refs = [] + self.refs: List[str] = [] - def add_ref(self, ref): + def add_ref(self, ref: str): self.refs.append(ref) def __lt__(self, other): return self.uri < other.uri -class BomItem(object): - def __init__(self, references, part: Part): - self.references = references - self.part = part +class Bom(object): + def __init__(self): + self._lines: MutableMapping[str, BomLine] = {} + + def add_part(self, part_uri, supplier_part, reference): + if part_uri not in self._lines: + self._lines[part_uri] = line = BomLine(part_uri, supplier_part) + else: + line = self._lines[part_uri] + + line.add_ref(reference) + + @property + def lines(self) -> List[BomLine]: + return sorted(self._lines.values()) def load_bom(bom_path: Path, part_files: List[Path]) -> (ObjDb[Part](), ObjDb[Part]()): @@ -67,12 +78,10 @@ def check_bom(bom: ObjDb[Part](), parts: ObjDb[Part]()): pass -def generate_bom(allow_incomplete, bom_parts: ObjDb[Part], supplier_parts: ObjDb[Part], group_by_ref=True) -> \ - Optional[List[BomLine]]: - lines: MutableMapping[str, BomLine] = {} - +def generate_bom(allow_incomplete, bom_parts: ObjDb[Part], supplier_parts: ObjDb[Part]) -> Optional[Bom]: uri_idx = supplier_parts.index("uri") + bom = Bom() for part in bom_parts.values: pr = part.get_only_part_reference() @@ -85,15 +94,11 @@ def generate_bom(allow_incomplete, bom_parts: ObjDb[Part], supplier_parts: ObjDb part_uri = pr.part_uriProp - if part_uri not in lines: - supplier_part = uri_idx.get_single(part_uri) - lines[part_uri] = line = BomLine(part_uri, supplier_part) - else: - line = lines[part_uri] - - line.add_ref(part.get_exactly_one_schematic_reference().referenceProp) + supplier_part = uri_idx.get_single(part_uri) + reference = part.get_exactly_one_schematic_reference().referenceProp + bom.add_part(part_uri, supplier_part, reference) - return sorted(lines.values()) + return bom def split_ref(ref): diff --git a/src/ee/tools/digikey_create_bom.py b/src/ee/tools/digikey_create_bom.py index eaea121..ed32578 100644 --- a/src/ee/tools/digikey_create_bom.py +++ b/src/ee/tools/digikey_create_bom.py @@ -23,7 +23,9 @@ parser.add_argument("--part-db", parser.add_argument("--allow-incomplete", action="store_true") +parser.add_argument("--quantity", default=1, type=float) + args = parser.parse_args() ee.tools.process_default_argparse_group(args) -create_bom(Path(args.bom), [Path(p) for p in args.part_db], Path(args.out), args.allow_incomplete) +create_bom(Path(args.bom), [Path(p) for p in args.part_db], Path(args.out), args.allow_incomplete, args.quantity) |