Coverage for pySystemRDLModel/__init__.py: 54%

71 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-07-05 00:30 +0000

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

2# ____ _ ____ ____ _ __ __ _ _ # 

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

4# | '_ \| | | \___ \| | | / __| __/ _ \ '_ ` _ \| |_) | | | | | | |\/| |/ _ \ / _` |/ _ \ | # 

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

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

7# |_| |___/ |___/ # 

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

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

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

14# Copyright 2023-2026 Patrick Lehmann - Boetzingen, Germany # 

15# # 

16# Licensed under the Apache License, Version 2.0 (the "License"); # 

17# you may not use this file except in compliance with the License. # 

18# You may obtain a copy of the License at # 

19# # 

20# http://www.apache.org/licenses/LICENSE-2.0 # 

21# # 

22# Unless required by applicable law or agreed to in writing, software # 

23# distributed under the License is distributed on an "AS IS" BASIS, # 

24# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

25# See the License for the specific language governing permissions and # 

26# limitations under the License. # 

27# # 

28# SPDX-License-Identifier: Apache-2.0 # 

29# ==================================================================================================================== # 

30# 

31""" 

32**An abstract SystemRDL language model.** 

33 

34This package provides a unified abstract language model for SystemRDL. Projects reading from source files can derive own 

35classes and implement additional logic to create a concrete language model for their tools. 

36 

37Projects consuming pre-processed SystemRDL data can build higher level features and services on such a model, while 

38supporting multiple frontends. 

39 

40.. admonition:: Copyright Information 

41 

42 :copyright: Copyright 2023-2026 Patrick Lehmann - Bötzingen, Germany 

43 :license: Apache License, Version 2.0 

44""" 

45from enum import unique, Enum 

46from typing import Dict, Union 

47 

48from pyTooling.Decorators import export 

49 

50 

51__author__ = "Patrick Lehmann" 

52__email__ = "Paebbels@gmail.com" 

53__copyright__ = "2023-2026, Patrick Lehmann" 

54__license__ = "Apache License, Version 2.0" 

55__version__ = "0.3.10" 

56# __keywords__ = [] 

57__project_url__ = "https://github.com/edaa-org/pySystemRDLModel" 

58__documentation_url__ = "https://edaa-org.github.io/pySystemRDLModel" 

59__issue_tracker_url__ = "https://GitHub.com/edaa-org/pySystemRDLModel/issues" 

60 

61 

62@export 

63@unique 

64class SystemRDLVersion(Enum): 

65 """ 

66 An enumeration for all possible version numbers for SystemRDL. 

67 

68 A version can be given as integer or string and is represented as a unified 

69 enumeration value. 

70 

71 This enumeration supports compare operators. 

72 """ 

73 Any = -1 #: Any 

74 

75 SystemRDL2005 = 2005 #: SystemRDL-2005 

76 SystemRDL2009 = 2009 #: SystemRDL-2009 

77 SystemRDL2012 = 2012 #: SystemRDL-2012 

78 SystemRDL2017 = 2017 #: SystemRDL-2017 

79 

80 Latest = 10000 #: Latest SystemRDL (2017) 

81 

82 __VERSION_MAPPINGS__: Dict[Union[int, str], Enum] = { 

83 -1: Any, 

84 5: SystemRDL2005, 

85 9: SystemRDL2009, 

86 12: SystemRDL2012, 

87 17: SystemRDL2017, 

88 2005: SystemRDL2005, 

89 2009: SystemRDL2009, 

90 2012: SystemRDL2012, 

91 2017: SystemRDL2017, 

92 10000: Latest, 

93 "Any": Any, 

94 "05": SystemRDL2005, 

95 "09": SystemRDL2009, 

96 "12": SystemRDL2012, 

97 "17": SystemRDL2017, 

98 "2005": SystemRDL2005, 

99 "2009": SystemRDL2009, 

100 "2012": SystemRDL2012, 

101 "2017": SystemRDL2017, 

102 "Latest": SystemRDL2017, 

103 } #: Dictionary of SystemRDL year codes variants as integer and strings for mapping to unique enum values. 

104 

105 def __init__(self, *_) -> None: 

106 """Patch the embedded MAP dictionary""" 

107 cls = self.__class__ 

108 for k, v in cls.__VERSION_MAPPINGS__.items(): 

109 if (not isinstance(v, cls)) and (v == self.value): 

110 cls.__VERSION_MAPPINGS__[k] = self 

111 

112 @classmethod 

113 def Parse(cls, value: Union[int, str]) -> "SystemRDLVersion": 

114 """ 

115 Parses a SystemRDL year code as integer or string to an enum value. 

116 

117 :param value: SystemRDL year code. 

118 :returns: Enumeration value. 

119 :raises ValueError: If the year code is not recognized. 

120 """ 

