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(). --- src/ee/bom/doit.py | 66 +++++++++++++----------------------------------------- 1 file changed, 16 insertions(+), 50 deletions(-) (limited to 'src/ee/bom') 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], } -- cgit v1.2.3