Coverage for src / pyTRLCConverter / render_config.py: 100%

56 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-21 12:06 +0000

1""" Provides the render configuration to all converters. 

2 

3 Author: Andreas Merkle (andreas.merkle@newtec.de) 

4""" 

5 

6# pyTRLCConverter - A tool to convert TRLC files to specific formats. 

7# Copyright (c) 2024 - 2025 NewTec GmbH 

8# 

9# This file is part of pyTRLCConverter program. 

10# 

11# The pyTRLCConverter program is free software: you can redistribute it and/or modify it under 

12# the terms of the GNU General Public License as published by the Free Software Foundation, 

13# either version 3 of the License, or (at your option) any later version. 

14# 

15# The pyTRLCConverter program is distributed in the hope that it will be useful, but 

16# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

17# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License along with pyTRLCConverter. 

20# If not, see <https://www.gnu.org/licenses/>. 

21 

22# Imports ********************************************************************** 

23import json 

24import re 

25 

26from pyTRLCConverter.logger import log_verbose 

27 

28# Variables ******************************************************************** 

29 

30# Classes ********************************************************************** 

31 

32 

33class RenderConfig(): 

34 # lobster-trace: SwRequirements.sw_req_render_configuration 

35 """Render configuration provider. 

36 """ 

37 

38 FORMAT_SPECIFIER_PLAIN = "plain" 

39 FORMAT_SPECIFIER_MD = "md" 

40 FORMAT_SPECIFIER_RST = "rst" 

41 

42 def __init__(self): 

43 """Constructs the render configuration provider. 

44 """ 

45 # The render configuration as dict. 

46 # 

47 # Example in JSON format: 

48 # { "renderCfg": [{ "package": "XX", "type": "YY", "attribute": "ZZ", "format": "md" }] } 

49 self._cfg = {} 

50 

51 def load(self, file_name: str) -> bool: 

52 """Loads the render configuration from the given file. 

53  

54 Args: 

55 file_name (str): Path to the render configuration file. 

56  

57 Returns: 

58 bool: True if successful, False otherwise. 

59 """ 

60 status = False 

61 

62 log_verbose(f"Loading render configuration {file_name}.") 

63 

64 try: 

65 with open(file_name, 'r', encoding='utf-8') as f: 

66 self._cfg = json.load(f) 

67 status = True 

68 

69 except FileNotFoundError: 

70 pass 

71 

72 return status 

73 

74 def _is_package_match(self, item: dict, trlc_package: str) -> bool: 

75 """Checks if the given TRLC package matches the package pattern in the given item. 

76 

77 Args: 

78 item (dict): Single render configuration item. 

79 trlc_package (str): The TRLC package. 

80 

81 Returns: 

82 bool: If the given TRLC package matches the package pattern in the given item. 

83 """ 

84 it_matches = False 

85 

86 if "package" in item: 

87 if re.match(item["package"], trlc_package): 

88 it_matches = True 

89 

90 return it_matches 

91 

92 def _is_type_match(self, item: dict, trlc_type: str) -> bool: 

93 """Checks if the given TRLC type matches the type pattern in the given item. 

94 

95 Args: 

96 item (dict): Single render configuration item. 

97 trlc_type (str): The TRLC type. 

98 

99 Returns: 

100 bool: If the given TRLC type matches the type pattern in the given item. 

101 """ 

102 it_matches = False 

103 

104 if "type" in item: 

105 if re.match(item["type"], trlc_type): 

106 it_matches = True 

107 

108 return it_matches 

109 

110 def _is_type_attribute_match(self, item: dict, trlc_type_attribute: str) -> bool: 

111 """Checks if the given TRLC type attribute matches the type attribute pattern in the given item. 

112 

113 Args: 

114 item (dict): Single render configuration item. 

115 trlc_type (str): The TRLC type attribute. 

116 

117 Returns: 

118 bool: If the given TRLC type attribute matches the type attribute pattern in the given item. 

119 """ 

120 it_matches = False 

121 

122 if "attribute" in item: 

123 if re.match(item["attribute"], trlc_type_attribute): 

124 it_matches = True 

125 

126 return it_matches 

127 

128 def get_format_specifier(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> str: 

129 """Returns the format specifier for the given TRLC package, type and attribute. 

130 

131 Args: 

132 trlc_package (str): The TRLC package. 

133 trlc_type (str): The TRLC type. 

134 trlc_type_attribute (str): The TRLC type attribute. 

135 

136 Returns: 

137 str: The format specifier for the given TRLC package, type and attribute. 

138 """ 

139 format_specifier = RenderConfig.FORMAT_SPECIFIER_PLAIN 

140 

141 if "renderCfg" in self._cfg: 

142 for item in self._cfg["renderCfg"]: 

143 match_list = [] 

144 

145 match_list.append(self._is_package_match(item, trlc_package)) 

146 match_list.append(self._is_type_match(item, trlc_type)) 

147 match_list.append(self._is_type_attribute_match(item, trlc_type_attribute)) 

148 

149 # All of the available ones must match! 

150 if all(match_list): 

151 if "format" in item: 

152 format_specifier = item["format"] 

153 

154 # First match wins. 

155 break 

156 

157 return format_specifier 

158 

159 def is_format_plain(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> bool: 

160 """Checks if the given TRLC package, type and attribute should be rendered in plain text format. 

161 

162 Args: 

163 trlc_package (str): The TRLC package. 

164 trlc_type (str): The TRLC type. 

165 trlc_type_attribute (str): The TRLC type attribute. 

166  

167 Returns: 

168 bool: True if the given TRLC attribute has plain text format, otherwise False. 

169 """ 

170 return self.get_format_specifier(trlc_package, trlc_type, trlc_type_attribute) == self.FORMAT_SPECIFIER_PLAIN 

171 

172 def is_format_md(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> bool: 

173 """Checks if the given TRLC package, type and attribute should be rendered in markdown format. 

174 

175 Args: 

176 trlc_package (str): The TRLC package. 

177 trlc_type (str): The TRLC type. 

178 trlc_type_attribute (str): The TRLC type attribute. 

179  

180 Returns: 

181 bool: True if the given TRLC attribute has Markdown format, otherwise False. 

182 """ 

183 return self.get_format_specifier(trlc_package, trlc_type, trlc_type_attribute) == self.FORMAT_SPECIFIER_MD 

184 

185 def is_format_rst(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> bool: 

186 """Checks if the given TRLC package, type and attribute should be rendered in reStructuredText format. 

187 

188 Args: 

189 trlc_package (str): The TRLC package. 

190 trlc_type (str): The TRLC type. 

191 trlc_type_attribute (str): The TRLC type attribute. 

192 

193 Returns: 

194 bool: True if the given TRLC attribute has reStructuredText format, otherwise False. 

195 """ 

196 return self.get_format_specifier(trlc_package, trlc_type, trlc_type_attribute) == self.FORMAT_SPECIFIER_RST 

197 

198# Functions ******************************************************************** 

199 

200# Main *************************************************************************