Coverage for pyEDAA/IPXACT/Design.py: 38%

94 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-10-25 17:18 +0000

1# ==================================================================================================================== # 

2# _____ ____ _ _ ___ ______ __ _ ____ _____ # 

3# _ __ _ _| ____| _ \ / \ / \ |_ _| _ \ \/ / / \ / ___|_ _| # 

4# | '_ \| | | | _| | | | |/ _ \ / _ \ | || |_) \ / / _ \| | | | # 

5# | |_) | |_| | |___| |_| / ___ \ / ___ \ _ | || __// \ / ___ \ |___ | | # 

6# | .__/ \__, |_____|____/_/ \_\/_/ \_(_)___|_| /_/\_\/_/ \_\____| |_| # 

7# |_| |___/ # 

8# ==================================================================================================================== # 

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

13# ==================================================================================================================== # 

14# Copyright 2017-2026 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 textwrap import dedent 

34from typing import List, ClassVar, Optional as Nullable 

35 

36from lxml.etree import _Element, QName 

37from pyTooling.Decorators import export 

38from pyTooling.Common import getFullyQualifiedName 

39 

40from pyEDAA.IPXACT import RootElement, __DEFAULT_SCHEMA__, VLNV, IPXACTSchema, Element, IPXACTException 

41 

42 

43@export 

44class Design(RootElement): 

45 """Represents an IP-XACT design.""" 

46 

47 _ROOT_TAG_NAME: ClassVar[str] = "design" 

48 

49 _componentInstances: List 

50 _interconnections: List 

51 _adHocConnections: List 

52 

53 def __init__( 

54 self, 

55 designFile: Nullable[Path] = None, 

56 parse: bool = False, 

57 vlnv: Nullable[VLNV] = None, 

58 description: Nullable[str] = None 

59 ) -> None: 

60 """ 

61 Instantiates a design structure. 

62 

63 :param vlnv: A Vendor-Library-Name-Version unique identified. 

64 :param description: A description text. 

65 """ 

66 self._componentInstances = [] 

67 self._interconnections = [] 

68 self._adHocConnections = [] 

69 

70 super().__init__(designFile, parse, vlnv, description) 

71 

72 # if not isinstance(description, str): 

73 # ex = TypeError(f"Parameter 'description' is not a string.") 

74 # ex.add_note(f"Got type '{getFullyQualifiedName(description)}'.") 

75 # raise ex 

76 # elif description == "": 

77 # raise ValueError(f"Parameter 'description' is empty.") 

78 

79 def Parse(self, element: _Element) -> None: 

80 elementLocalname = QName(element).localname 

81 if elementLocalname == "componentInstances": 

82 pass 

83 # for ipxactFileElement in element: 

84 # self.AddItem(IpxactFile.FromXml(ipxactFileElement)) 

85 elif elementLocalname == "componentInstances": 85 ↛ 86line 85 didn't jump to line 86 because the condition on line 85 was never true

86 pass 

87 elif elementLocalname == "interconnections": 

88 pass 

89 elif elementLocalname == "adHocConnections": 89 ↛ 92line 89 didn't jump to line 92 because the condition on line 89 was always true

90 pass 

91 else: 

92 raise IPXACTException(f"Unsupported tag '{elementLocalname}' at root-level.") 

93 

94 def AddItem(self, item) -> None: 

95 if isinstance(item, ComponentInstance): 

96 self._componentInstances.append(item) 

97 elif isinstance(item, Interconnection): 

98 self._interconnections.append(item) 

99 elif isinstance(item, AdHocConnection): 

100 self._adHocConnections.append(item) 

101 else: 

102 raise ValueError() 

103 

104 def ToXml(self, schema: IPXACTSchema = __DEFAULT_SCHEMA__) -> str: 

105 """Converts the object's data into XML format.""" 

106 

107 xmlns = schema.NamespacePrefix 

108 buffer = dedent(f"""\ 

109 <?xml version="1.0" encoding="UTF-8"?> 

110 <{xmlns}:design 

111 \txmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

112 \txmlns:{xmlns}="{schema.SchemaUri}" 

113 \txsi:schemaLocation="{schema.SchemaUri} {schema.SchemaUrl}"> 

114 {self._vlnv.ToXml(schema, isVersionedIdentifier=True)} 

115 \t<{xmlns}:description>{self._description}</{xmlns}:description> 

116 """) 

117 

118 if self._componentInstances: 

119 buffer += f"\t<{xmlns}:componentInstances>\n" 

120 for componentInstance in self._componentInstances: 

