From 3523190bb7ca1c38caea3a1aae51062d22e56b09 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 15 Mar 2019 10:55:06 +0100 Subject: o Switching PartDb to serialize into many xml files into a single document. --- src/ee/part/__init__.py | 56 +++++++-------------------- src/ee/part/create_distributor_search_list.py | 8 ++-- 2 files changed, 17 insertions(+), 47 deletions(-) (limited to 'src/ee/part') diff --git a/src/ee/part/__init__.py b/src/ee/part/__init__.py index 27b6619..45561ec 100644 --- a/src/ee/part/__init__.py +++ b/src/ee/part/__init__.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import List, MutableMapping, Optional, Iterator from ee import EeException -from ee.xml import types +from ee.xml import types, bom_file_utils from ee.xml.bom_file_utils import find_pn, find_dpn, find_root_tag __all__ = [ @@ -62,52 +62,24 @@ class PartDb(object): return idx[pn].part -def load_db(dir_path: Path) -> PartDb: +def load_db(path: Path) -> PartDb: db = PartDb() - for file in dir_path.iterdir(): - if not file.is_file() or not file.name.endswith(".xml") or file.name == "index.xml": - continue + with path.open("r") as f: + part_db: types.PartDb = types.parse(f, silence=True) - part = types.parse(str(file), silence=True) # type: types.Part - db.add_entry(part, False) + part_db.partsProp = part_db.partsProp or types.PartList() - return db - - -def save_db(dir_path: Path, db: PartDb): - if dir_path.exists(): - if not dir_path.is_dir(): - raise EeException("The given db path is not a directory") - - idx_path = dir_path / "index.xml" - if not idx_path.is_file(): - # Ninja creates the parent directories out the output.. - if len(list(dir_path.iterdir())) > 0: - raise EeException("The given db directory exists, but does not look like a part db dir") + for p in part_db.partsProp.part: + db.add_entry(p, False) - for p in dir_path.iterdir(): - if not p.is_file(): - raise EeException("Non-file: {}".format(p)) - p.unlink() - dir_path.rmdir() - - dir_path.mkdir(parents=True, exist_ok=True) - - idx = types.IndexFile() - idx.filesProp = types.FileList() - files = idx.filesProp.fileProp - - parts = db.iterparts() - parts = sorted(parts, key=lambda p: p.idProp) + return db - for part in parts: - id_ = part.id - path = dir_path / "{}.xml".format(id_.replace("/", "_")) - with path.open("w") as f: - part.export(outfile=f, level=0, name_=find_root_tag(part)) - files.append(types.File(path=str(path))) +def save_db(path: Path, db: PartDb): + part_db = types.PartDb() + parts = part_db.parts = types.PartList() + parts.partProp.extend(db.iterparts(sort=True)) - with (dir_path / "index.xml").open("w") as f: - idx.export(f, level=0, name_=find_root_tag(idx)) + with path.open("w") as f: + part_db.export(outfile=f, level=0, name_=find_root_tag(part_db)) diff --git a/src/ee/part/create_distributor_search_list.py b/src/ee/part/create_distributor_search_list.py index bd5f69e..88e5a10 100644 --- a/src/ee/part/create_distributor_search_list.py +++ b/src/ee/part/create_distributor_search_list.py @@ -7,10 +7,8 @@ from ee.xml.bom_file_utils import * __all__ = ["create_distributor_search_list"] -def create_distributor_search_list(in_dir: Path, out_dir: Path): - print("in: {}, out: {}".format(in_dir, out_dir)) - - in_parts = load_db(in_dir) +def create_distributor_search_list(in_path: Path, out_path: Path): + in_parts = load_db(in_path) out_parts = PartDb() print("loaded {} existing parts".format(in_parts.size())) @@ -33,4 +31,4 @@ def create_distributor_search_list(in_dir: Path, out_dir: Path): out_parts.add_entry(new_part, True) print("Saving {} work parts".format(out_parts.size())) - save_db(out_dir, out_parts) + save_db(out_path, out_parts) -- cgit v1.2.3