diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ee/digikey/__init__.py | 22 | ||||
-rw-r--r-- | src/ee/kicad/__init__.py | 3 | ||||
-rw-r--r-- | src/ee/kicad/model.py | 9 | ||||
-rw-r--r-- | src/ee/kicad/to_bom.py | 8 | ||||
-rw-r--r-- | src/ee/tools/digikey_download_facts.py | 42 | ||||
-rw-r--r-- | src/ee/tools/kicad_make_bom.py | 2 |
6 files changed, 64 insertions, 22 deletions
diff --git a/src/ee/digikey/__init__.py b/src/ee/digikey/__init__.py index 64d8943..af5977b 100644 --- a/src/ee/digikey/__init__.py +++ b/src/ee/digikey/__init__.py @@ -1,8 +1,12 @@ import enum from typing import List, Optional +from ee.tools import mk_parents import re import requests +import os +import os.path +import yaml from cachecontrol import CacheControl from cachecontrol.caches.file_cache import FileCache from cachecontrol.heuristics import ExpiresAfter @@ -266,3 +270,21 @@ class DigikeyClient(object): return res else: return DigikeySearchResponse(1, SearchResponseTypes.NO_MATCHES) + +class DigikeyRepository(object): + def __init__(self, path): + self._path = path + + def mpn_to_path(self, mpn): + return "{}/{}.yaml".format(self._path, mpn) + + def has_product(self, mpn): + filename = self.mpn_to_path(mpn) + return os.path.isfile(filename) + + def save(self, product: DigikeyProduct): + y = product.to_yaml() + filename = self.mpn_to_path(product.mpn) + mk_parents(filename) + with open(filename, "w") as f: + yaml.dump(y, f, encoding="utf-8", allow_unicode=True) diff --git a/src/ee/kicad/__init__.py b/src/ee/kicad/__init__.py index adf8ddd..5187a9c 100644 --- a/src/ee/kicad/__init__.py +++ b/src/ee/kicad/__init__.py @@ -2,7 +2,7 @@ from typing import Any from ee import EeException from ee.kicad.read_schematic import read_schematic -from ee.kicad.to_bom import to_bom +from ee.kicad.to_bom import to_bom, to_bom_xml from .model import * __all__ = [ @@ -12,6 +12,7 @@ __all__ = [ "Schematic", "read_schematic", "to_bom", + "to_bom_xml", "to_pandas", ] diff --git a/src/ee/kicad/model.py b/src/ee/kicad/model.py index f112e72..b230f94 100644 --- a/src/ee/kicad/model.py +++ b/src/ee/kicad/model.py @@ -105,6 +105,10 @@ class Component(object): return self.ref_type == "#PWR" @property + def is_flg(self) -> bool: + return self.ref_type == "#FLG" + + @property def value(self): return self._fields[1].value @@ -120,6 +124,11 @@ class Component(object): def named_fields(self): return [f for f in self._fields if f.name] + def get_field(self, name) -> ComponentField: + for f in self.fields: + if name.lower() == f.name.lower(): + return f + class Sheet(object): def __init__(self): diff --git a/src/ee/kicad/to_bom.py b/src/ee/kicad/to_bom.py index d684486..ecab211 100644 --- a/src/ee/kicad/to_bom.py +++ b/src/ee/kicad/to_bom.py @@ -34,7 +34,11 @@ def comp(c: Component) -> Element: return comp -def to_bom(schematic: Schematic) -> Element: +def to_bom(schematic: Schematic) -> Component: + return [c for c in sorted(schematic.components) if c.ref_type != "#PWR" and c.ref_type != "#FLG"] + + +def to_bom_xml(schematic: Schematic) -> Element: root = Element("export") root.attrib["version"] = "D" design = Element("design") @@ -42,6 +46,6 @@ def to_bom(schematic: Schematic) -> Element: components = Element("components") root.append(components) - [components.append(comp(c)) for c in sorted(schematic.components) if c.ref_type != "#PWR" and c.ref_type != "#FLG"] + [components.append(comp(c)) for c in to_bom(schematic)] return root diff --git a/src/ee/tools/digikey_download_facts.py b/src/ee/tools/digikey_download_facts.py index c97ba9d..0550c5f 100644 --- a/src/ee/tools/digikey_download_facts.py +++ b/src/ee/tools/digikey_download_facts.py @@ -1,8 +1,6 @@ import argparse from itertools import * -import os.path -import yaml from colors import color import ee.digikey as dk @@ -11,11 +9,6 @@ from ee.tools import mk_parents parser = argparse.ArgumentParser(description="Download facts about parts from Digi-Key") -parser.add_argument("parts", - metavar="PART", - nargs="+", - help="The parts to download fact for") - parser.add_argument("--out", required=True, metavar="OUTPUT_DIRECTORY", @@ -23,6 +16,12 @@ parser.add_argument("--out", action="store", help="A directory to store fact files") +parser.add_argument("--part", + nargs="+", + help="the parts to download fact for") + +parser.add_argument("--sch") + parser.add_argument("--force", dest="force", action="store", @@ -32,26 +31,33 @@ args = parser.parse_args() digikey = dk.Digikey() client = dk.DigikeyClient(digikey, on_download=lambda s: print(color(s, 'grey'))) +repo = dk.DigikeyRepository(args.out) -def mpn_to_path(mpn): - return "{}/{}.yaml".format(args.out, mpn) +def on_product(product: DigikeyProduct): + repo.save(product) -def on_product(product: DigikeyProduct): - y = product.to_yaml() +parts = [] - filename = mpn_to_path(product.mpn) - mk_parents(filename) - with open(filename, "w") as f: - yaml.dump(y, f, encoding="utf-8", allow_unicode=True) +if args.part: + parts.append(args.part) +if args.sch: + from ee.kicad import read_schematic, to_bom + sch = read_schematic(args.sch) + for c in to_bom(sch): + digikey = c.get_field("digikey") + if digikey: + parts.append(digikey.value) + mpn = c.get_field("mpn") + if mpn: + parts.append(mpn.value) -for p in args.parts: +for p in parts: print(color("Searching for {}".format(p), "white")) - path = mpn_to_path(p) - if os.path.isfile(path) and not args.force: + if repo.has_product(p) and not args.force: continue response = client.search(p) diff --git a/src/ee/tools/kicad_make_bom.py b/src/ee/tools/kicad_make_bom.py index 518cf75..7d25799 100644 --- a/src/ee/tools/kicad_make_bom.py +++ b/src/ee/tools/kicad_make_bom.py @@ -23,7 +23,7 @@ args = parser.parse_args() sch = kicad.read_schematic(args.sch) -bom = kicad.to_bom(sch) +bom = kicad.to_bom_xml(sch) xml = ElementTree.tostring(bom, encoding='unicode') if args.pretty: |