import configparser from pathlib import Path from ee import EeException from ee.tools import mk_parents from ee.xml.uris import DIGIKEY_URI def load_config(project_dir: Path) -> configparser.ConfigParser: config = configparser.ConfigParser() config_path = project_dir / ".ee" / "config" try: with config_path.open("r") as f: config.read_file(f, source=str(config_path)) except FileNotFoundError: pass return config class SupplierDescriptor(object): def __init__(self, key: str, uri: str): self.key = key self.uri = uri class Project(object): def __init__(self, project_dir: Path, cfg: configparser.ConfigParser): self.report_dir = project_dir / "ee" / "reports" self.public_dir = project_dir / "ee" self.project_dir = project_dir self._cfg = cfg # TODO: read from config self._suppliers = [SupplierDescriptor("digikey", DIGIKEY_URI)] def get_supplier_by_key(self, key) -> SupplierDescriptor: sd = next((s for s in self._suppliers if s.key == key), None) if sd: return sd raise EeException("No such supplier configured: {}".format(key)) @property def cfg(self): return self._cfg @classmethod def load(cls): project_dir = Path(".") cfg = load_config(project_dir) return Project(project_dir, cfg) def save(self): path = self.project_dir / ".ee" / "config" mk_parents(path) with (path).open("w") as f: self._cfg.write(f)