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. --- Makefile | 6 +- src/ee/digikey/import_parts.py | 100 -- src/ee/digikey/refresh_parts.py | 97 -- src/ee/digikey/search_parts.py | 94 ++ src/ee/kicad/make_bom.py | 32 +- src/ee/part/__init__.py | 103 ++ src/ee/part/create_distributor_search_list.py | 33 + src/ee/tools/digikey_import_parts.py | 17 - src/ee/tools/digikey_refresh_parts.py | 22 - src/ee/tools/digikey_search_parts.py | 22 + src/ee/tools/kicad_make_bom.py | 3 +- .../tools/part_create_distributor_search_list.py | 19 + src/ee/xml/bomFile.py | 5 +- src/ee/xml/bom_file_utils.py | 13 +- src/ee/xml/catalogFile.py | 1105 -------------------- src/ee/xml/indexFile.py | 1101 +++++++++++++++++++ xsd/ee-bom.xsd | 1 + xsd/ee-catalog.xsd | 26 - xsd/ee-index.xsd | 24 + 19 files changed, 1427 insertions(+), 1396 deletions(-) delete mode 100644 src/ee/digikey/import_parts.py delete mode 100644 src/ee/digikey/refresh_parts.py create mode 100644 src/ee/digikey/search_parts.py create mode 100644 src/ee/part/__init__.py create mode 100644 src/ee/part/create_distributor_search_list.py delete mode 100644 src/ee/tools/digikey_import_parts.py delete mode 100644 src/ee/tools/digikey_refresh_parts.py create mode 100644 src/ee/tools/digikey_search_parts.py create mode 100644 src/ee/tools/part_create_distributor_search_list.py delete mode 100644 src/ee/xml/catalogFile.py create mode 100644 src/ee/xml/indexFile.py delete mode 100644 xsd/ee-catalog.xsd create mode 100644 xsd/ee-index.xsd diff --git a/Makefile b/Makefile index 79ff345..c648bca 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -XSDS = xsd/ee-bom.xsd xsd/ee-catalog.xsd -XSD_PYS = src/ee/xml/bomFile.py src/ee/xml/catalogFile.py +XSDS = xsd/ee-bom.xsd xsd/ee-index.xsd +XSD_PYS = src/ee/xml/bomFile.py src/ee/xml/indexFile.py all: env/pip.cookie $(XSD_PYS) @@ -19,8 +19,8 @@ src/ee/xml/%File.py: xsd/ee-%.xsd -f \ --no-dates \ --no-versions \ - --one-file-per-xsd \ --output-directory=src/ee/xml \ + -o $@ \ -m $< $(XSDS): env/pip.cookie src/ee/xml/__init__.py diff --git a/src/ee/digikey/import_parts.py b/src/ee/digikey/import_parts.py deleted file mode 100644 index 748bbef..0000000 --- a/src/ee/digikey/import_parts.py +++ /dev/null @@ -1,100 +0,0 @@ -import os -from typing import List, MutableMapping - -from ee.xml import bomFile -from ee.xml.bom_file_utils import * -from ee.xml.uris import DIGIKEY_URI - -__all__ = ["import_parts"] - - -class Entry(object): - def __init__(self, new: bool, part: bomFile.Part): - self.new = new - self.part = part - - self.pn = find_pn(part) - self.dpn = find_dpn(part, DIGIKEY_URI) - - -def import_parts(in_path, out_path): - print("in: {}, out: {}".format(in_path, out_path)) - - in_file = bomFile.parse(in_path, True) - if in_file.partsProp is None: - in_file.partsProp = bomFile.PartList() - in_part_list = in_file.partsProp.partProp # type: List[bomFile.Part] - - print("in file: {} parts".format(len(in_part_list))) - - if os.path.isfile(out_path): - out_file = bomFile.parse(out_path, True) - else: - out_file = bomFile.BomFile() - - if out_file.partsProp is None: - out_file.partsProp = bomFile.PartList() - out_part_list = out_file.partsProp.partProp # type: List[bomFile.Part] - print("out file: {} parts".format(len(out_part_list))) - - existing_parts = [] # type: List[Entry] - pn_index = {} # type: MutableMapping[str, Entry] - dpn_index = {} # type: MutableMapping[str, Entry] - new_entry_added = 0 - - def add_entry(e: Entry): - existing_parts.append(e) - pn_index[e.pn] = e - dpn_index[e.dpn] = e - - if e.new: - out_part_list.append(e.part) - nonlocal new_entry_added - new_entry_added = new_entry_added + 1 - - print("len(out_part_list)={}".format(len(out_part_list))) - for part in out_part_list: # type: bomFile.Part - entry = Entry(False, part) - add_entry(entry) - - print("loaded {} existing parts".format(len(existing_parts))) - - for part in in_part_list: - pn_value = find_pn(part) - - if pn_value is None: - print("Skipping part with no part number: id={}".format(part.idProp)) - continue - - entry = pn_index.get(pn_value) - - if entry is not None: - print("Already imported pn_value={}".format(pn_value)) - continue - - print("Importing {}".format(pn_value)) - - pns = bomFile.PartNumberList() - - if pn_value is not None: - pns.add_part_number(bomFile.PartNumber(value=pn_value)) - - dpn_value = find_dpn(part, DIGIKEY_URI) - if dpn_value is not None: - pns.add_part_number(bomFile.PartNumber(value=dpn_value, distributor=DIGIKEY_URI)) - - if len(pns.part_numberProp) == 0: - continue - - new_part = bomFile.Part(part_numbers=pns) - entry = Entry(True, new_part) - add_entry(entry) - - if new_entry_added: - print("Imported {} entries".format(new_entry_added)) - tmp_path = out_path + ".tmp" - with open(tmp_path, "w") as f: - out_file.export(f, 0, name_="bom-file") - os.rename(tmp_path, out_path) - else: - print("no new entries") diff --git a/src/ee/digikey/refresh_parts.py b/src/ee/digikey/refresh_parts.py deleted file mode 100644 index 87edf2f..0000000 --- a/src/ee/digikey/refresh_parts.py +++ /dev/null @@ -1,97 +0,0 @@ -import os -from pathlib import Path -from typing import List - -from ee.digikey import Digikey, DigikeyParser, DigikeyClient, SearchResponseTypes, DigikeyProduct -from ee.xml import bomFile, bom_file_utils -from ee.xml.bomFile import DigikeyDistributorInfo -from ee.xml.uris import DIGIKEY_URI - -__all__ = ["refresh_parts"] - - -def resolved(di: DigikeyDistributorInfo, part: bomFile.Part, p: DigikeyProduct): - di.stateProp = "resolved" - - fact_set = bom_file_utils.find_fact_set(part, DIGIKEY_URI, create=True) - - # Remove the old list - fact_set.factsProp = bomFile.FactList() - facts: List[bomFile.Fact] = fact_set.factsProp.factProp - - for a in p.attributes: - facts.append(bomFile.Fact(key=a.attribute_type.id, label=a.attribute_type.label, value=a.value)) - - -def refresh_parts(in_path: Path, out_path: Path, cache_dir: Path, force_refresh: bool): - print("in: {}, out: {}".format(in_path, out_path)) - - in_file = bomFile.parse(str(in_path), True) - if in_file.partsProp is None: - in_file.partsProp = bomFile.PartList() - - parser = DigikeyParser(Digikey()) - client = DigikeyClient(cache_dir) - - for part in in_file.partsProp.partProp: # type: bomFile.Part - dpn = bom_file_utils.find_dpn(part, DIGIKEY_URI) - mpn = bom_file_utils.find_pn(part) - - is_mpn = query = None - - if dpn is not None: - query = dpn - is_mpn = False - elif mpn is not None: - query = mpn - is_mpn = True - - if query is None: - print("could not find pn or dpn: part.id={}".format(part.idProp)) - continue - - di = part.distributor_infoProp # type: DigikeyDistributorInfo - - if di is None: - di = bomFile.DigikeyDistributorInfo() - di.extensiontype_ = "DigikeyDistributorInfo" - di.original_tagname_ = "distributor-info" - part.distributor_infoProp = di - - if force_refresh or di.stateProp != "resolved": - text = client.search(query) - response = parser.parse_string(text) - - if response.response_type == SearchResponseTypes.SINGLE: - resolved(di, part, response.products[0]) - elif response.response_type == SearchResponseTypes.MANY: - - # find those with an exact match. Digikey uses a prefix search so a query for "FOO" will return "FOO" - # and "FOOT". - def get_field(p): - return p.mpn if is_mpn else p.part_number - - filtered_products = [p for p in response.products if get_field(p) == query] - - if len(filtered_products) == 0: - di.stateProp = "not-found" - else: - dpn = sorted(filtered_products, key=lambda p: p.part_number)[0].part_number - - response = parser.parse_string(client.search(dpn)) - if response.response_type == SearchResponseTypes.SINGLE: - resolved(di, part, response.products[0]) - else: - di.stateProp = "many" - - elif response.response_type == SearchResponseTypes.TOO_MANY: - di.stateProp = "too-many" - elif response.response_type == SearchResponseTypes.NO_MATCHES: - di.stateProp = "not-found" - - out_path = in_path - out_file = in_file - tmp_path = str(out_path) + ".tmp" - with open(tmp_path, "w") as f: - out_file.export(f, 0, name_="bom-file") - os.rename(tmp_path, str(out_path)) diff --git a/src/ee/digikey/search_parts.py b/src/ee/digikey/search_parts.py new file mode 100644 index 0000000..62bb4ee --- /dev/null +++ b/src/ee/digikey/search_parts.py @@ -0,0 +1,94 @@ +from pathlib import Path +from typing import List + +from ee.digikey import Digikey, DigikeyParser, DigikeyClient, SearchResponseTypes, DigikeyProduct +from ee.part import PartDb, load_db, save_db +from ee.xml import bomFile, bom_file_utils +from ee.xml.bomFile import DigikeyDistributorInfo +from ee.xml.uris import DIGIKEY_URI + +__all__ = ["search_parts"] + + +def resolved(di: DigikeyDistributorInfo, part: bomFile.Part, p: DigikeyProduct): + di.stateProp = "resolved" + + fact_set = bom_file_utils.find_fact_set(part, DIGIKEY_URI, create=True) + + # Remove the old list + fact_set.factsProp = bomFile.FactList() + facts: List[bomFile.Fact] = fact_set.factsProp.factProp + + for a in p.attributes: + facts.append(bomFile.Fact(key=a.attribute_type.id, label=a.attribute_type.label, value=a.value)) + + +def search_parts(in_dir: Path, out_dir: Path, cache_dir: Path, force_refresh: bool): + print("in: {}, out: {}".format(in_dir, out_dir)) + + in_db = load_db(in_dir) + out_parts = PartDb() + + parser = DigikeyParser(Digikey()) + client = DigikeyClient(cache_dir) + + for part in in_db.iterparts(): + dpn = bom_file_utils.find_dpn(part, DIGIKEY_URI) + mpn = bom_file_utils.find_pn(part) + + is_mpn = query = None + + if dpn is not None: + query = dpn + is_mpn = False + elif mpn is not None: + query = mpn + is_mpn = True + + if query is None: + print("could not find pn or dpn: part.id={}".format(part.idProp)) + continue + + di = part.distributor_infoProp # type: DigikeyDistributorInfo + + if di is None: + di = bomFile.DigikeyDistributorInfo() + di.extensiontype_ = "DigikeyDistributorInfo" + di.original_tagname_ = "distributor-info" + part.distributor_infoProp = di + + if force_refresh or di.stateProp != "resolved": + text = client.search(query) + response = parser.parse_string(text) + + if response.response_type == SearchResponseTypes.SINGLE: + resolved(di, part, response.products[0]) + elif response.response_type == SearchResponseTypes.MANY: + + # find those with an exact match. Digikey uses a prefix search so a query for "FOO" will return "FOO" + # and "FOOT". + def get_field(p): + return p.mpn if is_mpn else p.part_number + + filtered_products = [p for p in response.products if get_field(p) == query] + + if len(filtered_products) == 0: + di.stateProp = "not-found" + else: + dpn = sorted(filtered_products, key=lambda p: p.part_number)[0].part_number + + response = parser.parse_string(client.search(dpn)) + if response.response_type == SearchResponseTypes.SINGLE: + resolved(di, part, response.products[0]) + else: + di.stateProp = "many" + + elif response.response_type == SearchResponseTypes.TOO_MANY: + di.stateProp = "too-many" + elif response.response_type == SearchResponseTypes.NO_MATCHES: + di.stateProp = "not-found" + + out_parts.add_entry(part, True) + + print("Saving {} work parts".format(out_parts.size())) + save_db(out_dir, out_parts) 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) diff --git a/src/ee/part/__init__.py b/src/ee/part/__init__.py new file mode 100644 index 0000000..84a6174 --- /dev/null +++ b/src/ee/part/__init__.py @@ -0,0 +1,103 @@ +from pathlib import Path +from typing import List, MutableMapping, Optional, Iterator + +from ee import EeException +from ee.xml import bomFile, indexFile +from ee.xml.bom_file_utils import find_pn, find_dpn, find_root_tag + +__all__ = [ + "PartDb", + "load_db", + "save_db", +] + + +class Entry(object): + def __init__(self, new: bool, part: bomFile.Part): + self.new = new + self.part = part + + self.pn = find_pn(part) + + def dpn(self, uri: str): + return find_dpn(self.part, uri) + + +class PartDb(object): + def __init__(self): + self.parts = [] # type: List[Entry] + self.pn_index = {} # type: MutableMapping[str, Entry] + self.dpn_indexes = {} # type: MutableMapping[str, MutableMapping[str, Entry]] + self.new_entries = 0 + + def add_entry(self, part: bomFile.Part, new: bool): + e = Entry(new, part) + self.parts.append(e) + + if e.pn: + self.pn_index[e.pn] = e + + if e.new: + self.new_entries = self.new_entries + 1 + + def iterparts(self) -> Iterator[bomFile.Part]: + return [e.part for e in self.parts] + + def size(self) -> int: + return len(self.parts) + + def find_by_pn(self, pn: str) -> Optional[bomFile.Part]: + entry = self.pn_index.get(pn, None) + return entry.part if entry else None + + def find_by_dpn(self, distributor: str, pn: str) -> bomFile.Part: + idx = self.dpn_indexes.get(distributor) + + if idx is None: + tmp = [(find_dpn(entry.part, distributor), entry) for entry in self.parts] + idx = {dpn: entry for dpn, entry in tmp if dpn is not None} + self.dpn_indexes[distributor] = idx + + return idx[pn].part + + +def load_db(dir_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 + + part = bomFile.parse(str(file), silence=True) # type: bomFile.Part + db.add_entry(part, False) + + 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(): + raise EeException("The given db directory exists, but does not look like a part db dir") + + dir_path.rmdir() + + dir_path.mkdir(parents=True, exist_ok=True) + + idx = indexFile.IndexFile() + idx.filesProp = indexFile.FileList() + files = idx.filesProp.fileProp + + for part in db.iterparts(): + id_ = part.id + path = dir_path / "{}.xml".format(id_) + with path.open("w") as f: + part.export(outfile=f, level=0, name_=find_root_tag(part)) + + files.append(indexFile.File(path=str(path))) + + with (dir_path / "index.xml").open("w") as f: + idx.export(f, level=0, name_=find_root_tag(idx)) diff --git a/src/ee/part/create_distributor_search_list.py b/src/ee/part/create_distributor_search_list.py new file mode 100644 index 0000000..ef91e1f --- /dev/null +++ b/src/ee/part/create_distributor_search_list.py @@ -0,0 +1,33 @@ +from pathlib import Path + +from ee.part import PartDb, load_db, save_db +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) + out_parts = PartDb() + + print("loaded {} existing parts".format(in_parts.size())) + + for part in in_parts.iterparts(): + pn_value = find_pn(part) + + if pn_value is None: + print("Skipping part with no part number: id={}".format(part.idProp)) + continue + + entry = out_parts.find_by_pn(pn_value) + + if entry is not None: + continue + + part.id = pn_value + out_parts.add_entry(part, True) + + print("Saving {} work parts".format(out_parts.size())) + save_db(out_dir, out_parts) diff --git a/src/ee/tools/digikey_import_parts.py b/src/ee/tools/digikey_import_parts.py deleted file mode 100644 index 77f87e8..0000000 --- a/src/ee/tools/digikey_import_parts.py +++ /dev/null @@ -1,17 +0,0 @@ -import argparse -from ee.digikey.import_parts import import_parts - -parser = argparse.ArgumentParser(description="Import all parts in XML file into Digi-Key parts list") - -parser.add_argument("--in", - dest="in_", - required=True, - metavar="FILE") - -parser.add_argument("--out", - required=True, - metavar="FILE") - -args = parser.parse_args() - -import_parts(args.in_, args.out) diff --git a/src/ee/tools/digikey_refresh_parts.py b/src/ee/tools/digikey_refresh_parts.py deleted file mode 100644 index e3e3b35..0000000 --- a/src/ee/tools/digikey_refresh_parts.py +++ /dev/null @@ -1,22 +0,0 @@ -import argparse -from pathlib import Path - -from ee.digikey.refresh_parts import refresh_parts - -parser = argparse.ArgumentParser() - -parser.add_argument("--in", - dest="in_", - required=True, - metavar="FILE") - -parser.add_argument("--out", - required=True, - metavar="FILE") - -args = parser.parse_args() - -cache_dir = ".ee/cache" -force = True - -refresh_parts(Path(args.in_), Path(args.out), Path(cache_dir), force) diff --git a/src/ee/tools/digikey_search_parts.py b/src/ee/tools/digikey_search_parts.py new file mode 100644 index 0000000..80393eb --- /dev/null +++ b/src/ee/tools/digikey_search_parts.py @@ -0,0 +1,22 @@ +import argparse +from pathlib import Path + +from ee.digikey.search_parts import search_parts + +parser = argparse.ArgumentParser() + +parser.add_argument("--in", + dest="in_", + required=True, + metavar="FILE") + +parser.add_argument("--out", + required=True, + metavar="FILE") + +args = parser.parse_args() + +cache_dir = ".ee/cache" +force = True + +search_parts(Path(args.in_), Path(args.out), Path(cache_dir), force) diff --git a/src/ee/tools/kicad_make_bom.py b/src/ee/tools/kicad_make_bom.py index 769ea0d..c4a1dd8 100644 --- a/src/ee/tools/kicad_make_bom.py +++ b/src/ee/tools/kicad_make_bom.py @@ -1,4 +1,5 @@ import argparse +from pathlib import Path from ee.kicad.make_bom import make_bom @@ -24,4 +25,4 @@ new_mode = True strategy = args.strategy if args.strategy else "ee.kicad.make_bom.MakeBomStrategy" -make_bom(args.sch, args.out, strategy, new_mode, pretty) +make_bom(Path(args.sch), Path(args.out), strategy, new_mode, pretty) diff --git a/src/ee/tools/part_create_distributor_search_list.py b/src/ee/tools/part_create_distributor_search_list.py new file mode 100644 index 0000000..22b4c3f --- /dev/null +++ b/src/ee/tools/part_create_distributor_search_list.py @@ -0,0 +1,19 @@ +import argparse +from pathlib import Path + +from ee.part.create_distributor_search_list import create_distributor_search_list + +parser = argparse.ArgumentParser() + +parser.add_argument("--in", + dest="in_", + required=True, + metavar="FILE") + +parser.add_argument("--out", + required=True, + metavar="FILE") + +args = parser.parse_args() + +create_distributor_search_list(Path(args.in_), Path(args.out)) diff --git a/src/ee/xml/bomFile.py b/src/ee/xml/bomFile.py index 5a78173..2e2b5a7 100644 --- a/src/ee/xml/bomFile.py +++ b/src/ee/xml/bomFile.py @@ -9,15 +9,15 @@ # ('-f', '') # ('--no-dates', '') # ('--no-versions', '') -# ('--one-file-per-xsd', '') # ('--output-directory', 'src/ee/xml') +# ('-o', 'src/ee/xml/bomFile.py') # ('-m', '') # # Command line arguments: # xsd/ee-bom.xsd # # Command line: -# env/bin/generateDS.py -f --no-dates --no-versions --one-file-per-xsd --output-directory="src/ee/xml" -m xsd/ee-bom.xsd +# env/bin/generateDS.py -f --no-dates --no-versions --output-directory="src/ee/xml" -o "src/ee/xml/bomFile.py" -m xsd/ee-bom.xsd # # Current working directory (os.getcwd()): # ee-python @@ -1765,6 +1765,7 @@ class DigikeyDistributorInfo(DistributorInfo): GDSClassesMapping = { 'bom-file': BomFile, + 'part': Part, } diff --git a/src/ee/xml/bom_file_utils.py b/src/ee/xml/bom_file_utils.py index f09a3dd..15f99b2 100644 --- a/src/ee/xml/bom_file_utils.py +++ b/src/ee/xml/bom_file_utils.py @@ -1,14 +1,23 @@ from typing import List, Optional -from ee.xml import bomFile +from ee.xml import bomFile, indexFile __all__ = [ - "part_numbers", + "find_root_tag", "find_pn", "find_dpn", ] +def find_root_tag(root): + tag = next((tag for tag, klass in bomFile.GDSClassesMapping.items() if klass == type(root)), None) + + if tag is not None: + return tag + + return next((tag for tag, klass in indexFile.GDSClassesMapping.items() if klass == type(root))) + + def part_numbers(part: bomFile.Part) -> List[bomFile.PartNumber]: pns = part.part_numbersProp # type: bomFile.PartNumberList diff --git a/src/ee/xml/catalogFile.py b/src/ee/xml/catalogFile.py deleted file mode 100644 index bda29ae..0000000 --- a/src/ee/xml/catalogFile.py +++ /dev/null @@ -1,1105 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -# -# Generated by generateDS.py. -# Python 3.7.2+ (default, Feb 2 2019, 14:31:48) [GCC 8.2.0] -# -# Command line options: -# ('-f', '') -# ('--no-dates', '') -# ('--no-versions', '') -# ('--one-file-per-xsd', '') -# ('--output-directory', 'src/ee/xml') -# ('-m', '') -# -# Command line arguments: -# xsd/ee-catalog.xsd -# -# Command line: -# env/bin/generateDS.py -f --no-dates --no-versions --one-file-per-xsd --output-directory="src/ee/xml" -m xsd/ee-catalog.xsd -# -# Current working directory (os.getcwd()): -# ee-python -# - -import sys -import re as re_ -import base64 -import datetime as datetime_ -import warnings as warnings_ -try: - from lxml import etree as etree_ -except ImportError: - from xml.etree import ElementTree as etree_ - - -Validate_simpletypes_ = True -if sys.version_info.major == 2: - BaseStrType_ = basestring -else: - BaseStrType_ = str - - -def parsexml_(infile, parser=None, **kwargs): - if parser is None: - # Use the lxml ElementTree compatible parser so that, e.g., - # we ignore comments. - try: - parser = etree_.ETCompatXMLParser() - except AttributeError: - # fallback to xml.etree - parser = etree_.XMLParser() - doc = etree_.parse(infile, parser=parser, **kwargs) - return doc - -def parsexmlstring_(instring, parser=None, **kwargs): - if parser is None: - # Use the lxml ElementTree compatible parser so that, e.g., - # we ignore comments. - try: - parser = etree_.ETCompatXMLParser() - except AttributeError: - # fallback to xml.etree - parser = etree_.XMLParser() - element = etree_.fromstring(instring, parser=parser, **kwargs) - return element - -# -# Namespace prefix definition table (and other attributes, too) -# -# The module generatedsnamespaces, if it is importable, must contain -# a dictionary named GeneratedsNamespaceDefs. This Python dictionary -# should map element type names (strings) to XML schema namespace prefix -# definitions. The export method for any class for which there is -# a namespace prefix definition, will export that definition in the -# XML representation of that element. See the export method of -# any generated element type class for a example of the use of this -# table. -# A sample table is: -# -# # File: generatedsnamespaces.py -# -# GenerateDSNamespaceDefs = { -# "ElementtypeA": "http://www.xxx.com/namespaceA", -# "ElementtypeB": "http://www.xxx.com/namespaceB", -# } -# - -try: - from generatedsnamespaces import GenerateDSNamespaceDefs as GenerateDSNamespaceDefs_ -except ImportError: - GenerateDSNamespaceDefs_ = {} - -# -# The root super-class for element type classes -# -# Calls to the methods in these classes are generated by generateDS.py. -# You can replace these methods by re-implementing the following class -# in a module named generatedssuper.py. - -try: - from generatedssuper import GeneratedsSuper -except ImportError as exp: - - class GeneratedsSuper(object): - tzoff_pattern = re_.compile(r'(\+|-)((0\d|1[0-3]):[0-5]\d|14:00)$') - class _FixedOffsetTZ(datetime_.tzinfo): - def __init__(self, offset, name): - self.__offset = datetime_.timedelta(minutes=offset) - self.__name = name - def utcoffset(self, dt): - return self.__offset - def tzname(self, dt): - return self.__name - def dst(self, dt): - return None - def gds_format_string(self, input_data, input_name=''): - return input_data - def gds_validate_string(self, input_data, node=None, input_name=''): - if not input_data: - return '' - else: - return input_data - def gds_format_base64(self, input_data, input_name=''): - return base64.b64encode(input_data) - def gds_validate_base64(self, input_data, node=None, input_name=''): - return input_data - def gds_format_integer(self, input_data, input_name=''): - return '%d' % input_data - def gds_validate_integer(self, input_data, node=None, input_name=''): - return input_data - def gds_format_integer_list(self, input_data, input_name=''): - return '%s' % ' '.join(input_data) - def gds_validate_integer_list( - self, input_data, node=None, input_name=''): - values = input_data.split() - for value in values: - try: - int(value) - except (TypeError, ValueError): - raise_parse_error(node, 'Requires sequence of integers') - return values - def gds_format_float(self, input_data, input_name=''): - return ('%.15f' % input_data).rstrip('0') - def gds_validate_float(self, input_data, node=None, input_name=''): - return input_data - def gds_format_float_list(self, input_data, input_name=''): - return '%s' % ' '.join(input_data) - def gds_validate_float_list( - self, input_data, node=None, input_name=''): - values = input_data.split() - for value in values: - try: - float(value) - except (TypeError, ValueError): - raise_parse_error(node, 'Requires sequence of floats') - return values - def gds_format_double(self, input_data, input_name=''): - return '%e' % input_data - def gds_validate_double(self, input_data, node=None, input_name=''): - return input_data - def gds_format_double_list(self, input_data, input_name=''): - return '%s' % ' '.join(input_data) - def gds_validate_double_list( - self, input_data, node=None, input_name=''): - values = input_data.split() - for value in values: - try: - float(value) - except (TypeError, ValueError): - raise_parse_error(node, 'Requires sequence of doubles') - return values - def gds_format_boolean(self, input_data, input_name=''): - return ('%s' % input_data).lower() - def gds_validate_boolean(self, input_data, node=None, input_name=''): - return input_data - def gds_format_boolean_list(self, input_data, input_name=''): - return '%s' % ' '.join(input_data) - def gds_validate_boolean_list( - self, input_data, node=None, input_name=''): - values = input_data.split() - for value in values: - if value not in ('true', '1', 'false', '0', ): - raise_parse_error( - node, - 'Requires sequence of booleans ' - '("true", "1", "false", "0")') - return values - def gds_validate_datetime(self, input_data, node=None, input_name=''): - return input_data - def gds_format_datetime(self, input_data, input_name=''): - if input_data.microsecond == 0: - _svalue = '%04d-%02d-%02dT%02d:%02d:%02d' % ( - input_data.year, - input_data.month, - input_data.day, - input_data.hour, - input_data.minute, - input_data.second, - ) - else: - _svalue = '%04d-%02d-%02dT%02d:%02d:%02d.%s' % ( - input_data.year, - input_data.month, - input_data.day, - input_data.hour, - input_data.minute, - input_data.second, - ('%f' % (float(input_data.microsecond) / 1000000))[2:], - ) - if input_data.tzinfo is not None: - tzoff = input_data.tzinfo.utcoffset(input_data) - if tzoff is not None: - total_seconds = tzoff.seconds + (86400 * tzoff.days) - if total_seconds == 0: - _svalue += 'Z' - else: - if total_seconds < 0: - _svalue += '-' - total_seconds *= -1 - else: - _svalue += '+' - hours = total_seconds // 3600 - minutes = (total_seconds - (hours * 3600)) // 60 - _svalue += '{0:02d}:{1:02d}'.format(hours, minutes) - return _svalue - @classmethod - def gds_parse_datetime(cls, input_data): - tz = None - if input_data[-1] == 'Z': - tz = GeneratedsSuper._FixedOffsetTZ(0, 'UTC') - input_data = input_data[:-1] - else: - results = GeneratedsSuper.tzoff_pattern.search(input_data) - if results is not None: - tzoff_parts = results.group(2).split(':') - tzoff = int(tzoff_parts[0]) * 60 + int(tzoff_parts[1]) - if results.group(1) == '-': - tzoff *= -1 - tz = GeneratedsSuper._FixedOffsetTZ( - tzoff, results.group(0)) - input_data = input_data[:-6] - time_parts = input_data.split('.') - if len(time_parts) > 1: - micro_seconds = int(float('0.' + time_parts[1]) * 1000000) - input_data = '%s.%s' % ( - time_parts[0], "{}".format(micro_seconds).rjust(6, "0"), ) - dt = datetime_.datetime.strptime( - input_data, '%Y-%m-%dT%H:%M:%S.%f') - else: - dt = datetime_.datetime.strptime( - input_data, '%Y-%m-%dT%H:%M:%S') - dt = dt.replace(tzinfo=tz) - return dt - def gds_validate_date(self, input_data, node=None, input_name=''): - return input_data - def gds_format_date(self, input_data, input_name=''): - _svalue = '%04d-%02d-%02d' % ( - input_data.year, - input_data.month, - input_data.day, - ) - try: - if input_data.tzinfo is not None: - tzoff = input_data.tzinfo.utcoffset(input_data) - if tzoff is not None: - total_seconds = tzoff.seconds + (86400 * tzoff.days) - if total_seconds == 0: - _svalue += 'Z' - else: - if total_seconds < 0: - _svalue += '-' - total_seconds *= -1 - else: - _svalue += '+' - hours = total_seconds // 3600 - minutes = (total_seconds - (hours * 3600)) // 60 - _svalue += '{0:02d}:{1:02d}'.format( - hours, minutes) - except AttributeError: - pass - return _svalue - @classmethod - def gds_parse_date(cls, input_data): - tz = None - if input_data[-1] == 'Z': - tz = GeneratedsSuper._FixedOffsetTZ(0, 'UTC') - input_data = input_data[:-1] - else: - results = GeneratedsSuper.tzoff_pattern.search(input_data) - if results is not None: - tzoff_parts = results.group(2).split(':') - tzoff = int(tzoff_parts[0]) * 60 + int(tzoff_parts[1]) - if results.group(1) == '-': - tzoff *= -1 - tz = GeneratedsSuper._FixedOffsetTZ( - tzoff, results.group(0)) - input_data = input_data[:-6] - dt = datetime_.datetime.strptime(input_data, '%Y-%m-%d') - dt = dt.replace(tzinfo=tz) - return dt.date() - def gds_validate_time(self, input_data, node=None, input_name=''): - return input_data - def gds_format_time(self, input_data, input_name=''): - if input_data.microsecond == 0: - _svalue = '%02d:%02d:%02d' % ( - input_data.hour, - input_data.minute, - input_data.second, - ) - else: - _svalue = '%02d:%02d:%02d.%s' % ( - input_data.hour, - input_data.minute, - input_data.second, - ('%f' % (float(input_data.microsecond) / 1000000))[2:], - ) - if input_data.tzinfo is not None: - tzoff = input_data.tzinfo.utcoffset(input_data) - if tzoff is not None: - total_seconds = tzoff.seconds + (86400 * tzoff.days) - if total_seconds == 0: - _svalue += 'Z' - else: - if total_seconds < 0: - _svalue += '-' - total_seconds *= -1 - else: - _svalue += '+' - hours = total_seconds // 3600 - minutes = (total_seconds - (hours * 3600)) // 60 - _svalue += '{0:02d}:{1:02d}'.format(hours, minutes) - return _svalue - def gds_validate_simple_patterns(self, patterns, target): - # pat is a list of lists of strings/patterns. - # The target value must match at least one of the patterns - # in order for the test to succeed. - found1 = True - for patterns1 in patterns: - found2 = False - for patterns2 in patterns1: - mo = re_.search(patterns2, target) - if mo is not None and len(mo.group(0)) == len(target): - found2 = True - break - if not found2: - found1 = False - break - return found1 - @classmethod - def gds_parse_time(cls, input_data): - tz = None - if input_data[-1] == 'Z': - tz = GeneratedsSuper._FixedOffsetTZ(0, 'UTC') - input_data = input_data[:-1] - else: - results = GeneratedsSuper.tzoff_pattern.search(input_data) - if results is not None: - tzoff_parts = results.group(2).split(':') - tzoff = int(tzoff_parts[0]) * 60 + int(tzoff_parts[1]) - if results.group(1) == '-': - tzoff *= -1 - tz = GeneratedsSuper._FixedOffsetTZ( - tzoff, results.group(0)) - input_data = input_data[:-6] - if len(input_data.split('.')) > 1: - dt = datetime_.datetime.strptime(input_data, '%H:%M:%S.%f') - else: - dt = datetime_.datetime.strptime(input_data, '%H:%M:%S') - dt = dt.replace(tzinfo=tz) - return dt.time() - def gds_str_lower(self, instring): - return instring.lower() - def get_path_(self, node): - path_list = [] - self.get_path_list_(node, path_list) - path_list.reverse() - path = '/'.join(path_list) - return path - Tag_strip_pattern_ = re_.compile(r'\{.*\}') - def get_path_list_(self, node, path_list): - if node is None: - return - tag = GeneratedsSuper.Tag_strip_pattern_.sub('', node.tag) - if tag: - path_list.append(tag) - self.get_path_list_(node.getparent(), path_list) - def get_class_obj_(self, node, default_class=None): - class_obj1 = default_class - if 'xsi' in node.nsmap: - classname = node.get('{%s}type' % node.nsmap['xsi']) - if classname is not None: - names = classname.split(':') - if len(names) == 2: - classname = names[1] - class_obj2 = globals().get(classname) - if class_obj2 is not None: - class_obj1 = class_obj2 - return class_obj1 - def gds_build_any(self, node, type_name=None): - return None - @classmethod - def gds_reverse_node_mapping(cls, mapping): - return dict(((v, k) for k, v in mapping.items())) - @staticmethod - def gds_encode(instring): - if sys.version_info.major == 2: - if ExternalEncoding: - encoding = ExternalEncoding - else: - encoding = 'utf-8' - return instring.encode(encoding) - else: - return instring - @staticmethod - def convert_unicode(instring): - if isinstance(instring, str): - result = quote_xml(instring) - elif sys.version_info.major == 2 and isinstance(instring, unicode): - result = quote_xml(instring).encode('utf8') - else: - result = GeneratedsSuper.gds_encode(str(instring)) - return result - def __eq__(self, other): - if type(self) != type(other): - return False - return self.__dict__ == other.__dict__ - def __ne__(self, other): - return not self.__eq__(other) - - def getSubclassFromModule_(module, class_): - '''Get the subclass of a class from a specific module.''' - name = class_.__name__ + 'Sub' - if hasattr(module, name): - return getattr(module, name) - else: - return None - - -# -# If you have installed IPython you can uncomment and use the following. -# IPython is available from http://ipython.scipy.org/. -# - -## from IPython.Shell import IPShellEmbed -## args = '' -## ipshell = IPShellEmbed(args, -## banner = 'Dropping into IPython', -## exit_msg = 'Leaving Interpreter, back to program.') - -# Then use the following line where and when you want to drop into the -# IPython shell: -# ipshell(' -- Entering ipshell.\nHit Ctrl-D to exit') - -# -# Globals -# - -ExternalEncoding = '' -Tag_pattern_ = re_.compile(r'({.*})?(.*)') -String_cleanup_pat_ = re_.compile(r"[\n\r\s]+") -Namespace_extract_pat_ = re_.compile(r'{(.*)}(.*)') -CDATA_pattern_ = re_.compile(r"", re_.DOTALL) - -# Change this to redirect the generated superclass module to use a -# specific subclass module. -CurrentSubclassModule_ = None - -# -# Support/utility functions. -# - - -def showIndent(outfile, level, pretty_print=True): - if pretty_print: - for idx in range(level): - outfile.write(' ') - - -def quote_xml(inStr): - "Escape markup chars, but do not modify CDATA sections." - if not inStr: - return '' - s1 = (isinstance(inStr, BaseStrType_) and inStr or '%s' % inStr) - s2 = '' - pos = 0 - matchobjects = CDATA_pattern_.finditer(s1) - for mo in matchobjects: - s3 = s1[pos:mo.start()] - s2 += quote_xml_aux(s3) - s2 += s1[mo.start():mo.end()] - pos = mo.end() - s3 = s1[pos:] - s2 += quote_xml_aux(s3) - return s2 - - -def quote_xml_aux(inStr): - s1 = inStr.replace('&', '&') - s1 = s1.replace('<', '<') - s1 = s1.replace('>', '>') - return s1 - - -def quote_attrib(inStr): - s1 = (isinstance(inStr, BaseStrType_) and inStr or '%s' % inStr) - s1 = s1.replace('&', '&') - s1 = s1.replace('<', '<') - s1 = s1.replace('>', '>') - if '"' in s1: - if "'" in s1: - s1 = '"%s"' % s1.replace('"', """) - else: - s1 = "'%s'" % s1 - else: - s1 = '"%s"' % s1 - return s1 - - -def quote_python(inStr): - s1 = inStr - if s1.find("'") == -1: - if s1.find('\n') == -1: - return "'%s'" % s1 - else: - return "'''%s'''" % s1 - else: - if s1.find('"') != -1: - s1 = s1.replace('"', '\\"') - if s1.find('\n') == -1: - return '"%s"' % s1 - else: - return '"""%s"""' % s1 - - -def get_all_text_(node): - if node.text is not None: - text = node.text - else: - text = '' - for child in node: - if child.tail is not None: - text += child.tail - return text - - -def find_attr_value_(attr_name, node): - attrs = node.attrib - attr_parts = attr_name.split(':') - value = None - if len(attr_parts) == 1: - value = attrs.get(attr_name) - elif len(attr_parts) == 2: - prefix, name = attr_parts - namespace = node.nsmap.get(prefix) - if namespace is not None: - value = attrs.get('{%s}%s' % (namespace, name, )) - return value - - -class GDSParseError(Exception): - pass - - -def raise_parse_error(node, msg): - msg = '%s (element %s/line %d)' % (msg, node.tag, node.sourceline, ) - raise GDSParseError(msg) - - -class MixedContainer: - # Constants for category: - CategoryNone = 0 - CategoryText = 1 - CategorySimple = 2 - CategoryComplex = 3 - # Constants for content_type: - TypeNone = 0 - TypeText = 1 - TypeString = 2 - TypeInteger = 3 - TypeFloat = 4 - TypeDecimal = 5 - TypeDouble = 6 - TypeBoolean = 7 - TypeBase64 = 8 - def __init__(self, category, content_type, name, value): - self.category = category - self.content_type = content_type - self.name = name - self.value = value - def getCategory(self): - return self.category - def getContenttype(self, content_type): - return self.content_type - def getValue(self): - return self.value - def getName(self): - return self.name - def export(self, outfile, level, name, namespace, - pretty_print=True): - if self.category == MixedContainer.CategoryText: - # Prevent exporting empty content as empty lines. - if self.value.strip(): - outfile.write(self.value) - elif self.category == MixedContainer.CategorySimple: - self.exportSimple(outfile, level, name) - else: # category == MixedContainer.CategoryComplex - self.value.export( - outfile, level, namespace, name_=name, - pretty_print=pretty_print) - def exportSimple(self, outfile, level, name): - if self.content_type == MixedContainer.TypeString: - outfile.write('<%s>%s' % ( - self.name, self.value, self.name)) - elif self.content_type == MixedContainer.TypeInteger or \ - self.content_type == MixedContainer.TypeBoolean: - outfile.write('<%s>%d' % ( - self.name, self.value, self.name)) - elif self.content_type == MixedContainer.TypeFloat or \ - self.content_type == MixedContainer.TypeDecimal: - outfile.write('<%s>%f' % ( - self.name, self.value, self.name)) - elif self.content_type == MixedContainer.TypeDouble: - outfile.write('<%s>%g' % ( - self.name, self.value, self.name)) - elif self.content_type == MixedContainer.TypeBase64: - outfile.write('<%s>%s' % ( - self.name, - base64.b64encode(self.value), - self.name)) - def to_etree(self, element): - if self.category == MixedContainer.CategoryText: - # Prevent exporting empty content as empty lines. - if self.value.strip(): - if len(element) > 0: - if element[-1].tail is None: - element[-1].tail = self.value - else: - element[-1].tail += self.value - else: - if element.text is None: - element.text = self.value - else: - element.text += self.value - elif self.category == MixedContainer.CategorySimple: - subelement = etree_.SubElement( - element, '%s' % self.name) - subelement.text = self.to_etree_simple() - else: # category == MixedContainer.CategoryComplex - self.value.to_etree(element) - def to_etree_simple(self): - if self.content_type == MixedContainer.TypeString: - text = self.value - elif (self.content_type == MixedContainer.TypeInteger or - self.content_type == MixedContainer.TypeBoolean): - text = '%d' % self.value - elif (self.content_type == MixedContainer.TypeFloat or - self.content_type == MixedContainer.TypeDecimal): - text = '%f' % self.value - elif self.content_type == MixedContainer.TypeDouble: - text = '%g' % self.value - elif self.content_type == MixedContainer.TypeBase64: - text = '%s' % base64.b64encode(self.value) - return text - def exportLiteral(self, outfile, level, name): - if self.category == MixedContainer.CategoryText: - showIndent(outfile, level) - outfile.write( - 'model_.MixedContainer(%d, %d, "%s", "%s"),\n' % ( - self.category, self.content_type, - self.name, self.value)) - elif self.category == MixedContainer.CategorySimple: - showIndent(outfile, level) - outfile.write( - 'model_.MixedContainer(%d, %d, "%s", "%s"),\n' % ( - self.category, self.content_type, - self.name, self.value)) - else: # category == MixedContainer.CategoryComplex - showIndent(outfile, level) - outfile.write( - 'model_.MixedContainer(%d, %d, "%s",\n' % ( - self.category, self.content_type, self.name,)) - self.value.exportLiteral(outfile, level + 1) - showIndent(outfile, level) - outfile.write(')\n') - - -class MemberSpec_(object): - def __init__(self, name='', data_type='', container=0, - optional=0, child_attrs=None, choice=None): - self.name = name - self.data_type = data_type - self.container = container - self.child_attrs = child_attrs - self.choice = choice - self.optional = optional - def set_name(self, name): self.name = name - def get_name(self): return self.name - def set_data_type(self, data_type): self.data_type = data_type - def get_data_type_chain(self): return self.data_type - def get_data_type(self): - if isinstance(self.data_type, list): - if len(self.data_type) > 0: - return self.data_type[-1] - else: - return 'xs:string' - else: - return self.data_type - def set_container(self, container): self.container = container - def get_container(self): return self.container - def set_child_attrs(self, child_attrs): self.child_attrs = child_attrs - def get_child_attrs(self): return self.child_attrs - def set_choice(self, choice): self.choice = choice - def get_choice(self): return self.choice - def set_optional(self, optional): self.optional = optional - def get_optional(self): return self.optional - - -def _cast(typ, value): - if typ is None or value is None: - return value - return typ(value) - -# -# Data representation classes. -# - - -class CatalogFile(GeneratedsSuper): - subclass = None - superclass = None - def __init__(self, products=None, **kwargs_): - self.original_tagname_ = None - self.parent_object_ = kwargs_.get('parent_object_') - self.products = products - def factory(*args_, **kwargs_): - if CurrentSubclassModule_ is not None: - subclass = getSubclassFromModule_( - CurrentSubclassModule_, CatalogFile) - if subclass is not None: - return subclass(*args_, **kwargs_) - if CatalogFile.subclass: - return CatalogFile.subclass(*args_, **kwargs_) - else: - return CatalogFile(*args_, **kwargs_) - factory = staticmethod(factory) - def get_products(self): - return self.products - def set_products(self, products): - self.products = products - productsProp = property(get_products, set_products) - def hasContent_(self): - if ( - self.products is not None - ): - return True - else: - return False - def export(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='CatalogFile', pretty_print=True): - imported_ns_def_ = GenerateDSNamespaceDefs_.get('CatalogFile') - if imported_ns_def_ is not None: - namespacedef_ = imported_ns_def_ - if pretty_print: - eol_ = '\n' - else: - eol_ = '' - if self.original_tagname_ is not None: - name_ = self.original_tagname_ - showIndent(outfile, level, pretty_print) - outfile.write('<%s%s%s' % (namespaceprefix_, name_, namespacedef_ and ' ' + namespacedef_ or '', )) - already_processed = set() - self.exportAttributes(outfile, level, already_processed, namespaceprefix_, name_='CatalogFile') - if self.hasContent_(): - outfile.write('>%s' % (eol_, )) - self.exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='CatalogFile', pretty_print=pretty_print) - showIndent(outfile, level, pretty_print) - outfile.write('%s' % (namespaceprefix_, name_, eol_)) - else: - outfile.write('/>%s' % (eol_, )) - def exportAttributes(self, outfile, level, already_processed, namespaceprefix_='', name_='CatalogFile'): - pass - def exportChildren(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='CatalogFile', fromsubclass_=False, pretty_print=True): - if pretty_print: - eol_ = '\n' - else: - eol_ = '' - if self.products is not None: - self.products.export(outfile, level, namespaceprefix_, namespacedef_='', name_='products', pretty_print=pretty_print) - def build(self, node): - already_processed = set() - self.buildAttributes(node, node.attrib, already_processed) - for child in node: - nodeName_ = Tag_pattern_.match(child.tag).groups()[-1] - self.buildChildren(child, node, nodeName_) - return self - def buildAttributes(self, node, attrs, already_processed): - pass - def buildChildren(self, child_, node, nodeName_, fromsubclass_=False): - if nodeName_ == 'products': - obj_ = ProductList.factory(parent_object_=self) - obj_.build(child_) - self.products = obj_ - obj_.original_tagname_ = 'products' -# end class CatalogFile - - -class Product(GeneratedsSuper): - subclass = None - superclass = None - def __init__(self, ref=None, **kwargs_): - self.original_tagname_ = None - self.parent_object_ = kwargs_.get('parent_object_') - self.ref = ref - def factory(*args_, **kwargs_): - if CurrentSubclassModule_ is not None: - subclass = getSubclassFromModule_( - CurrentSubclassModule_, Product) - if subclass is not None: - return subclass(*args_, **kwargs_) - if Product.subclass: - return Product.subclass(*args_, **kwargs_) - else: - return Product(*args_, **kwargs_) - factory = staticmethod(factory) - def get_ref(self): - return self.ref - def set_ref(self, ref): - self.ref = ref - refProp = property(get_ref, set_ref) - def hasContent_(self): - if ( - self.ref is not None - ): - return True - else: - return False - def export(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='Product', pretty_print=True): - imported_ns_def_ = GenerateDSNamespaceDefs_.get('Product') - if imported_ns_def_ is not None: - namespacedef_ = imported_ns_def_ - if pretty_print: - eol_ = '\n' - else: - eol_ = '' - if self.original_tagname_ is not None: - name_ = self.original_tagname_ - showIndent(outfile, level, pretty_print) - outfile.write('<%s%s%s' % (namespaceprefix_, name_, namespacedef_ and ' ' + namespacedef_ or '', )) - already_processed = set() - self.exportAttributes(outfile, level, already_processed, namespaceprefix_, name_='Product') - if self.hasContent_(): - outfile.write('>%s' % (eol_, )) - self.exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='Product', pretty_print=pretty_print) - showIndent(outfile, level, pretty_print) - outfile.write('%s' % (namespaceprefix_, name_, eol_)) - else: - outfile.write('/>%s' % (eol_, )) - def exportAttributes(self, outfile, level, already_processed, namespaceprefix_='', name_='Product'): - pass - def exportChildren(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='Product', fromsubclass_=False, pretty_print=True): - if pretty_print: - eol_ = '\n' - else: - eol_ = '' - if self.ref is not None: - showIndent(outfile, level, pretty_print) - outfile.write('<%sref>%s%s' % (namespaceprefix_ , self.gds_encode(self.gds_format_string(quote_xml(self.ref), input_name='ref')), namespaceprefix_ , eol_)) - def build(self, node): - already_processed = set() - self.buildAttributes(node, node.attrib, already_processed) - for child in node: - nodeName_ = Tag_pattern_.match(child.tag).groups()[-1] - self.buildChildren(child, node, nodeName_) - return self - def buildAttributes(self, node, attrs, already_processed): - pass - def buildChildren(self, child_, node, nodeName_, fromsubclass_=False): - if nodeName_ == 'ref': - ref_ = child_.text - ref_ = self.gds_validate_string(ref_, node, 'ref') - self.ref = ref_ -# end class Product - - -class ProductList(GeneratedsSuper): - subclass = None - superclass = None - def __init__(self, product=None, **kwargs_): - self.original_tagname_ = None - self.parent_object_ = kwargs_.get('parent_object_') - if product is None: - self.product = [] - else: - self.product = product - def factory(*args_, **kwargs_): - if CurrentSubclassModule_ is not None: - subclass = getSubclassFromModule_( - CurrentSubclassModule_, ProductList) - if subclass is not None: - return subclass(*args_, **kwargs_) - if ProductList.subclass: - return ProductList.subclass(*args_, **kwargs_) - else: - return ProductList(*args_, **kwargs_) - factory = staticmethod(factory) - def get_product(self): - return self.product - def set_product(self, product): - self.product = product - def add_product(self, value): - self.product.append(value) - def add_product(self, value): - self.product.append(value) - def insert_product_at(self, index, value): - self.product.insert(index, value) - def replace_product_at(self, index, value): - self.product[index] = value - productProp = property(get_product, set_product) - def hasContent_(self): - if ( - self.product - ): - return True - else: - return False - def export(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='ProductList', pretty_print=True): - imported_ns_def_ = GenerateDSNamespaceDefs_.get('ProductList') - if imported_ns_def_ is not None: - namespacedef_ = imported_ns_def_ - if pretty_print: - eol_ = '\n' - else: - eol_ = '' - if self.original_tagname_ is not None: - name_ = self.original_tagname_ - showIndent(outfile, level, pretty_print) - outfile.write('<%s%s%s' % (namespaceprefix_, name_, namespacedef_ and ' ' + namespacedef_ or '', )) - already_processed = set() - self.exportAttributes(outfile, level, already_processed, namespaceprefix_, name_='ProductList') - if self.hasContent_(): - outfile.write('>%s' % (eol_, )) - self.exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='ProductList', pretty_print=pretty_print) - showIndent(outfile, level, pretty_print) - outfile.write('%s' % (namespaceprefix_, name_, eol_)) - else: - outfile.write('/>%s' % (eol_, )) - def exportAttributes(self, outfile, level, already_processed, namespaceprefix_='', name_='ProductList'): - pass - def exportChildren(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='ProductList', fromsubclass_=False, pretty_print=True): - if pretty_print: - eol_ = '\n' - else: - eol_ = '' - for product_ in self.product: - product_.export(outfile, level, namespaceprefix_, namespacedef_='', name_='product', pretty_print=pretty_print) - def build(self, node): - already_processed = set() - self.buildAttributes(node, node.attrib, already_processed) - for child in node: - nodeName_ = Tag_pattern_.match(child.tag).groups()[-1] - self.buildChildren(child, node, nodeName_) - return self - def buildAttributes(self, node, attrs, already_processed): - pass - def buildChildren(self, child_, node, nodeName_, fromsubclass_=False): - if nodeName_ == 'product': - obj_ = Product.factory(parent_object_=self) - obj_.build(child_) - self.product.append(obj_) - obj_.original_tagname_ = 'product' -# end class ProductList - - -GDSClassesMapping = { -} - - -USAGE_TEXT = """ -Usage: python .py [ -s ] -""" - - -def usage(): - print(USAGE_TEXT) - sys.exit(1) - - -def get_root_tag(node): - tag = Tag_pattern_.match(node.tag).groups()[-1] - rootClass = GDSClassesMapping.get(tag) - if rootClass is None: - rootClass = globals().get(tag) - return tag, rootClass - - -def parse(inFileName, silence=False): - parser = None - doc = parsexml_(inFileName, parser) - rootNode = doc.getroot() - rootTag, rootClass = get_root_tag(rootNode) - if rootClass is None: - rootTag = 'CatalogFile' - rootClass = CatalogFile - rootObj = rootClass.factory() - rootObj.build(rootNode) - # Enable Python to collect the space used by the DOM. - doc = None - if not silence: - sys.stdout.write('\n') - rootObj.export( - sys.stdout, 0, name_=rootTag, - namespacedef_='', - pretty_print=True) - return rootObj - - -def parseEtree(inFileName, silence=False): - parser = None - doc = parsexml_(inFileName, parser) - rootNode = doc.getroot() - rootTag, rootClass = get_root_tag(rootNode) - if rootClass is None: - rootTag = 'CatalogFile' - rootClass = CatalogFile - rootObj = rootClass.factory() - rootObj.build(rootNode) - # Enable Python to collect the space used by the DOM. - doc = None - mapping = {} - rootElement = rootObj.to_etree(None, name_=rootTag, mapping_=mapping) - reverse_mapping = rootObj.gds_reverse_node_mapping(mapping) - if not silence: - content = etree_.tostring( - rootElement, pretty_print=True, - xml_declaration=True, encoding="utf-8") - sys.stdout.write(content) - sys.stdout.write('\n') - return rootObj, rootElement, mapping, reverse_mapping - - -def parseString(inString, silence=False): - '''Parse a string, create the object tree, and export it. - - Arguments: - - inString -- A string. This XML fragment should not start - with an XML declaration containing an encoding. - - silence -- A boolean. If False, export the object. - Returns -- The root object in the tree. - ''' - parser = None - rootNode= parsexmlstring_(inString, parser) - rootTag, rootClass = get_root_tag(rootNode) - if rootClass is None: - rootTag = 'CatalogFile' - rootClass = CatalogFile - rootObj = rootClass.factory() - rootObj.build(rootNode) - # Enable Python to collect the space used by the DOM. - if not silence: - sys.stdout.write('\n') - rootObj.export( - sys.stdout, 0, name_=rootTag, - namespacedef_='') - return rootObj - - -def parseLiteral(inFileName, silence=False): - parser = None - doc = parsexml_(inFileName, parser) - rootNode = doc.getroot() - rootTag, rootClass = get_root_tag(rootNode) - if rootClass is None: - rootTag = 'CatalogFile' - rootClass = CatalogFile - rootObj = rootClass.factory() - rootObj.build(rootNode) - # Enable Python to collect the space used by the DOM. - doc = None - if not silence: - sys.stdout.write('#from catalogFile import *\n\n') - sys.stdout.write('import catalogFile as model_\n\n') - sys.stdout.write('rootObj = model_.rootClass(\n') - rootObj.exportLiteral(sys.stdout, 0, name_=rootTag) - sys.stdout.write(')\n') - return rootObj - - -def main(): - args = sys.argv[1:] - if len(args) == 1: - parse(args[0]) - else: - usage() - - -if __name__ == '__main__': - #import pdb; pdb.set_trace() - main() - - -__all__ = [ - "CatalogFile", - "Product", - "ProductList" -] diff --git a/src/ee/xml/indexFile.py b/src/ee/xml/indexFile.py new file mode 100644 index 0000000..d460db3 --- /dev/null +++ b/src/ee/xml/indexFile.py @@ -0,0 +1,1101 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Generated by generateDS.py. +# Python 3.7.2+ (default, Feb 2 2019, 14:31:48) [GCC 8.2.0] +# +# Command line options: +# ('-f', '') +# ('--no-dates', '') +# ('--no-versions', '') +# ('--output-directory', 'src/ee/xml') +# ('-o', 'src/ee/xml/indexFile.py') +# ('-m', '') +# +# Command line arguments: +# xsd/ee-index.xsd +# +# Command line: +# env/bin/generateDS.py -f --no-dates --no-versions --output-directory="src/ee/xml" -o "src/ee/xml/indexFile.py" -m xsd/ee-index.xsd +# +# Current working directory (os.getcwd()): +# ee-python +# + +import sys +import re as re_ +import base64 +import datetime as datetime_ +import warnings as warnings_ +try: + from lxml import etree as etree_ +except ImportError: + from xml.etree import ElementTree as etree_ + + +Validate_simpletypes_ = True +if sys.version_info.major == 2: + BaseStrType_ = basestring +else: + BaseStrType_ = str + + +def parsexml_(infile, parser=None, **kwargs): + if parser is None: + # Use the lxml ElementTree compatible parser so that, e.g., + # we ignore comments. + try: + parser = etree_.ETCompatXMLParser() + except AttributeError: + # fallback to xml.etree + parser = etree_.XMLParser() + doc = etree_.parse(infile, parser=parser, **kwargs) + return doc + +def parsexmlstring_(instring, parser=None, **kwargs): + if parser is None: + # Use the lxml ElementTree compatible parser so that, e.g., + # we ignore comments. + try: + parser = etree_.ETCompatXMLParser() + except AttributeError: + # fallback to xml.etree + parser = etree_.XMLParser() + element = etree_.fromstring(instring, parser=parser, **kwargs) + return element + +# +# Namespace prefix definition table (and other attributes, too) +# +# The module generatedsnamespaces, if it is importable, must contain +# a dictionary named GeneratedsNamespaceDefs. This Python dictionary +# should map element type names (strings) to XML schema namespace prefix +# definitions. The export method for any class for which there is +# a namespace prefix definition, will export that definition in the +# XML representation of that element. See the export method of +# any generated element type class for a example of the use of this +# table. +# A sample table is: +# +# # File: generatedsnamespaces.py +# +# GenerateDSNamespaceDefs = { +# "ElementtypeA": "http://www.xxx.com/namespaceA", +# "ElementtypeB": "http://www.xxx.com/namespaceB", +# } +# + +try: + from generatedsnamespaces import GenerateDSNamespaceDefs as GenerateDSNamespaceDefs_ +except ImportError: + GenerateDSNamespaceDefs_ = {} + +# +# The root super-class for element type classes +# +# Calls to the methods in these classes are generated by generateDS.py. +# You can replace these methods by re-implementing the following class +# in a module named generatedssuper.py. + +try: + from generatedssuper import GeneratedsSuper +except ImportError as exp: + + class GeneratedsSuper(object): + tzoff_pattern = re_.compile(r'(\+|-)((0\d|1[0-3]):[0-5]\d|14:00)$') + class _FixedOffsetTZ(datetime_.tzinfo): + def __init__(self, offset, name): + self.__offset = datetime_.timedelta(minutes=offset) + self.__name = name + def utcoffset(self, dt): + return self.__offset + def tzname(self, dt): + return self.__name + def dst(self, dt): + return None + def gds_format_string(self, input_data, input_name=''): + return input_data + def gds_validate_string(self, input_data, node=None, input_name=''): + if not input_data: + return '' + else: + return input_data + def gds_format_base64(self, input_data, input_name=''): + return base64.b64encode(input_data) + def gds_validate_base64(self, input_data, node=None, input_name=''): + return input_data + def gds_format_integer(self, input_data, input_name=''): + return '%d' % input_data + def gds_validate_integer(self, input_data, node=None, input_name=''): + return input_data + def gds_format_integer_list(self, input_data, input_name=''): + return '%s' % ' '.join(input_data) + def gds_validate_integer_list( + self, input_data, node=None, input_name=''): + values = input_data.split() + for value in values: + try: + int(value) + except (TypeError, ValueError): + raise_parse_error(node, 'Requires sequence of integers') + return values + def gds_format_float(self, input_data, input_name=''): + return ('%.15f' % input_data).rstrip('0') + def gds_validate_float(self, input_data, node=None, input_name=''): + return input_data + def gds_format_float_list(self, input_data, input_name=''): + return '%s' % ' '.join(input_data) + def gds_validate_float_list( + self, input_data, node=None, input_name=''): + values = input_data.split() + for value in values: + try: + float(value) + except (TypeError, ValueError): + raise_parse_error(node, 'Requires sequence of floats') + return values + def gds_format_double(self, input_data, input_name=''): + return '%e' % input_data + def gds_validate_double(self, input_data, node=None, input_name=''): + return input_data + def gds_format_double_list(self, input_data, input_name=''): + return '%s' % ' '.join(input_data) + def gds_validate_double_list( + self, input_data, node=None, input_name=''): + values = input_data.split() + for value in values: + try: + float(value) + except (TypeError, ValueError): + raise_parse_error(node, 'Requires sequence of doubles') + return values + def gds_format_boolean(self, input_data, input_name=''): + return ('%s' % input_data).lower() + def gds_validate_boolean(self, input_data, node=None, input_name=''): + return input_data + def gds_format_boolean_list(self, input_data, input_name=''): + return '%s' % ' '.join(input_data) + def gds_validate_boolean_list( + self, input_data, node=None, input_name=''): + values = input_data.split() + for value in values: + if value not in ('true', '1', 'false', '0', ): + raise_parse_error( + node, + 'Requires sequence of booleans ' + '("true", "1", "false", "0")') + return values + def gds_validate_datetime(self, input_data, node=None, input_name=''): + return input_data + def gds_format_datetime(self, input_data, input_name=''): + if input_data.microsecond == 0: + _svalue = '%04d-%02d-%02dT%02d:%02d:%02d' % ( + input_data.year, + input_data.month, + input_data.day, + input_data.hour, + input_data.minute, + input_data.second, + ) + else: + _svalue = '%04d-%02d-%02dT%02d:%02d:%02d.%s' % ( + input_data.year, + input_data.month, + input_data.day, + input_data.hour, + input_data.minute, + input_data.second, + ('%f' % (float(input_data.microsecond) / 1000000))[2:], + ) + if input_data.tzinfo is not None: + tzoff = input_data.tzinfo.utcoffset(input_data) + if tzoff is not None: + total_seconds = tzoff.seconds + (86400 * tzoff.days) + if total_seconds == 0: + _svalue += 'Z' + else: + if total_seconds < 0: + _svalue += '-' + total_seconds *= -1 + else: + _svalue += '+' + hours = total_seconds // 3600 + minutes = (total_seconds - (hours * 3600)) // 60 + _svalue += '{0:02d}:{1:02d}'.format(hours, minutes) + return _svalue + @classmethod + def gds_parse_datetime(cls, input_data): + tz = None + if input_data[-1] == 'Z': + tz = GeneratedsSuper._FixedOffsetTZ(0, 'UTC') + input_data = input_data[:-1] + else: + results = GeneratedsSuper.tzoff_pattern.search(input_data) + if results is not None: + tzoff_parts = results.group(2).split(':') + tzoff = int(tzoff_parts[0]) * 60 + int(tzoff_parts[1]) + if results.group(1) == '-': + tzoff *= -1 + tz = GeneratedsSuper._FixedOffsetTZ( + tzoff, results.group(0)) + input_data = input_data[:-6] + time_parts = input_data.split('.') + if len(time_parts) > 1: + micro_seconds = int(float('0.' + time_parts[1]) * 1000000) + input_data = '%s.%s' % ( + time_parts[0], "{}".format(micro_seconds).rjust(6, "0"), ) + dt = datetime_.datetime.strptime( + input_data, '%Y-%m-%dT%H:%M:%S.%f') + else: + dt = datetime_.datetime.strptime( + input_data, '%Y-%m-%dT%H:%M:%S') + dt = dt.replace(tzinfo=tz) + return dt + def gds_validate_date(self, input_data, node=None, input_name=''): + return input_data + def gds_format_date(self, input_data, input_name=''): + _svalue = '%04d-%02d-%02d' % ( + input_data.year, + input_data.month, + input_data.day, + ) + try: + if input_data.tzinfo is not None: + tzoff = input_data.tzinfo.utcoffset(input_data) + if tzoff is not None: + total_seconds = tzoff.seconds + (86400 * tzoff.days) + if total_seconds == 0: + _svalue += 'Z' + else: + if total_seconds < 0: + _svalue += '-' + total_seconds *= -1 + else: + _svalue += '+' + hours = total_seconds // 3600 + minutes = (total_seconds - (hours * 3600)) // 60 + _svalue += '{0:02d}:{1:02d}'.format( + hours, minutes) + except AttributeError: + pass + return _svalue + @classmethod + def gds_parse_date(cls, input_data): + tz = None + if input_data[-1] == 'Z': + tz = GeneratedsSuper._FixedOffsetTZ(0, 'UTC') + input_data = input_data[:-1] + else: + results = GeneratedsSuper.tzoff_pattern.search(input_data) + if results is not None: + tzoff_parts = results.group(2).split(':') + tzoff = int(tzoff_parts[0]) * 60 + int(tzoff_parts[1]) + if results.group(1) == '-': + tzoff *= -1 + tz = GeneratedsSuper._FixedOffsetTZ( + tzoff, results.group(0)) + input_data = input_data[:-6] + dt = datetime_.datetime.strptime(input_data, '%Y-%m-%d') + dt = dt.replace(tzinfo=tz) + return dt.date() + def gds_validate_time(self, input_data, node=None, input_name=''): + return input_data + def gds_format_time(self, input_data, input_name=''): + if input_data.microsecond == 0: + _svalue = '%02d:%02d:%02d' % ( + input_data.hour, + input_data.minute, + input_data.second, + ) + else: + _svalue = '%02d:%02d:%02d.%s' % ( + input_data.hour, + input_data.minute, + input_data.second, + ('%f' % (float(input_data.microsecond) / 1000000))[2:], + ) + if input_data.tzinfo is not None: + tzoff = input_data.tzinfo.utcoffset(input_data) + if tzoff is not None: + total_seconds = tzoff.seconds + (86400 * tzoff.days) + if total_seconds == 0: + _svalue += 'Z' + else: + if total_seconds < 0: + _svalue += '-' + total_seconds *= -1 + else: + _svalue += '+' + hours = total_seconds // 3600 + minutes = (total_seconds - (hours * 3600)) // 60 + _svalue += '{0:02d}:{1:02d}'.format(hours, minutes) + return _svalue + def gds_validate_simple_patterns(self, patterns, target): + # pat is a list of lists of strings/patterns. + # The target value must match at least one of the patterns + # in order for the test to succeed. + found1 = True + for patterns1 in patterns: + found2 = False + for patterns2 in patterns1: + mo = re_.search(patterns2, target) + if mo is not None and len(mo.group(0)) == len(target): + found2 = True + break + if not found2: + found1 = False + break + return found1 + @classmethod + def gds_parse_time(cls, input_data): + tz = None + if input_data[-1] == 'Z': + tz = GeneratedsSuper._FixedOffsetTZ(0, 'UTC') + input_data = input_data[:-1] + else: + results = GeneratedsSuper.tzoff_pattern.search(input_data) + if results is not None: + tzoff_parts = results.group(2).split(':') + tzoff = int(tzoff_parts[0]) * 60 + int(tzoff_parts[1]) + if results.group(1) == '-': + tzoff *= -1 + tz = GeneratedsSuper._FixedOffsetTZ( + tzoff, results.group(0)) + input_data = input_data[:-6] + if len(input_data.split('.')) > 1: + dt = datetime_.datetime.strptime(input_data, '%H:%M:%S.%f') + else: + dt = datetime_.datetime.strptime(input_data, '%H:%M:%S') + dt = dt.replace(tzinfo=tz) + return dt.time() + def gds_str_lower(self, instring): + return instring.lower() + def get_path_(self, node): + path_list = [] + self.get_path_list_(node, path_list) + path_list.reverse() + path = '/'.join(path_list) + return path + Tag_strip_pattern_ = re_.compile(r'\{.*\}') + def get_path_list_(self, node, path_list): + if node is None: + return + tag = GeneratedsSuper.Tag_strip_pattern_.sub('', node.tag) + if tag: + path_list.append(tag) + self.get_path_list_(node.getparent(), path_list) + def get_class_obj_(self, node, default_class=None): + class_obj1 = default_class + if 'xsi' in node.nsmap: + classname = node.get('{%s}type' % node.nsmap['xsi']) + if classname is not None: + names = classname.split(':') + if len(names) == 2: + classname = names[1] + class_obj2 = globals().get(classname) + if class_obj2 is not None: + class_obj1 = class_obj2 + return class_obj1 + def gds_build_any(self, node, type_name=None): + return None + @classmethod + def gds_reverse_node_mapping(cls, mapping): + return dict(((v, k) for k, v in mapping.items())) + @staticmethod + def gds_encode(instring): + if sys.version_info.major == 2: + if ExternalEncoding: + encoding = ExternalEncoding + else: + encoding = 'utf-8' + return instring.encode(encoding) + else: + return instring + @staticmethod + def convert_unicode(instring): + if isinstance(instring, str): + result = quote_xml(instring) + elif sys.version_info.major == 2 and isinstance(instring, unicode): + result = quote_xml(instring).encode('utf8') + else: + result = GeneratedsSuper.gds_encode(str(instring)) + return result + def __eq__(self, other): + if type(self) != type(other): + return False + return self.__dict__ == other.__dict__ + def __ne__(self, other): + return not self.__eq__(other) + + def getSubclassFromModule_(module, class_): + '''Get the subclass of a class from a specific module.''' + name = class_.__name__ + 'Sub' + if hasattr(module, name): + return getattr(module, name) + else: + return None + + +# +# If you have installed IPython you can uncomment and use the following. +# IPython is available from http://ipython.scipy.org/. +# + +## from IPython.Shell import IPShellEmbed +## args = '' +## ipshell = IPShellEmbed(args, +## banner = 'Dropping into IPython', +## exit_msg = 'Leaving Interpreter, back to program.') + +# Then use the following line where and when you want to drop into the +# IPython shell: +# ipshell(' -- Entering ipshell.\nHit Ctrl-D to exit') + +# +# Globals +# + +ExternalEncoding = '' +Tag_pattern_ = re_.compile(r'({.*})?(.*)') +String_cleanup_pat_ = re_.compile(r"[\n\r\s]+") +Namespace_extract_pat_ = re_.compile(r'{(.*)}(.*)') +CDATA_pattern_ = re_.compile(r"", re_.DOTALL) + +# Change this to redirect the generated superclass module to use a +# specific subclass module. +CurrentSubclassModule_ = None + +# +# Support/utility functions. +# + + +def showIndent(outfile, level, pretty_print=True): + if pretty_print: + for idx in range(level): + outfile.write(' ') + + +def quote_xml(inStr): + "Escape markup chars, but do not modify CDATA sections." + if not inStr: + return '' + s1 = (isinstance(inStr, BaseStrType_) and inStr or '%s' % inStr) + s2 = '' + pos = 0 + matchobjects = CDATA_pattern_.finditer(s1) + for mo in matchobjects: + s3 = s1[pos:mo.start()] + s2 += quote_xml_aux(s3) + s2 += s1[mo.start():mo.end()] + pos = mo.end() + s3 = s1[pos:] + s2 += quote_xml_aux(s3) + return s2 + + +def quote_xml_aux(inStr): + s1 = inStr.replace('&', '&') + s1 = s1.replace('<', '<') + s1 = s1.replace('>', '>') + return s1 + + +def quote_attrib(inStr): + s1 = (isinstance(inStr, BaseStrType_) and inStr or '%s' % inStr) + s1 = s1.replace('&', '&') + s1 = s1.replace('<', '<') + s1 = s1.replace('>', '>') + if '"' in s1: + if "'" in s1: + s1 = '"%s"' % s1.replace('"', """) + else: + s1 = "'%s'" % s1 + else: + s1 = '"%s"' % s1 + return s1 + + +def quote_python(inStr): + s1 = inStr + if s1.find("'") == -1: + if s1.find('\n') == -1: + return "'%s'" % s1 + else: + return "'''%s'''" % s1 + else: + if s1.find('"') != -1: + s1 = s1.replace('"', '\\"') + if s1.find('\n') == -1: + return '"%s"' % s1 + else: + return '"""%s"""' % s1 + + +def get_all_text_(node): + if node.text is not None: + text = node.text + else: + text = '' + for child in node: + if child.tail is not None: + text += child.tail + return text + + +def find_attr_value_(attr_name, node): + attrs = node.attrib + attr_parts = attr_name.split(':') + value = None + if len(attr_parts) == 1: + value = attrs.get(attr_name) + elif len(attr_parts) == 2: + prefix, name = attr_parts + namespace = node.nsmap.get(prefix) + if namespace is not None: + value = attrs.get('{%s}%s' % (namespace, name, )) + return value + + +class GDSParseError(Exception): + pass + + +def raise_parse_error(node, msg): + msg = '%s (element %s/line %d)' % (msg, node.tag, node.sourceline, ) + raise GDSParseError(msg) + + +class MixedContainer: + # Constants for category: + CategoryNone = 0 + CategoryText = 1 + CategorySimple = 2 + CategoryComplex = 3 + # Constants for content_type: + TypeNone = 0 + TypeText = 1 + TypeString = 2 + TypeInteger = 3 + TypeFloat = 4 + TypeDecimal = 5 + TypeDouble = 6 + TypeBoolean = 7 + TypeBase64 = 8 + def __init__(self, category, content_type, name, value): + self.category = category + self.content_type = content_type + self.name = name + self.value = value + def getCategory(self): + return self.category + def getContenttype(self, content_type): + return self.content_type + def getValue(self): + return self.value + def getName(self): + return self.name + def export(self, outfile, level, name, namespace, + pretty_print=True): + if self.category == MixedContainer.CategoryText: + # Prevent exporting empty content as empty lines. + if self.value.strip(): + outfile.write(self.value) + elif self.category == MixedContainer.CategorySimple: + self.exportSimple(outfile, level, name) + else: # category == MixedContainer.CategoryComplex + self.value.export( + outfile, level, namespace, name_=name, + pretty_print=pretty_print) + def exportSimple(self, outfile, level, name): + if self.content_type == MixedContainer.TypeString: + outfile.write('<%s>%s' % ( + self.name, self.value, self.name)) + elif self.content_type == MixedContainer.TypeInteger or \ + self.content_type == MixedContainer.TypeBoolean: + outfile.write('<%s>%d' % ( + self.name, self.value, self.name)) + elif self.content_type == MixedContainer.TypeFloat or \ + self.content_type == MixedContainer.TypeDecimal: + outfile.write('<%s>%f' % ( + self.name, self.value, self.name)) + elif self.content_type == MixedContainer.TypeDouble: + outfile.write('<%s>%g' % ( + self.name, self.value, self.name)) + elif self.content_type == MixedContainer.TypeBase64: + outfile.write('<%s>%s' % ( + self.name, + base64.b64encode(self.value), + self.name)) + def to_etree(self, element): + if self.category == MixedContainer.CategoryText: + # Prevent exporting empty content as empty lines. + if self.value.strip(): + if len(element) > 0: + if element[-1].tail is None: + element[-1].tail = self.value + else: + element[-1].tail += self.value + else: + if element.text is None: + element.text = self.value + else: + element.text += self.value + elif self.category == MixedContainer.CategorySimple: + subelement = etree_.SubElement( + element, '%s' % self.name) + subelement.text = self.to_etree_simple() + else: # category == MixedContainer.CategoryComplex + self.value.to_etree(element) + def to_etree_simple(self): + if self.content_type == MixedContainer.TypeString: + text = self.value + elif (self.content_type == MixedContainer.TypeInteger or + self.content_type == MixedContainer.TypeBoolean): + text = '%d' % self.value + elif (self.content_type == MixedContainer.TypeFloat or + self.content_type == MixedContainer.TypeDecimal): + text = '%f' % self.value + elif self.content_type == MixedContainer.TypeDouble: + text = '%g' % self.value + elif self.content_type == MixedContainer.TypeBase64: + text = '%s' % base64.b64encode(self.value) + return text + def exportLiteral(self, outfile, level, name): + if self.category == MixedContainer.CategoryText: + showIndent(outfile, level) + outfile.write( + 'model_.MixedContainer(%d, %d, "%s", "%s"),\n' % ( + self.category, self.content_type, + self.name, self.value)) + elif self.category == MixedContainer.CategorySimple: + showIndent(outfile, level) + outfile.write( + 'model_.MixedContainer(%d, %d, "%s", "%s"),\n' % ( + self.category, self.content_type, + self.name, self.value)) + else: # category == MixedContainer.CategoryComplex + showIndent(outfile, level) + outfile.write( + 'model_.MixedContainer(%d, %d, "%s",\n' % ( + self.category, self.content_type, self.name,)) + self.value.exportLiteral(outfile, level + 1) + showIndent(outfile, level) + outfile.write(')\n') + + +class MemberSpec_(object): + def __init__(self, name='', data_type='', container=0, + optional=0, child_attrs=None, choice=None): + self.name = name + self.data_type = data_type + self.container = container + self.child_attrs = child_attrs + self.choice = choice + self.optional = optional + def set_name(self, name): self.name = name + def get_name(self): return self.name + def set_data_type(self, data_type): self.data_type = data_type + def get_data_type_chain(self): return self.data_type + def get_data_type(self): + if isinstance(self.data_type, list): + if len(self.data_type) > 0: + return self.data_type[-1] + else: + return 'xs:string' + else: + return self.data_type + def set_container(self, container): self.container = container + def get_container(self): return self.container + def set_child_attrs(self, child_attrs): self.child_attrs = child_attrs + def get_child_attrs(self): return self.child_attrs + def set_choice(self, choice): self.choice = choice + def get_choice(self): return self.choice + def set_optional(self, optional): self.optional = optional + def get_optional(self): return self.optional + + +def _cast(typ, value): + if typ is None or value is None: + return value + return typ(value) + +# +# Data representation classes. +# + + +class IndexFile(GeneratedsSuper): + subclass = None + superclass = None + def __init__(self, files=None, **kwargs_): + self.original_tagname_ = None + self.parent_object_ = kwargs_.get('parent_object_') + self.files = files + def factory(*args_, **kwargs_): + if CurrentSubclassModule_ is not None: + subclass = getSubclassFromModule_( + CurrentSubclassModule_, IndexFile) + if subclass is not None: + return subclass(*args_, **kwargs_) + if IndexFile.subclass: + return IndexFile.subclass(*args_, **kwargs_) + else: + return IndexFile(*args_, **kwargs_) + factory = staticmethod(factory) + def get_files(self): + return self.files + def set_files(self, files): + self.files = files + filesProp = property(get_files, set_files) + def hasContent_(self): + if ( + self.files is not None + ): + return True + else: + return False + def export(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='IndexFile', pretty_print=True): + imported_ns_def_ = GenerateDSNamespaceDefs_.get('IndexFile') + if imported_ns_def_ is not None: + namespacedef_ = imported_ns_def_ + if pretty_print: + eol_ = '\n' + else: + eol_ = '' + if self.original_tagname_ is not None: + name_ = self.original_tagname_ + showIndent(outfile, level, pretty_print) + outfile.write('<%s%s%s' % (namespaceprefix_, name_, namespacedef_ and ' ' + namespacedef_ or '', )) + already_processed = set() + self.exportAttributes(outfile, level, already_processed, namespaceprefix_, name_='IndexFile') + if self.hasContent_(): + outfile.write('>%s' % (eol_, )) + self.exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='IndexFile', pretty_print=pretty_print) + showIndent(outfile, level, pretty_print) + outfile.write('%s' % (namespaceprefix_, name_, eol_)) + else: + outfile.write('/>%s' % (eol_, )) + def exportAttributes(self, outfile, level, already_processed, namespaceprefix_='', name_='IndexFile'): + pass + def exportChildren(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='IndexFile', fromsubclass_=False, pretty_print=True): + if pretty_print: + eol_ = '\n' + else: + eol_ = '' + if self.files is not None: + self.files.export(outfile, level, namespaceprefix_, namespacedef_='', name_='files', pretty_print=pretty_print) + def build(self, node): + already_processed = set() + self.buildAttributes(node, node.attrib, already_processed) + for child in node: + nodeName_ = Tag_pattern_.match(child.tag).groups()[-1] + self.buildChildren(child, node, nodeName_) + return self + def buildAttributes(self, node, attrs, already_processed): + pass + def buildChildren(self, child_, node, nodeName_, fromsubclass_=False): + if nodeName_ == 'files': + obj_ = FileList.factory(parent_object_=self) + obj_.build(child_) + self.files = obj_ + obj_.original_tagname_ = 'files' +# end class IndexFile + + +class File(GeneratedsSuper): + subclass = None + superclass = None + def __init__(self, path=None, **kwargs_): + self.original_tagname_ = None + self.parent_object_ = kwargs_.get('parent_object_') + self.path = _cast(None, path) + def factory(*args_, **kwargs_): + if CurrentSubclassModule_ is not None: + subclass = getSubclassFromModule_( + CurrentSubclassModule_, File) + if subclass is not None: + return subclass(*args_, **kwargs_) + if File.subclass: + return File.subclass(*args_, **kwargs_) + else: + return File(*args_, **kwargs_) + factory = staticmethod(factory) + def get_path(self): + return self.path + def set_path(self, path): + self.path = path + pathProp = property(get_path, set_path) + def hasContent_(self): + if ( + + ): + return True + else: + return False + def export(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='File', pretty_print=True): + imported_ns_def_ = GenerateDSNamespaceDefs_.get('File') + if imported_ns_def_ is not None: + namespacedef_ = imported_ns_def_ + if pretty_print: + eol_ = '\n' + else: + eol_ = '' + if self.original_tagname_ is not None: + name_ = self.original_tagname_ + showIndent(outfile, level, pretty_print) + outfile.write('<%s%s%s' % (namespaceprefix_, name_, namespacedef_ and ' ' + namespacedef_ or '', )) + already_processed = set() + self.exportAttributes(outfile, level, already_processed, namespaceprefix_, name_='File') + if self.hasContent_(): + outfile.write('>%s' % (eol_, )) + self.exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='File', pretty_print=pretty_print) + outfile.write('%s' % (namespaceprefix_, name_, eol_)) + else: + outfile.write('/>%s' % (eol_, )) + def exportAttributes(self, outfile, level, already_processed, namespaceprefix_='', name_='File'): + if self.path is not None and 'path' not in already_processed: + already_processed.add('path') + outfile.write(' path=%s' % (self.gds_encode(self.gds_format_string(quote_attrib(self.path), input_name='path')), )) + def exportChildren(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='File', fromsubclass_=False, pretty_print=True): + pass + def build(self, node): + already_processed = set() + self.buildAttributes(node, node.attrib, already_processed) + for child in node: + nodeName_ = Tag_pattern_.match(child.tag).groups()[-1] + self.buildChildren(child, node, nodeName_) + return self + def buildAttributes(self, node, attrs, already_processed): + value = find_attr_value_('path', node) + if value is not None and 'path' not in already_processed: + already_processed.add('path') + self.path = value + def buildChildren(self, child_, node, nodeName_, fromsubclass_=False): + pass +# end class File + + +class FileList(GeneratedsSuper): + subclass = None + superclass = None + def __init__(self, file=None, **kwargs_): + self.original_tagname_ = None + self.parent_object_ = kwargs_.get('parent_object_') + if file is None: + self.file = [] + else: + self.file = file + def factory(*args_, **kwargs_): + if CurrentSubclassModule_ is not None: + subclass = getSubclassFromModule_( + CurrentSubclassModule_, FileList) + if subclass is not None: + return subclass(*args_, **kwargs_) + if FileList.subclass: + return FileList.subclass(*args_, **kwargs_) + else: + return FileList(*args_, **kwargs_) + factory = staticmethod(factory) + def get_file(self): + return self.file + def set_file(self, file): + self.file = file + def add_file(self, value): + self.file.append(value) + def add_file(self, value): + self.file.append(value) + def insert_file_at(self, index, value): + self.file.insert(index, value) + def replace_file_at(self, index, value): + self.file[index] = value + fileProp = property(get_file, set_file) + def hasContent_(self): + if ( + self.file + ): + return True + else: + return False + def export(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='FileList', pretty_print=True): + imported_ns_def_ = GenerateDSNamespaceDefs_.get('FileList') + if imported_ns_def_ is not None: + namespacedef_ = imported_ns_def_ + if pretty_print: + eol_ = '\n' + else: + eol_ = '' + if self.original_tagname_ is not None: + name_ = self.original_tagname_ + showIndent(outfile, level, pretty_print) + outfile.write('<%s%s%s' % (namespaceprefix_, name_, namespacedef_ and ' ' + namespacedef_ or '', )) + already_processed = set() + self.exportAttributes(outfile, level, already_processed, namespaceprefix_, name_='FileList') + if self.hasContent_(): + outfile.write('>%s' % (eol_, )) + self.exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='FileList', pretty_print=pretty_print) + showIndent(outfile, level, pretty_print) + outfile.write('%s' % (namespaceprefix_, name_, eol_)) + else: + outfile.write('/>%s' % (eol_, )) + def exportAttributes(self, outfile, level, already_processed, namespaceprefix_='', name_='FileList'): + pass + def exportChildren(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='FileList', fromsubclass_=False, pretty_print=True): + if pretty_print: + eol_ = '\n' + else: + eol_ = '' + for file_ in self.file: + file_.export(outfile, level, namespaceprefix_, namespacedef_='', name_='file', pretty_print=pretty_print) + def build(self, node): + already_processed = set() + self.buildAttributes(node, node.attrib, already_processed) + for child in node: + nodeName_ = Tag_pattern_.match(child.tag).groups()[-1] + self.buildChildren(child, node, nodeName_) + return self + def buildAttributes(self, node, attrs, already_processed): + pass + def buildChildren(self, child_, node, nodeName_, fromsubclass_=False): + if nodeName_ == 'file': + obj_ = File.factory(parent_object_=self) + obj_.build(child_) + self.file.append(obj_) + obj_.original_tagname_ = 'file' +# end class FileList + + +GDSClassesMapping = { + 'file-index': IndexFile, +} + + +USAGE_TEXT = """ +Usage: python .py [ -s ] +""" + + +def usage(): + print(USAGE_TEXT) + sys.exit(1) + + +def get_root_tag(node): + tag = Tag_pattern_.match(node.tag).groups()[-1] + rootClass = GDSClassesMapping.get(tag) + if rootClass is None: + rootClass = globals().get(tag) + return tag, rootClass + + +def parse(inFileName, silence=False): + parser = None + doc = parsexml_(inFileName, parser) + rootNode = doc.getroot() + rootTag, rootClass = get_root_tag(rootNode) + if rootClass is None: + rootTag = 'IndexFile' + rootClass = IndexFile + rootObj = rootClass.factory() + rootObj.build(rootNode) + # Enable Python to collect the space used by the DOM. + doc = None + if not silence: + sys.stdout.write('\n') + rootObj.export( + sys.stdout, 0, name_=rootTag, + namespacedef_='', + pretty_print=True) + return rootObj + + +def parseEtree(inFileName, silence=False): + parser = None + doc = parsexml_(inFileName, parser) + rootNode = doc.getroot() + rootTag, rootClass = get_root_tag(rootNode) + if rootClass is None: + rootTag = 'IndexFile' + rootClass = IndexFile + rootObj = rootClass.factory() + rootObj.build(rootNode) + # Enable Python to collect the space used by the DOM. + doc = None + mapping = {} + rootElement = rootObj.to_etree(None, name_=rootTag, mapping_=mapping) + reverse_mapping = rootObj.gds_reverse_node_mapping(mapping) + if not silence: + content = etree_.tostring( + rootElement, pretty_print=True, + xml_declaration=True, encoding="utf-8") + sys.stdout.write(content) + sys.stdout.write('\n') + return rootObj, rootElement, mapping, reverse_mapping + + +def parseString(inString, silence=False): + '''Parse a string, create the object tree, and export it. + + Arguments: + - inString -- A string. This XML fragment should not start + with an XML declaration containing an encoding. + - silence -- A boolean. If False, export the object. + Returns -- The root object in the tree. + ''' + parser = None + rootNode= parsexmlstring_(inString, parser) + rootTag, rootClass = get_root_tag(rootNode) + if rootClass is None: + rootTag = 'IndexFile' + rootClass = IndexFile + rootObj = rootClass.factory() + rootObj.build(rootNode) + # Enable Python to collect the space used by the DOM. + if not silence: + sys.stdout.write('\n') + rootObj.export( + sys.stdout, 0, name_=rootTag, + namespacedef_='') + return rootObj + + +def parseLiteral(inFileName, silence=False): + parser = None + doc = parsexml_(inFileName, parser) + rootNode = doc.getroot() + rootTag, rootClass = get_root_tag(rootNode) + if rootClass is None: + rootTag = 'IndexFile' + rootClass = IndexFile + rootObj = rootClass.factory() + rootObj.build(rootNode) + # Enable Python to collect the space used by the DOM. + doc = None + if not silence: + sys.stdout.write('#from indexFile import *\n\n') + sys.stdout.write('import indexFile as model_\n\n') + sys.stdout.write('rootObj = model_.rootClass(\n') + rootObj.exportLiteral(sys.stdout, 0, name_=rootTag) + sys.stdout.write(')\n') + return rootObj + + +def main(): + args = sys.argv[1:] + if len(args) == 1: + parse(args[0]) + else: + usage() + + +if __name__ == '__main__': + #import pdb; pdb.set_trace() + main() + + +__all__ = [ + "File", + "FileList", + "IndexFile" +] diff --git a/xsd/ee-bom.xsd b/xsd/ee-bom.xsd index 16d18bb..a357deb 100644 --- a/xsd/ee-bom.xsd +++ b/xsd/ee-bom.xsd @@ -6,6 +6,7 @@ + diff --git a/xsd/ee-catalog.xsd b/xsd/ee-catalog.xsd deleted file mode 100644 index 6da3558..0000000 --- a/xsd/ee-catalog.xsd +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/xsd/ee-index.xsd b/xsd/ee-index.xsd new file mode 100644 index 0000000..21dbeb2 --- /dev/null +++ b/xsd/ee-index.xsd @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3