3232from pathlib import Path
3333from sys import version_info
3434from textwrap import dedent
35- from typing import List , Dict
35+ from typing import List , Dict , Optional as Nullable , ClassVar
3636
37- from lxml .etree import XMLParser , XML , XMLSchema , ElementTree , QName
37+ from lxml .etree import QName , _Element
3838
3939from pyTooling .Decorators import export , readonly
4040from pyTooling .Common import getFullyQualifiedName
4141
42- from pyEDAA .IPXACT import RootElement , VLNV , IPXACTException , __URI_MAP__ , __DEFAULT_SCHEMA__ , IPXACTSchema , Element
42+ from pyEDAA .IPXACT import NamedElement , RootElement , VLNV , IPXACTException , __DEFAULT_SCHEMA__ , IPXACTSchema , Element
4343from pyEDAA .IPXACT .Component import Component
4444
4545
4646@export
47- class IpxactFile (Element ):
47+ class IpxactFile (NamedElement ):
4848 """Represents a IP-XACT file."""
4949
5050 _name : str #: Name
@@ -135,7 +135,8 @@ def ToXml(self, indent: int = 0, schema: IPXACTSchema = __DEFAULT_SCHEMA__):
135135class Catalog (RootElement ):
136136 """Represents an IP-XACT catalog."""
137137
138- _description : str
138+ _rootTagName : ClassVar [str ] = "catalog"
139+
139140 _abstractionDefinitions : List
140141 _abstractors : List
141142 _busInterfaces : List
@@ -145,10 +146,13 @@ class Catalog(RootElement):
145146 _designs : List
146147 _generatorChains : List
147148
148- def __init__ (self , vlnv : VLNV , description : str ):
149- super ().__init__ (vlnv )
150-
151- self ._description = description
149+ def __init__ (
150+ self ,
151+ catalogFile : Nullable [Path ] = None ,
152+ parse : bool = False ,
153+ vlnv : Nullable [VLNV ] = None ,
154+ description : Nullable [str ] = None
155+ ):
152156 self ._abstractionDefinitions = []
153157 self ._abstractors = []
154158 self ._busInterfaces = []
@@ -158,73 +162,27 @@ def __init__(self, vlnv: VLNV, description: str):
158162 self ._designs = []
159163 self ._generatorChains = []
160164
161- @classmethod
162- def FromFile (cls , filePath : Path ):
163- """Constructs an instance of ``Catalog`` from a file."""
164-
165- if not filePath .exists ():
166- raise IPXACTException (f"IPXACT file '{ filePath } ' not found." ) from FileNotFoundError (str (filePath ))
167-
168- try :
169- with filePath .open ("rb" ) as fileHandle :
170- content = fileHandle .read ()
171- except OSError as ex :
172- raise IPXACTException (f"Couldn't open '{ filePath } '." ) from ex
173-
174- xmlParser = XMLParser (remove_blank_text = True , encoding = "utf-8" )
175- root = XML (content , parser = xmlParser , base_url = filePath .resolve ().as_uri ()) # - relative paths are not supported
176- rootTag = QName (root .tag )
177-
178- if rootTag .localname != "catalog" :
179- raise IPXACTException ("The input IP-XACT file is not a catalog file." )
180-
181- namespacePrefix = root .prefix
182- namespaceURI = root .nsmap [namespacePrefix ]
183- if namespaceURI in __URI_MAP__ :
184- ipxactSchema = __URI_MAP__ [namespaceURI ]
165+ super ().__init__ (catalogFile , parse , vlnv , description )
166+
167+ def Parse (self , element : _Element ) -> None :
168+ elementLocalname = QName (element ).localname
169+ if elementLocalname == "catalogs" :
170+ for ipxactFileElement in element :
171+ self .AddItem (IpxactFile .FromXml (ipxactFileElement ))
172+ elif elementLocalname == "abstractionDefinitions" :
173+ pass
174+ elif elementLocalname == "components" :
175+ pass
176+ elif elementLocalname == "abstractors" :
177+ pass
178+ elif elementLocalname == "designs" :
179+ pass
180+ elif elementLocalname == "designConfigurations" :
181+ pass
182+ elif elementLocalname == "generatorChains" :
183+ pass
185184 else :
186- raise IPXACTException (f"The input IP-XACT file uses an unsupported namespace: '{ namespaceURI } '." )
187-
188- try :
189- with ipxactSchema .LocalPath .open ("rb" ) as fileHandle :
190- schema = fileHandle .read ()
191- except OSError as ex :
192- raise IPXACTException (f"Couldn't open IP-XACT schema '{ ipxactSchema .LocalPath } ' for { namespacePrefix } ({ namespaceURI } )." ) from ex
193-
194- schemaRoot = XML (schema , parser = xmlParser , base_url = ipxactSchema .LocalPath .as_uri ())
195- schemaTree = ElementTree (schemaRoot )
196- xmlSchema = XMLSchema (schemaTree )
197-
198- try :
199- xmlSchema .assertValid (root )
200- except Exception as ex :
201- raise IPXACTException ("The input IP-XACT file is not valid." ) from ex
202-
203- items = []
204- for rootElements in root :
205- element = QName (rootElements )
206- if element .localname == "vendor" :
207- vendor = rootElements .text
208- elif element .localname == "library" :
209- library = rootElements .text
210- elif element .localname == "name" :
211- name = rootElements .text
212- elif element .localname == "version" :
213- version = rootElements .text
214- elif element .localname == "description" :
215- description = rootElements .text
216- elif element .localname == "catalogs" :
217- for ipxactFileElement in rootElements :
218- items .append (IpxactFile .FromXml (ipxactFileElement ))
219- else :
220- raise IPXACTException (f"Unsupported tag '{ element .localname } ' at root-level." )
221-
222- vlnv = VLNV (vendor = vendor , library = library , name = name , version = version )
223- catalog = cls (vlnv , description = description )
224- for item in items :
225- catalog .AddItem (item )
226-
227- return catalog
185+ raise IPXACTException (f"Unsupported tag '{ elementLocalname } ' at root-level." )
228186
229187 def AddItem (self , item ) -> None :
230188 if isinstance (item , IpxactFile ):
0 commit comments