diff options
Diffstat (limited to 'src/ee/digikey')
-rw-r--r-- | src/ee/digikey/__init__.py | 22 |
1 files changed, 22 insertions, 0 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) |