From 97c8bb9db96e27051f8746865f657408263db0b8 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 26 Feb 2019 23:08:19 +0100 Subject: o Creating a PartDb that manages a file system directory with one xml file per part. o Switching xml-based code to use PartDb. --- src/ee/kicad/make_bom.py | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) (limited to 'src/ee/kicad/make_bom.py') diff --git a/src/ee/kicad/make_bom.py b/src/ee/kicad/make_bom.py index 2f5a2ac..00e9ae3 100644 --- a/src/ee/kicad/make_bom.py +++ b/src/ee/kicad/make_bom.py @@ -1,4 +1,5 @@ -import sys +import pydoc +from pathlib import Path from typing import Optional, List, Callable, Mapping from xml.dom import minidom from xml.etree import ElementTree @@ -7,7 +8,7 @@ from ee import EeException from ee.kicad.model import Component from ee.kicad.read_schematic import read_schematics from ee.kicad.to_bom import to_bom, to_bom_xml -from ee.tools import mk_parents +from ee.part import PartDb, save_db from ee.xml import bomFile, uris __all__ = [ @@ -97,7 +98,7 @@ class MakeBomStrategy(): return apply_strategies(component, part, self.default_strategies) -def work(sch, out_file, strategy: MakeBomStrategy, new_mode, pretty): +def work(sch, out: Path, strategy: MakeBomStrategy, new_mode, pretty): if not new_mode: bom = to_bom_xml(sch) xml = ElementTree.tostring(bom, encoding="unicode") @@ -105,13 +106,9 @@ def work(sch, out_file, strategy: MakeBomStrategy, new_mode, pretty): if pretty: xml = minidom.parseString(xml).toprettyxml(indent=" ") - print(xml, file=out_file) + print(xml) else: - file = bomFile.BomFile() - - parts = bomFile.PartList() - file.partsProp = parts - + parts = PartDb() components = to_bom(sch) for c in components: part = bomFile.Part(id=c.ref) @@ -124,16 +121,14 @@ def work(sch, out_file, strategy: MakeBomStrategy, new_mode, pretty): part.part_numbersProp = None if part is not None: - parts.add_part(part) + parts.add_entry(part, True) - file.export(out_file, 0, name_="bom-file", namespacedef_="xmlns='http://purl.org/ee/bom-file'", - pretty_print=pretty) + save_db(out, parts) -def make_bom(sch_file: str, out_file: Optional[str], strategy_name: str, new_mode: bool, pretty: bool): - sch = read_schematics(sch_file) +def make_bom(sch_file: Path, out_dir: Path, strategy_name: str, new_mode: bool, pretty: bool): + sch = read_schematics(str(sch_file)) - import pydoc make_bom_strategy_factory = pydoc.locate(strategy_name) if not callable(make_bom_strategy_factory): @@ -144,9 +139,4 @@ def make_bom(sch_file: str, out_file: Optional[str], strategy_name: str, new_mod if not isinstance(make_bom_strategy, MakeBomStrategy): raise EeException("Not a MakeBomStrategy: {}, is a {}".format(strategy_name, type(make_bom_strategy))) - if out_file: - mk_parents(out_file) - with open(out_file, "w") as f: - work(sch, f, make_bom_strategy, new_mode, pretty) - else: - work(sch, sys.stdout, make_bom_strategy, new_mode, pretty) + work(sch, out_dir, make_bom_strategy, new_mode, pretty) -- cgit v1.2.3