aboutsummaryrefslogtreecommitdiff
path: root/src/ee/part
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-03-15 10:55:06 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2019-03-15 10:55:06 +0100
commit3523190bb7ca1c38caea3a1aae51062d22e56b09 (patch)
treedb975c27c38c873a012d88a266ad82621b0859b2 /src/ee/part
parentb67aa2b41247991e361dec0963670b4e5108410a (diff)
downloadee-python-3523190bb7ca1c38caea3a1aae51062d22e56b09.tar.gz
ee-python-3523190bb7ca1c38caea3a1aae51062d22e56b09.tar.bz2
ee-python-3523190bb7ca1c38caea3a1aae51062d22e56b09.tar.xz
ee-python-3523190bb7ca1c38caea3a1aae51062d22e56b09.zip
o Switching PartDb to serialize into many xml files into a single
<part-db> document.
Diffstat (limited to 'src/ee/part')
-rw-r--r--src/ee/part/__init__.py56
-rw-r--r--src/ee/part/create_distributor_search_list.py8
2 files changed, 17 insertions, 47 deletions
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)