Coverage for pySystemRDLModel/__init__.py: 52%

68 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-22 22:25 +0000

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

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

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

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

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

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

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

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

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

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

14# Copyright 2023-2024 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-2024 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-2024, Patrick Lehmann" 

54__license__ = "Apache License, Version 2.0" 

55__version__ = "0.3.2" 

56 

57 

58@export 

59@unique 

60class SystemRDLVersion(Enum): 

61 """ 

62 An enumeration for all possible version numbers for SystemRDL. 

63 

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

65 enumeration value. 

66 

67 This enumeration supports compare operators. 

68 """ 

69 Any = -1 #: Any 

70 

71 SystemRDL2005 = 2005 #: SystemRDL-2005 

72 SystemRDL2009 = 2009 #: SystemRDL-2009 

73 SystemRDL2012 = 2012 #: SystemRDL-2012 

74 SystemRDL2017 = 2017 #: SystemRDL-2017 

75 

76 Latest = 10000 #: Latest SystemRDL (2017) 

77 

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

79 -1: Any, 

80 5: SystemRDL2005, 

81 9: SystemRDL2009, 

82 12: SystemRDL2012, 

83 17: SystemRDL2017, 

84 2005: SystemRDL2005, 

85 2009: SystemRDL2009, 

86 2012: SystemRDL2012, 

87 2017: SystemRDL2017, 

88 10000: Latest, 

89 "Any": Any, 

90 "05": SystemRDL2005, 

91 "09": SystemRDL2009, 

92 "12": SystemRDL2012, 

93 "17": SystemRDL2017, 

94 "2005": SystemRDL2005, 

95 "2009": SystemRDL2009, 

96 "2012": SystemRDL2012, 

97 "2017": SystemRDL2017, 

98 "Latest": SystemRDL2017, 

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

100 

101 def __init__(self, *_): 

102 """Patch the embedded MAP dictionary""" 

103 cls = self.__class__ 

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

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

106 cls.__VERSION_MAPPINGS__[k] = self 

107 

108 @classmethod 

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

110 """ 

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

112 

113 :param value: SystemRDL year code. 

114 :returns: Enumeration value. 

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

116 """ 

117 try: 

118 return cls.__VERSION_MAPPINGS__[value] 

119 except KeyError: 

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

121 

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

123 """ 

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

125 

126 :param other: Parameter to compare against. 

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

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

129 """ 

130 if isinstance(other, SystemRDLVersion): 

131 return self.value < other.value 

132 else: 

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

134 

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

136 """ 

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

138 

139 :param other: Parameter to compare against. 

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

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

142 """ 

143 if isinstance(other, SystemRDLVersion): 

144 return self.value <= other.value 

145 else: 

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

147 

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

149 """ 

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

151 

152 :param other: Parameter to compare against. 

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

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

155 """ 

156 if isinstance(other, SystemRDLVersion): 

157 return self.value > other.value 

158 else: 

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

160 

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

162 """ 

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

164 

165 :param other: Parameter to compare against. 

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

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

168 """ 

169 if isinstance(other, SystemRDLVersion): 

170 return self.value >= other.value 

171 else: 

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

173 

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

175 """ 

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

177 

178 :param other: Parameter to compare against. 

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

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

181 """ 

182 if isinstance(other, SystemRDLVersion): 

183 return self.value != other.value 

184 else: 

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

186 

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

188 """ 

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

190 

191 :param other: Parameter to compare against. 

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

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

194 """ 

195 if isinstance(other, SystemRDLVersion): 

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

197 return True 

198 else: 

199 return self.value == other.value 

200 else: 

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

202 

203 def __str__(self) -> str: 

204 """ 

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

206 

207 :return: Formatted SystemRDL version. 

208 """ 

209 if self.value == self.Any.value: 

210 return "SystemRDL'Any" 

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

212 return "SystemRDL'Latest" 

213 

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

215 return f"SystemRDL'{year}" 

216 

217 def __repr__(self) -> str: 

218 """ 

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

220 

221 :return: Formatted SystemRDL version. 

222 """ 

223 if self.value == self.Any.value: 

224 return "Any" 

225 elif self.value == self.Latest.value: 

226 return "Latest" 

227 else: 

228 return str(self.value)