Coverage for pyEDAA/IPXACT/Design.py: 41%
98 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-05-30 22:17 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-05-30 22:17 +0000
1# ==================================================================================================================== #
2# _____ ____ _ _ ___ ______ __ _ ____ _____ #
3# _ __ _ _| ____| _ \ / \ / \ |_ _| _ \ \/ / / \ / ___|_ _| #
4# | '_ \| | | | _| | | | |/ _ \ / _ \ | || |_) \ / / _ \| | | | #
5# | |_) | |_| | |___| |_| / ___ \ / ___ \ _ | || __// \ / ___ \ |___ | | #
6# | .__/ \__, |_____|____/_/ \_\/_/ \_(_)___|_| /_/\_\/_/ \_\____| |_| #
7# |_| |___/ #
8# ==================================================================================================================== #
9# Authors: #
10# Patrick Lehmann #
11# #
12# License: #
13# ==================================================================================================================== #
14# Copyright 2017-2025 Patrick Lehmann - Bötzingen, Germany #
15# Copyright 2016-2016 Patrick Lehmann - Dresden, Germany #
16# #
17# Licensed under the Apache License, Version 2.0 (the "License"); #
18# you may not use this file except in compliance with the License. #
19# You may obtain a copy of the License at #
20# #
21# http://www.apache.org/licenses/LICENSE-2.0 #
22# #
23# Unless required by applicable law or agreed to in writing, software #
24# distributed under the License is distributed on an "AS IS" BASIS, #
25# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
26# See the License for the specific language governing permissions and #
27# limitations under the License. #
28# #
29# SPDX-License-Identifier: Apache-2.0 #
30# ==================================================================================================================== #
31#
32from pathlib import Path
33from sys import version_info
34from textwrap import dedent
35from typing import List, ClassVar, Optional as Nullable
37from lxml.etree import _Element, QName
38from pyTooling.Decorators import export
39from pyTooling.Common import getFullyQualifiedName
41from pyEDAA.IPXACT import RootElement, __DEFAULT_SCHEMA__, VLNV, IPXACTSchema, Element, IPXACTException
44@export
45class Design(RootElement):
46 """Represents an IP-XACT design."""
48 _rootTagName: ClassVar[str] = "design"
50 _componentInstances: List
51 _interconnections: List
52 _adHocConnections: List
54 def __init__(
55 self,
56 designFile: Nullable[Path] = None,
57 parse: bool = False,
58 vlnv: Nullable[VLNV] = None,
59 description: Nullable[str] = None
60 ):
61 """
62 Instantiates a design structure.
64 :param vlnv: A Vendor-Library-Name-Version unique identified.
65 :param description: A description text.
66 """
67 self._componentInstances = []
68 self._interconnections = []
69 self._adHocConnections = []
71 super().__init__(designFile, parse, vlnv, description)
73 # if not isinstance(description, str):
74 # ex = TypeError(f"Parameter 'description' is not a string.")
75 # if version_info >= (3, 11): # pragma: no cover
76 # ex.add_note(f"Got type '{getFullyQualifiedName(description)}'.")
77 # raise ex
78 # elif description == "":
79 # raise ValueError(f"Parameter 'description' is empty.")
81 def Parse(self, element: _Element) -> None:
82 elementLocalname = QName(element).localname
83 if elementLocalname == "componentInstances":
84 pass
85 # for ipxactFileElement in element:
86 # self.AddItem(IpxactFile.FromXml(ipxactFileElement))
87 elif elementLocalname == "componentInstances": 87 ↛ 88line 87 didn't jump to line 88 because the condition on line 87 was never true
88 pass
89 elif elementLocalname == "interconnections":
90 pass
91 elif elementLocalname == "adHocConnections": 91 ↛ 94line 91 didn't jump to line 94 because the condition on line 91 was always true
92 pass
93 else:
94 raise IPXACTException(f"Unsupported tag '{elementLocalname}' at root-level.")
96 def AddItem(self, item) -> None:
97 if isinstance(item, ComponentInstance):
98 self._componentInstances.append(item)
99 elif isinstance(item, Interconnection):
100 self._interconnections.append(item)
101 elif isinstance(item, AdHocConnection):
102 self._adHocConnections.append(item)
103 else:
104 raise ValueError()
106 def ToXml(self, schema: IPXACTSchema = __DEFAULT_SCHEMA__) -> str:
107 """Converts the object's data into XML format."""
109 xmlns = schema.NamespacePrefix
110 buffer = dedent(f"""\
111 <?xml version="1.0" encoding="UTF-8"?>
112 <{xmlns}:design
113 \txmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
114 \txmlns:{xmlns}="{schema.SchemaUri}"
115 \txsi:schemaLocation="{schema.SchemaUri} {schema.SchemaUrl}">
116 {self._vlnv.ToXml(schema, isVersionedIdentifier=True)}
117 \t<{xmlns}:description>{self._description}</{xmlns}:description>
118 """)
120 if self._componentInstances:
121 buffer += f"\t<{xmlns}:componentInstances>\n"
122 for componentInstance in self._componentInstances:
123 buffer += componentInstance.ToXml(2, schema)
124 buffer += f"\t</{xmlns}:componentInstances>\n"
126 if self._interconnections:
127 buffer += f"\t<{xmlns}:interconnections>\n"
128 for interconnection in self._interconnections:
129 buffer += interconnection.ToXml(2, schema)
130 buffer += f"\t</{xmlns}:interconnections>\n"
132 if self._adHocConnections:
133 buffer += f"\t<{xmlns}:adHocConnections>\n"
134 for adHocConnection in self._adHocConnections:
135 buffer += adHocConnection.ToXml(2, schema)
136 buffer += f"\t</{xmlns}:adHocConnections>\n"
138 buffer += dedent(f"""\
139 </{xmlns}:design>
140 """)
142 return buffer
145@export
146class IpxactFile(Element):
147 """Represents a IP-XACT file."""
149 _name: str #: Name
150 _description: str #: Description
152 def __init__(self, vlnv, name, description):
153 """
154 Instantiates an ipxactFile structure.
156 :param vlnv: A Vendor-Library-Name-Version unique identified.
157 :param name: Name of the IP-XACT file.
158 :param description: A description text.
159 :raises TypeError: If parameter vlnv is not a VLNV.
160 :raises TypeError: If parameter name is not a string.
161 :raises ValueError: If parameter name is empty.
162 :raises TypeError: If parameter description is not a string.
163 :raises ValueError: If parameter description is empty.
164 """
165 super().__init__(vlnv)
167 if not isinstance(name, str):
168 ex = TypeError(f"Parameter 'name' is not a string.")
169 if version_info >= (3, 11): # pragma: no cover
170 ex.add_note(f"Got type '{getFullyQualifiedName(name)}'.")
171 raise ex
172 elif name == "":
173 raise ValueError(f"Parameter 'name' is empty.")
175 if not isinstance(description, str):
176 ex = TypeError(f"Parameter 'description' is not a string.")
177 if version_info >= (3, 11): # pragma: no cover
178 ex.add_note(f"Got type '{getFullyQualifiedName(description)}'.")
179 raise ex
180 elif description == "":
181 raise ValueError(f"Parameter 'description' is empty.")
183 self._name = name
184 self._description = description
186 def ToXml(self, indent: int = 0, schema: IPXACTSchema = __DEFAULT_SCHEMA__) -> str:
187 """Converts the object's data into XML format."""
189 # WORKAROUND:
190 # Python <=3.11:
191 # {'\t' * indent} is not supported by Python before 3.12 due to a backslash within {...}
192 indent = "\t" * indent
193 xmlns = schema.NamespacePrefix
194 return dedent(f"""\
195 {indent}<{xmlns}:ipxactFile>
196 {indent}\t{self._vlnv.ToXml(indent)}
197 {indent}\t<{xmlns}:name>{self._name}</{xmlns}:name>
198 {indent}\t<{xmlns}:description>{self._description}</{xmlns}:description>
199 {indent}</{xmlns}:ipxactFile>
200 """)
203@export
204class ComponentInstance(Element):
205 """Represents an IP-XACT component instance."""
207 def __init__(self, vlnv: VLNV) -> None:
208 super().__init__(vlnv)
210 def ToXml(self, indent: int = 0, schema: IPXACTSchema = __DEFAULT_SCHEMA__) -> str:
211 """Converts the object's data into XML format."""
213 return ""
216@export
217class Interconnection(Element):
218 """Represents an IP-XACT interconnection."""
220 def __init__(self, vlnv: VLNV) -> None:
221 super().__init__(vlnv)
223 def ToXml(self, indent: int = 0, schema: IPXACTSchema = __DEFAULT_SCHEMA__) -> str:
224 """Converts the object's data into XML format."""
226 return ""
229@export
230class AdHocConnection(Element):
231 """Represents an IP-XACT ad-hoc connection."""
233 def __init__(self, vlnv: VLNV) -> None:
234 super().__init__(vlnv)
236 def ToXml(self, indent: int = 0, schema: IPXACTSchema = __DEFAULT_SCHEMA__) -> str:
237 """Converts the object's data into XML format."""
239 return ""