121 buffer += componentInstance.ToXml(2, schema) 

122 buffer += f"\t</{xmlns}:componentInstances>\n" 

123 

124 if self._interconnections: 

125 buffer += f"\t<{xmlns}:interconnections>\n" 

126 for interconnection in self._interconnections: 

127 buffer += interconnection.ToXml(2, schema) 

128 buffer += f"\t</{xmlns}:interconnections>\n" 

129 

130 if self._adHocConnections: 

131 buffer += f"\t<{xmlns}:adHocConnections>\n" 

132 for adHocConnection in self._adHocConnections: 

133 buffer += adHocConnection.ToXml(2, schema) 

134 buffer += f"\t</{xmlns}:adHocConnections>\n" 

135 

136 buffer += dedent(f"""\ 

137 </{xmlns}:design> 

138 """) 

139 

140 return buffer 

141 

142 

143@export 

144class IpxactFile(Element): 

145 """Represents a IP-XACT file.""" 

146 

147 _name: str #: Name 

148 _description: str #: Description 

149 

150 def __init__(self, vlnv, name, description) -> None: 

151 """ 

152 Instantiates an ipxactFile structure. 

153 

154 :param vlnv: A Vendor-Library-Name-Version unique identified. 

155 :param name: Name of the IP-XACT file. 

156 :param description: A description text. 

157 :raises TypeError: If parameter vlnv is not a VLNV. 

158 :raises TypeError: If parameter name is not a string. 

159 :raises ValueError: If parameter name is empty. 

160 :raises TypeError: If parameter description is not a string. 

161 :raises ValueError: If parameter description is empty. 

162 """ 

163 super().__init__(vlnv) 

164 

165 if not isinstance(name, str): 

166 ex = TypeError("Parameter 'name' is not a string.") 

167 ex.add_note(f"Got type '{getFullyQualifiedName(name)}'.") 

168 raise ex 

169 elif name == "": 

170 raise ValueError("Parameter 'name' is empty.") 

171 

172 if not isinstance(description, str): 

173 ex = TypeError("Parameter 'description' is not a string.") 

174 ex.add_note(f"Got type '{getFullyQualifiedName(description)}'.") 

175 raise ex 

176 elif description == "": 

177 raise ValueError("Parameter 'description' is empty.") 

178 

179 self._name = name 

180 self._description = description 

181 

182 def ToXml(self, indent: int = 0, schema: IPXACTSchema = __DEFAULT_SCHEMA__) -> str: 

183 """Converts the object's data into XML format.""" 

184 

185 # WORKAROUND: 

186 # Python <=3.11: 

187 # {'\t' * indent} is not supported by Python before 3.12 due to a backslash within {...} 

188 indent = "\t" * indent 

189 xmlns = schema.NamespacePrefix 

190 return dedent(f"""\ 

191 {indent}<{xmlns}:ipxactFile> 

192 {indent}\t{self._vlnv.ToXml(indent)} 

193 {indent}\t<{xmlns}:name>{self._name}</{xmlns}:name> 

194 {indent}\t<{xmlns}:description>{self._description}</{xmlns}:description> 

195 {indent}</{xmlns}:ipxactFile> 

196 """) 

197 

198 

199@export 

200class ComponentInstance(Element): 

201 """Represents an IP-XACT component instance.""" 

202 

203 def __init__(self, vlnv: VLNV) -> None: 

204 super().__init__(vlnv) 

205 

206 def ToXml(self, indent: int = 0, schema: IPXACTSchema = __DEFAULT_SCHEMA__) -> str: 

207 """Converts the object's data into XML format.""" 

208 

209 return "" 

210 

211 

212@export 

213class Interconnection(Element): 

214 """Represents an IP-XACT interconnection.""" 

215 

216 def __init__(self, vlnv: VLNV) -> None: 

217 super().__init__(vlnv) 

218 

219 def ToXml(self, indent: int = 0, schema: IPXACTSchema = __DEFAULT_SCHEMA__) -> str: 

220 """Converts the object's data into XML format.""" 

221 

222 return "" 

223 

224 

225@export 

226class AdHocConnection(Element): 

227 """Represents an IP-XACT ad-hoc connection.""" 

228 

229 def __init__(self, vlnv: VLNV) -> None: 

230 super().__init__(vlnv) 

231 

232 def ToXml(self, indent: int = 0, schema: IPXACTSchema = __DEFAULT_SCHEMA__) -> str: 

233 """Converts the object's data into XML format.""" 

234 

235 return ""