aboutsummaryrefslogtreecommitdiff
path: root/src/ee/digikey
diff options
context:
space:
mode:
Diffstat (limited to 'src/ee/digikey')
-rw-r--r--src/ee/digikey/__init__.py80
1 files changed, 63 insertions, 17 deletions
diff --git a/src/ee/digikey/__init__.py b/src/ee/digikey/__init__.py
index af5977b..1315f96 100644
--- a/src/ee/digikey/__init__.py
+++ b/src/ee/digikey/__init__.py
@@ -6,7 +6,8 @@ import re
import requests
import os
import os.path
-import yaml
+import configparser
+import glob
from cachecontrol import CacheControl
from cachecontrol.caches.file_cache import FileCache
from cachecontrol.heuristics import ExpiresAfter
@@ -90,13 +91,32 @@ class DigikeyProduct(object):
def __hash__(self):
return self.part_number.__hash__()
- def to_yaml(self):
- yaml = {"part_number": self.part_number}
+ def to_ini(self):
+ c = configparser.ConfigParser()
+ c["overview"] = {}; overview = c["overview"]
+ overview["part_number"] = self.part_number
if self.mpn:
- yaml["mpn"] = self.mpn
- yaml["attributes"] = [{"type": {"id": a.attribute_type.id, "label": a.attribute_type.label}, "value": a.value}
- for a in self.attributes]
- return yaml
+ overview["mpn"] = self.mpn
+ c["attributes"] = {}; attributes = c["attributes"]
+ for a in self.attributes:
+ key = "{}/{}".format(a.attribute_type.id, a.attribute_type.label)
+ key = key.replace("%", "_")
+ value = a.value.replace("%", "%%")
+ attributes[key] = value
+ return c
+
+ from_ini_r = re.compile("([^/]*)/(.*)")
+
+ @staticmethod
+ def from_ini(digikey, c):
+ overview = c["overview"]
+ attributes = []
+ for k, value in c.items("attributes"):
+# print("k={}".format(k))
+ (type_id, label) = DigikeyProduct.from_ini_r.match(k).groups()
+ a_type = digikey.get_attribute_type(type_id, label)
+ attributes.append(DigikeyAttributeValue(value, a_type))
+ return DigikeyProduct(overview["part_number"], overview["mpn"], attributes)
class DigikeyAttributeType(object):
@@ -228,8 +248,8 @@ class DigikeyClient(object):
products = tree.xpath("//*[@itemtype='http://schema.org/Product']")
for product in products:
- part_number = _first(product.xpath("//*[@itemprop='productid' and @content]"))
- mpn = _first(product.xpath("//*[@itemprop='name']"))
+ part_number = _first(product.xpath(".//*[@itemprop='productid' and @content]"))
+ mpn = _first(product.xpath(".//*[@itemprop='name']"))
if part_number is not None and mpn is not None:
res.append(DigikeyProduct(
@@ -272,19 +292,45 @@ class DigikeyClient(object):
return DigikeySearchResponse(1, SearchResponseTypes.NO_MATCHES)
class DigikeyRepository(object):
- def __init__(self, path):
+ def __init__(self, digikey, path):
+ self._digikey = digikey
self._path = path
+ self._products = {}
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)
+ mpn = mpn.replace("/", "_").replace(" ", "_")
+ return "{}/{}.ini".format(self._path, mpn)
+
+ def has_product(self, mpn=None, dpn=None):
+ if mpn is not None:
+ filename = self.mpn_to_path(mpn)
+ return os.path.isfile(filename)
+ if dpn is not None:
+ for p in self.products:
+ if p.part_number == dpn:
+ return p
def save(self, product: DigikeyProduct):
- y = product.to_yaml()
+ y = product.to_ini()
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)
+ y.write(f)
+
+ def load_all(self):
+ [self._load(path) for path in glob.glob(self._path + "/*.ini")]
+
+ def _load(self, path):
+ c = configparser.ConfigParser()
+ c.read(path)
+ p = DigikeyProduct.from_ini(self._digikey, c)
+ self._products[p.mpn] = p
+ return p
+
+ @property
+ def products(self):
+ self.load_all()
+ return self._products.values()
+
+ def find_by_mpn(self, mpn):
+ return [p for p in self.products if p.mpn == mpn]