121 try: 

122 return cls.__VERSION_MAPPINGS__[value] 

123 except KeyError: 

124 raise ValueError(f"Value '{value!s}' cannot be parsed to member of {cls.__name__}.") 

125 

126 def __lt__(self, other: Any) -> bool: 

127 """ 

128 Compare two SystemRDL versions if the version is less than the second operand. 

129 

130 :param other: Parameter to compare against. 

131 :returns: True if version is less than the second operand. 

132 :raises TypeError: If parameter ``other`` is not of type :class:`SystemRDLVersion`. 

133 """ 

134 if isinstance(other, SystemRDLVersion): 

135 return self.value < other.value 

136 else: 

137 raise TypeError("Second operand is not of type 'SystemRDLVersion'.") 

138 

139 def __le__(self, other: Any) -> bool: 

140 """ 

141 Compare two SystemRDL versions if the version is less or equal than the second operand. 

142 

143 :param other: Parameter to compare against. 

144 :returns: True if version is less or equal than the second operand. 

145 :raises TypeError: If parameter ``other`` is not of type :class:`SystemRDLVersion`. 

146 """ 

147 if isinstance(other, SystemRDLVersion): 

148 return self.value <= other.value 

149 else: 

150 raise TypeError("Second operand is not of type 'SystemRDLVersion'.") 

151 

152 def __gt__(self, other: Any) -> bool: 

153 """ 

154 Compare two SystemRDL versions if the version is greater than the second operand. 

155 

156 :param other: Parameter to compare against. 

157 :returns: True if version is greater than the second operand. 

158 :raises TypeError: If parameter ``other`` is not of type :class:`SystemRDLVersion`. 

159 """ 

160 if isinstance(other, SystemRDLVersion): 

161 return self.value > other.value 

162 else: 

163 raise TypeError("Second operand is not of type 'SystemRDLVersion'.") 

164 

165 def __ge__(self, other: Any) -> bool: 

166 """ 

167 Compare two SystemRDL versions if the version is greater or equal than the second operand. 

168 

169 :param other: Parameter to compare against. 

170 :returns: True if version is greater or equal than the second operand. 

171 :raises TypeError: If parameter ``other`` is not of type :class:`SystemRDLVersion`. 

172 """ 

173 if isinstance(other, SystemRDLVersion): 

174 return self.value >= other.value 

175 else: 

176 raise TypeError("Second operand is not of type 'SystemRDLVersion'.") 

177 

178 def __ne__(self, other: Any) -> bool: 

179 """ 

180 Compare two SystemRDL versions if the version is unequal to the second operand. 

181 

182 :param other: Parameter to compare against. 

183 :returns: True if version is unequal to the second operand. 

184 :raises TypeError: If parameter ``other`` is not of type :class:`SystemRDLVersion`. 

185 """ 

186 if isinstance(other, SystemRDLVersion): 

187 return self.value != other.value 

188 else: 

189 raise TypeError("Second operand is not of type 'SystemRDLVersion'.") 

190 

191 def __eq__(self, other: Any) -> bool: 

192 """ 

193 Compare two SystemRDL versions if the version is equal to the second operand. 

194 

195 :param other: Parameter to compare against. 

196 :returns: True if version is equal to the second operand. 

197 :raises TypeError: If parameter ``other`` is not of type :class:`SystemRDLVersion`. 

198 """ 

199 if isinstance(other, SystemRDLVersion): 

200 if (self is self.__class__.Any) or (other is self.__class__.Any): 

201 return True 

202 else: 

203 return self.value == other.value 

204 else: 

205 raise TypeError("Second operand is not of type 'SystemRDLVersion'.") 

206 

207 def __str__(self) -> str: 

208 """ 

209 Formats the SystemRDLVersion version to pattern ``SystemRDL'xx``. 

210 

211 :returns: Formatted SystemRDL version. 

212 """ 

213 if self.value == self.Any.value: 

214 return "SystemRDL'Any" 

215 if self.value == self.Latest.value: 215 ↛ 216line 215 didn't jump to line 216 because the condition on line 215 was never true

216 return "SystemRDL'Latest" 

217 

218 year = str(self.value)[-2:] 

219 return f"SystemRDL'{year}" 

220 

221 def __repr__(self) -> str: 

222 """ 

223 Formats the SystemRDL version to pattern ``xxxx``. 

224 

225 :returns: Formatted SystemRDL version. 

226 """ 

227 if self.value == self.Any.value: 

228 return "Any" 

229 elif self.value == self.Latest.value: 

230 return "Latest" 

231 else: 

232 return str(self.value)