From 4896b414e70b7c537825ea3375915e1f46d1b9cd Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 1 Aug 2018 10:33:39 +0200 Subject: o Moving all doit modules to use DoitConfig(). --- demo/doit/dodo.py | 5 ++-- src/ee/bom/doit.py | 66 ++++++++++++-------------------------------------- src/ee/doit.py | 11 +++++++-- src/ee/kicad/doit.py | 59 ++++++++++++++++---------------------------- test/doit/test_doit.py | 7 +++--- 5 files changed, 53 insertions(+), 95 deletions(-) diff --git a/demo/doit/dodo.py b/demo/doit/dodo.py index fe9b9ae..c2b8194 100644 --- a/demo/doit/dodo.py +++ b/demo/doit/dodo.py @@ -6,6 +6,7 @@ import ee.kicad.doit from ee.bom.doit import * # noinspection PyUnresolvedReferences from ee.digikey.doit import * +from ee.ds import DataSetManager from ee.kicad.doit import * prj = "demo" @@ -21,7 +22,7 @@ dsm = DataSetManager("ee") kicad_footprint = "kicad-footprint" dsm.add_ds("csv", kicad_footprint, "kicad-footprint-mapping", path="ee/kicad-footprint.csv") -ee.kicad.doit.change_data_sets_for_task(task_kicad_create_component_data_set, lambda ds: ds + [kicad_footprint]) +ee.kicad.doit.doit_config.change_data_sets_for_task(task_kicad_create_component_data_set, lambda ds: ds + [kicad_footprint]) ee.kicad.doit.init( data_set_manager=dsm, @@ -38,7 +39,7 @@ ee.digikey.doit.doit_config.configure(data_set_manager=dsm) def task_orders(): - data_sets = [ee.bom.doit.output_data_set_for_task(task_bom), + data_sets = [ee.bom.doit.doit_config.out_data_set_for(task_bom), ee.digikey.doit.doit_config.out_data_set_for(task_digikey_resolve_schematic_components)] yield create_task_order_csv( diff --git a/src/ee/bom/doit.py b/src/ee/bom/doit.py index 7c8f63a..bf6975b 100644 --- a/src/ee/bom/doit.py +++ b/src/ee/bom/doit.py @@ -1,48 +1,15 @@ import logging from pathlib import Path -from typing import Mapping, Union +from typing import Union -from configclass import Config from namedlist import namedlist -from ee.ds import DataSetManager, DataSet, create_message +from ee.doit import DoItConfig +from ee.ds import DataSet, create_message logger = logging.getLogger(__name__) -_dsm = None # type: DataSetManager - -_data_sets = {} - - -def change_data_sets_for_task(task, _callable): - _data_sets[task][1] = _callable(_data_sets[task][1]) - - -def output_data_set_for_task(task): - try: - return _data_sets[task][0] - except KeyError: - raise KeyError("No such task registered in this module: {}".format(task)) - - -def input_data_sets_for_task(task): - try: - return _data_sets[task][1] - except KeyError: - raise KeyError("No such task registered in this module: {}".format(task)) - - -_config_template = Config({}) - -_config = None # type: Mapping[str, str] - - -def init(data_set_manager: DataSetManager, **kwargs): - global _config - _config = _config_template.make(kwargs) - - global _dsm - _dsm = data_set_manager +doit_config = DoItConfig() class BomComponent(object): @@ -65,13 +32,12 @@ def task_bom(): creates 'bom-component' objects. """ - out_data_set = _data_sets[task_bom][0] - in_data_sets = _data_sets[task_bom][1] + out_data_set, in_data_sets = doit_config.data_sets_for(task_bom) def action(): - in_ds = _dsm.load_data_sets(in_data_sets) + in_ds = doit_config.dsm.load_data_sets(in_data_sets) - with _dsm.create_rw(out_data_set, clean=True) as output: + with doit_config.dsm.create_rw(out_data_set, clean=True) as output: components = [o for o in in_ds.items() if o.object_type.name == "component"] bom_components = {} @@ -95,17 +61,17 @@ def task_bom(): [c.to_object(output) for c in bom_components.values()] return { - "file_dep": [_dsm.cookie_for_ds(ds) for ds in in_data_sets], + "file_dep": [doit_config.dsm.cookie_for_ds(ds) for ds in in_data_sets], "actions": [action], - "targets": [_dsm.cookie_for_ds(out_data_set)], + "targets": [doit_config.dsm.cookie_for_ds(out_data_set)], } -_data_sets[task_bom] = ["bom", ["components"]] +doit_config.set_data_sets_for(task_bom, "bom", "components") def order_csv(count: int, style: str, output_file: Path, out_ds: DataSet, data_sets): - ds = _dsm.load_data_sets(data_sets) + ds = doit_config.dsm.load_data_sets(data_sets) csv_ds = DataSet() @@ -196,18 +162,18 @@ def order_csv(count: int, style: str, output_file: Path, out_ds: DataSet, data_s fields = ["Digi-Key Part Number", refs_field, count_field, mpn_field] include_extra_fields = False - _dsm.store_csv(output_file, csv_ds, "row", order_by=mpn_field, fields=fields, - include_extra_fields=include_extra_fields) + doit_config.dsm.store_csv(output_file, csv_ds, "row", order_by=mpn_field, fields=fields, + include_extra_fields=include_extra_fields) def create_task_order_csv(*, style: str = None, output_file: Union[str, Path], out_data_set, data_sets, count: int = 1): def action(): - with _dsm.create_rw(out_data_set, clean=True) as out: + with doit_config.dsm.create_rw(out_data_set, clean=True) as out: order_csv(count, style, Path(output_file), out, data_sets) return { "name": "order-{}".format(count) if not style else "order-{}-{}".format(style, count), "actions": [action], - "file_dep": [_dsm.cookie_for_ds(ds) for ds in data_sets], - "targets": [_dsm.cookie_for_ds(out_data_set), output_file], + "file_dep": [doit_config.dsm.cookie_for_ds(ds) for ds in data_sets], + "targets": [doit_config.dsm.cookie_for_ds(out_data_set), output_file], } diff --git a/src/ee/doit.py b/src/ee/doit.py index dc89fae..c0a01ea 100644 --- a/src/ee/doit.py +++ b/src/ee/doit.py @@ -33,7 +33,10 @@ class DoItConfig(object): return self._dsm def data_sets_for(self, task): - return self._data_sets[task] + try: + return self._data_sets[task] + except KeyError: + raise KeyError("No such task registered in this module: {}".format(task)) def out_data_set_for(self, task): return self.data_sets_for(task)[0] @@ -42,4 +45,8 @@ class DoItConfig(object): return self.data_sets_for(task)[1] def set_data_sets_for(self, task, out_dataset: str, *in_datasets: str): - self._data_sets[task] = [out_dataset, in_datasets] + self._data_sets[task] = [out_dataset, list(in_datasets)] + + def change_data_sets_for_task(self, task, _callable): + ds = self._data_sets[task] + ds[1] = _callable(ds[1]) diff --git a/src/ee/kicad/doit.py b/src/ee/kicad/doit.py index bf0ee3a..c881c70 100644 --- a/src/ee/kicad/doit.py +++ b/src/ee/kicad/doit.py @@ -7,7 +7,7 @@ from configclass import Config import ee.kicad import ee.kicad.pcb -from ee.ds import DataSetManager +from ee.doit import DoItConfig logger = logging.getLogger(__name__) @@ -18,29 +18,12 @@ _config_template = Config({ "gerber_zip": None, }) -_config = None # type: Mapping[str, str] - -_dsm = None # type: DataSetManager - -_data_sets = {} - - -def change_data_sets_for_task(task, _callable): - _data_sets[task][1] = _callable(_data_sets[task][1]) - - -def output_data_set_for_task(task): - return _data_sets[task][0] - - -def input_data_sets_for_task(task): - return _data_sets[task][1] +doit_config = DoItConfig() +_config = None # type: Mapping[str, str] -def init(data_set_manager: DataSetManager, **kwargs): - global _dsm - _dsm = data_set_manager +def init(**kwargs): global _config _config = _config_template.make(kwargs) @@ -84,14 +67,14 @@ def task_kicad_gerber(): def task_kicad_sch_to_data_set(): - out_data_set, in_data_sets = _data_sets[task_kicad_sch_to_data_set] + out_data_set, in_data_sets = doit_config.data_sets_for(task_kicad_sch_to_data_set) sch = _config["sch"] def action(): from ee.kicad.model import ComponentField - with _dsm.create_rw(out_data_set, clean=True) as ds: + with doit_config.dsm.create_rw(out_data_set, clean=True) as ds: schematics = ee.kicad.read_schematics(sch) for c in [c for c in schematics.components]: o = ds.create_object("kicad-schematic-component", c.timestamp) @@ -108,26 +91,26 @@ def task_kicad_sch_to_data_set(): o.set("field-{}".format(f.name), str(f.value)) return { - "file_dep": [Path(sch)] + [_dsm.cookie_for_ds(ds) for ds in in_data_sets], + "file_dep": [Path(sch)] + [doit_config.dsm.cookie_for_ds(ds) for ds in in_data_sets], "actions": [action], - "targets": [_dsm.cookie_for_ds(out_data_set)], + "targets": [doit_config.dsm.cookie_for_ds(out_data_set)], } -_data_sets[task_kicad_sch_to_data_set] = ["kicad-sch", []] +doit_config.set_data_sets_for(task_kicad_sch_to_data_set, "kicad-sch") def task_kicad_pcb_to_data_set(): kicad_pcb = _config["kicad_pcb"] - out_data_set, in_data_sets = _data_sets[task_kicad_pcb_to_data_set] + out_data_set, in_data_sets = doit_config.data_sets_for(task_kicad_pcb_to_data_set) def action(): from ee.kicad.pcb import KicadPcb, Module logger.debug("Parsing PCB {}".format(kicad_pcb)) - with _dsm.create_rw(out_data_set, clean=True) as ds: + with doit_config.dsm.create_rw(out_data_set, clean=True) as ds: # [ds.delete(o) for o in ds.items(object_type="kicad-pcb-component")] pcb = ee.kicad.pcb.parse(kicad_pcb) # type: KicadPcb @@ -146,20 +129,21 @@ def task_kicad_pcb_to_data_set(): o.set("layer", m.layer) return { - "file_dep": [Path(kicad_pcb)] + [_dsm.cookie_for_ds(ds) for ds in in_data_sets], + "file_dep": [Path(kicad_pcb)] + [doit_config.dsm.cookie_for_ds(ds) for ds in in_data_sets], "actions": [action], - "targets": [_dsm.cookie_for_ds(out_data_set)], + "targets": [doit_config.dsm.cookie_for_ds(out_data_set)], } -_data_sets[task_kicad_pcb_to_data_set] = ["kicad-pcb", []] +doit_config.set_data_sets_for(task_kicad_pcb_to_data_set, "kicad-pcb") def task_kicad_create_component_data_set(): - out_data_set, in_data_sets = _data_sets[task_kicad_create_component_data_set] + out_data_set, in_data_sets = doit_config.data_sets_for(task_kicad_create_component_data_set) def action(): - in_ds = _dsm.load_data_sets(in_data_sets) + in_ds = doit_config.dsm.load_data_sets(in_data_sets) + # for o in in_ds.items(): # logger.info("item: {}/{}".format(o.object_type.name, o.key)) @@ -174,7 +158,7 @@ def task_kicad_create_component_data_set(): return footprint - with _dsm.create_rw(out_data_set, clean=True) as output: + with doit_config.dsm.create_rw(out_data_set, clean=True) as output: kicad_sch = [o for o in in_ds.items() if o.object_type.name == "kicad-schematic-component"] logger.info("processing {} kicad-sch".format(len(kicad_sch))) @@ -222,17 +206,16 @@ def task_kicad_create_component_data_set(): # c.set("footprint", pcb.) return { - "file_dep": [_dsm.cookie_for_ds(ds) for ds in in_data_sets], + "file_dep": [doit_config.dsm.cookie_for_ds(ds) for ds in in_data_sets], "actions": [action], - "targets": [_dsm.cookie_for_ds(out_data_set)], + "targets": [doit_config.dsm.cookie_for_ds(out_data_set)], } -_data_sets[task_kicad_create_component_data_set] = ["components", ["kicad-sch", "kicad-pcb"]] +doit_config.set_data_sets_for(task_kicad_create_component_data_set, "components", "kicad-sch", "kicad-pcb") __all__ = [ init.__name__, - change_data_sets_for_task.__name__, task_kicad_create_component_data_set.__name__, task_kicad_gerber.__name__, diff --git a/test/doit/test_doit.py b/test/doit/test_doit.py index 2f6f035..969d75c 100644 --- a/test/doit/test_doit.py +++ b/test/doit/test_doit.py @@ -39,10 +39,11 @@ def test_doit(tmpdir, caplog): dsm.add_ds("csv", "kicad-footprint", "kicad-footprint-mapping", path=schematics_dir / "ee" / "kicad-footprint.csv") - ee.kicad.doit.change_data_sets_for_task(task_kicad_create_component_data_set, lambda ds: ds + ["kicad-footprint"]) + ee.kicad.doit.doit_config.configure(data_set_manager=dsm) + ee.kicad.doit.doit_config.change_data_sets_for_task(task_kicad_create_component_data_set, + lambda ds: ds + ["kicad-footprint"]) - ee.kicad.doit.init(data_set_manager=dsm, - sch=schematics_dir / "schematic-1.sch", + ee.kicad.doit.init(sch=schematics_dir / "schematic-1.sch", kicad_pcb=schematics_dir / "schematic-1.kicad_pcb",) ee.digikey.doit.doit_config.configure(data_set_manager=dsm) -- cgit v1.2.3