diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2019-02-26 23:08:19 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2019-02-26 23:08:19 +0100 |
commit | 97c8bb9db96e27051f8746865f657408263db0b8 (patch) | |
tree | 604cb6e9b87e79f43e54940adbc83520e89940fe /src/ee/xml | |
parent | 80e0623913e87c6480049520590e424a831e0401 (diff) | |
download | ee-python-97c8bb9db96e27051f8746865f657408263db0b8.tar.gz ee-python-97c8bb9db96e27051f8746865f657408263db0b8.tar.bz2 ee-python-97c8bb9db96e27051f8746865f657408263db0b8.tar.xz ee-python-97c8bb9db96e27051f8746865f657408263db0b8.zip |
o Creating a PartDb that manages a file system directory with one xml
file per part.
o Switching xml-based code to use PartDb.
Diffstat (limited to 'src/ee/xml')
-rw-r--r-- | src/ee/xml/bomFile.py | 5 | ||||
-rw-r--r-- | src/ee/xml/bom_file_utils.py | 13 | ||||
-rw-r--r-- | src/ee/xml/indexFile.py (renamed from src/ee/xml/catalogFile.py) | 218 |
3 files changed, 121 insertions, 115 deletions
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/indexFile.py index bda29ae..d460db3 100644 --- a/src/ee/xml/catalogFile.py +++ b/src/ee/xml/indexFile.py @@ -9,15 +9,15 @@ # ('-f', '') # ('--no-dates', '') # ('--no-versions', '') -# ('--one-file-per-xsd', '') # ('--output-directory', 'src/ee/xml') +# ('-o', 'src/ee/xml/indexFile.py') # ('-m', '') # # Command line arguments: -# xsd/ee-catalog.xsd +# xsd/ee-index.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 +# 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 @@ -726,38 +726,38 @@ def _cast(typ, value): # -class CatalogFile(GeneratedsSuper): +class IndexFile(GeneratedsSuper): subclass = None superclass = None - def __init__(self, products=None, **kwargs_): + def __init__(self, files=None, **kwargs_): self.original_tagname_ = None self.parent_object_ = kwargs_.get('parent_object_') - self.products = products + self.files = files def factory(*args_, **kwargs_): if CurrentSubclassModule_ is not None: subclass = getSubclassFromModule_( - CurrentSubclassModule_, CatalogFile) + CurrentSubclassModule_, IndexFile) if subclass is not None: return subclass(*args_, **kwargs_) - if CatalogFile.subclass: - return CatalogFile.subclass(*args_, **kwargs_) + if IndexFile.subclass: + return IndexFile.subclass(*args_, **kwargs_) else: - return CatalogFile(*args_, **kwargs_) + return IndexFile(*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 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.products is not None + self.files 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') + 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: @@ -769,23 +769,23 @@ class CatalogFile(GeneratedsSuper): 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') + self.exportAttributes(outfile, level, already_processed, namespaceprefix_, name_='IndexFile') if self.hasContent_(): outfile.write('>%s' % (eol_, )) - self.exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='CatalogFile', pretty_print=pretty_print) + self.exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='IndexFile', pretty_print=pretty_print) showIndent(outfile, level, pretty_print) outfile.write('</%s%s>%s' % (namespaceprefix_, name_, eol_)) else: outfile.write('/>%s' % (eol_, )) - def exportAttributes(self, outfile, level, already_processed, namespaceprefix_='', name_='CatalogFile'): + def exportAttributes(self, outfile, level, already_processed, namespaceprefix_='', name_='IndexFile'): pass - def exportChildren(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='CatalogFile', fromsubclass_=False, pretty_print=True): + def exportChildren(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='IndexFile', 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) + 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) @@ -796,46 +796,46 @@ class CatalogFile(GeneratedsSuper): 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) + if nodeName_ == 'files': + obj_ = FileList.factory(parent_object_=self) obj_.build(child_) - self.products = obj_ - obj_.original_tagname_ = 'products' -# end class CatalogFile + self.files = obj_ + obj_.original_tagname_ = 'files' +# end class IndexFile -class Product(GeneratedsSuper): +class File(GeneratedsSuper): subclass = None superclass = None - def __init__(self, ref=None, **kwargs_): + def __init__(self, path=None, **kwargs_): self.original_tagname_ = None self.parent_object_ = kwargs_.get('parent_object_') - self.ref = ref + self.path = _cast(None, path) def factory(*args_, **kwargs_): if CurrentSubclassModule_ is not None: subclass = getSubclassFromModule_( - CurrentSubclassModule_, Product) + CurrentSubclassModule_, File) if subclass is not None: return subclass(*args_, **kwargs_) - if Product.subclass: - return Product.subclass(*args_, **kwargs_) + if File.subclass: + return File.subclass(*args_, **kwargs_) else: - return Product(*args_, **kwargs_) + return File(*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 get_path(self): + return self.path + def set_path(self, path): + self.path = path + pathProp = property(get_path, set_path) 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') + 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: @@ -847,24 +847,19 @@ class Product(GeneratedsSuper): 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') + self.exportAttributes(outfile, level, already_processed, namespaceprefix_, name_='File') 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) + self.exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='File', pretty_print=pretty_print) outfile.write('</%s%s>%s' % (namespaceprefix_, name_, eol_)) else: outfile.write('/>%s' % (eol_, )) - def exportAttributes(self, outfile, level, already_processed, namespaceprefix_='', name_='Product'): + 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 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</%sref>%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) @@ -873,58 +868,58 @@ class Product(GeneratedsSuper): self.buildChildren(child, node, nodeName_) return self def buildAttributes(self, node, attrs, already_processed): - pass + 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): - if nodeName_ == 'ref': - ref_ = child_.text - ref_ = self.gds_validate_string(ref_, node, 'ref') - self.ref = ref_ -# end class Product + pass +# end class File -class ProductList(GeneratedsSuper): +class FileList(GeneratedsSuper): subclass = None superclass = None - def __init__(self, product=None, **kwargs_): + def __init__(self, file=None, **kwargs_): self.original_tagname_ = None self.parent_object_ = kwargs_.get('parent_object_') - if product is None: - self.product = [] + if file is None: + self.file = [] else: - self.product = product + self.file = file def factory(*args_, **kwargs_): if CurrentSubclassModule_ is not None: subclass = getSubclassFromModule_( - CurrentSubclassModule_, ProductList) + CurrentSubclassModule_, FileList) if subclass is not None: return subclass(*args_, **kwargs_) - if ProductList.subclass: - return ProductList.subclass(*args_, **kwargs_) + if FileList.subclass: + return FileList.subclass(*args_, **kwargs_) else: - return ProductList(*args_, **kwargs_) + return FileList(*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 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.product + self.file ): return True else: return False - def export(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='ProductList', pretty_print=True): - imported_ns_def_ = GenerateDSNamespaceDefs_.get('ProductList') + 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: @@ -936,23 +931,23 @@ class ProductList(GeneratedsSuper): 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') + self.exportAttributes(outfile, level, already_processed, namespaceprefix_, name_='FileList') if self.hasContent_(): outfile.write('>%s' % (eol_, )) - self.exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='ProductList', pretty_print=pretty_print) + self.exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='FileList', pretty_print=pretty_print) showIndent(outfile, level, pretty_print) outfile.write('</%s%s>%s' % (namespaceprefix_, name_, eol_)) else: outfile.write('/>%s' % (eol_, )) - def exportAttributes(self, outfile, level, already_processed, namespaceprefix_='', name_='ProductList'): + def exportAttributes(self, outfile, level, already_processed, namespaceprefix_='', name_='FileList'): pass - def exportChildren(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='ProductList', fromsubclass_=False, pretty_print=True): + def exportChildren(self, outfile, level, namespaceprefix_='', namespacedef_='', name_='FileList', 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) + 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) @@ -963,15 +958,16 @@ class ProductList(GeneratedsSuper): 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) + if nodeName_ == 'file': + obj_ = File.factory(parent_object_=self) obj_.build(child_) - self.product.append(obj_) - obj_.original_tagname_ = 'product' -# end class ProductList + self.file.append(obj_) + obj_.original_tagname_ = 'file' +# end class FileList GDSClassesMapping = { + 'file-index': IndexFile, } @@ -999,8 +995,8 @@ def parse(inFileName, silence=False): rootNode = doc.getroot() rootTag, rootClass = get_root_tag(rootNode) if rootClass is None: - rootTag = 'CatalogFile' - rootClass = CatalogFile + rootTag = 'IndexFile' + rootClass = IndexFile rootObj = rootClass.factory() rootObj.build(rootNode) # Enable Python to collect the space used by the DOM. @@ -1020,8 +1016,8 @@ def parseEtree(inFileName, silence=False): rootNode = doc.getroot() rootTag, rootClass = get_root_tag(rootNode) if rootClass is None: - rootTag = 'CatalogFile' - rootClass = CatalogFile + rootTag = 'IndexFile' + rootClass = IndexFile rootObj = rootClass.factory() rootObj.build(rootNode) # Enable Python to collect the space used by the DOM. @@ -1051,8 +1047,8 @@ def parseString(inString, silence=False): rootNode= parsexmlstring_(inString, parser) rootTag, rootClass = get_root_tag(rootNode) if rootClass is None: - rootTag = 'CatalogFile' - rootClass = CatalogFile + rootTag = 'IndexFile' + rootClass = IndexFile rootObj = rootClass.factory() rootObj.build(rootNode) # Enable Python to collect the space used by the DOM. @@ -1070,15 +1066,15 @@ def parseLiteral(inFileName, silence=False): rootNode = doc.getroot() rootTag, rootClass = get_root_tag(rootNode) if rootClass is None: - rootTag = 'CatalogFile' - rootClass = CatalogFile + 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 catalogFile import *\n\n') - sys.stdout.write('import catalogFile as model_\n\n') + 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') @@ -1099,7 +1095,7 @@ if __name__ == '__main__': __all__ = [ - "CatalogFile", - "Product", - "ProductList" + "File", + "FileList", + "IndexFile" ] |