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

77 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-12-01 11:26 +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 - 2026 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 # lobster-trace: SwRequirements.sw_req_reqif_render_table_options 

36 """Render configuration provider. 

37 """ 

38 

39 FORMAT_SPECIFIER_PLAIN = "plain" # Plain text format; no special formatting, e.g. for line breaks, lists, etc. 

40 FORMAT_SPECIFIER_MD = "md" # CommonMark Markdown format; https://spec.commonmark.org/0.31.2/ 

41 FORMAT_SPECIFIER_RST = "rst" # ReStructuredText format; https://docutils.sourceforge.io/rst.html 

42 FORMAT_SPECIFIER_GFM = "gfm" # GitHub Flavored Markdown format; https://github.github.com/gfm/ 

43 FORMAT_SPECIFIER_XHTML = "xhtml" # XHTML format; https://www.w3.org/TR/xhtml1/ 

44 FORMAT_SPECIFIER_PATH = "path" # File path format; the value is a path to an external file. 

45 

46 def __init__(self): 

47 """Constructs the render configuration provider. 

48 """ 

49 # The render configuration as dict. 

50 # 

51 # Example in JSON format: 

52 # { "renderCfg": [{ "package": "XX", "type": "YY", "attribute": "ZZ", "format": "md", 

53 # "tableOptions": { "border": "<css-style>", "headingStyle": "<css-style>" } }] } 

54 self._cfg = {} 

55 

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

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

58  

59 Args: 

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

61  

62 Returns: 

63 bool: True if successful, False otherwise. 

64 """ 

65 status = False 

66 

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

68 

69 try: 

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

71 self._cfg = json.load(f) 

72 status = True 

73 

74 except FileNotFoundError: 

75 pass 

76 

77 return status 

78 

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

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

81 

82 Args: 

83 item (dict): Single render configuration item. 

84 trlc_package (str): The TRLC package. 

85 

86 Returns: 

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

88 """ 

89 it_matches = False 

90 

91 if "package" in item: 

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

93 it_matches = True 

94 

95 return it_matches 

96 

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

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

99 

100 Args: 

101 item (dict): Single render configuration item. 

102 trlc_type (str): The TRLC type. 

103 

104 Returns: 

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

106 """ 

107 it_matches = False 

108 

109 if "type" in item: 

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

111 it_matches = True 

112 

113 return it_matches 

114 

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

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

117 

118 Args: 

119 item (dict): Single render configuration item. 

120 trlc_type (str): The TRLC type attribute. 

121 

122 Returns: 

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

124 """ 

125 it_matches = False 

126 

127 if "attribute" in item: 

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

129 it_matches = True 

130 

131 return it_matches 

132 

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

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

135 

136 Args: 

137 trlc_package (str): The TRLC package. 

138 trlc_type (str): The TRLC type. 

139 trlc_type_attribute (str): The TRLC type attribute. 

140 

141 Returns: 

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

143 """ 

144 format_specifier = RenderConfig.FORMAT_SPECIFIER_PLAIN 

145 

146 if "renderCfg" in self._cfg: 

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

148 match_list = [] 

149 

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

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

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

153 

154 # All of the available ones must match! 

155 if all(match_list): 

156 if "format" in item: 

157 format_specifier = item["format"] 

158 

159 # First match wins. 

160 break 

161 

162 return format_specifier 

163 

164 def get_table_options(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> dict: 

165 """Returns the table rendering options for the given TRLC package, type and attribute. 

166 

167 The returned dictionary may contain the following optional keys: 

168 

169 - ``"border"`` (str): CSS style value applied to the ``<table>`` element's ``style`` 

170 attribute (e.g. ``"border: 1px solid black; border-collapse: collapse;"``). 

171 - ``"headingStyle"`` (str): CSS style value applied to each ``<th>`` cell's ``style`` 

172 attribute (e.g. ``"background-color: #c0c0c0;"``). 

173 

174 Args: 

175 trlc_package (str): The TRLC package. 

176 trlc_type (str): The TRLC type. 

177 trlc_type_attribute (str): The TRLC type attribute. 

178 

179 Returns: 

180 dict: Table options dict, or empty dict if no table options are configured. 

181 """ 

182 table_options = {} 

183 

184 if "renderCfg" in self._cfg: 

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

186 match_list = [] 

187 

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

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

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

191 

192 # All of the available ones must match! 

193 if all(match_list): 

194 table_options = item.get("tableOptions", {}) 

195 

196 # First match wins. 

197 break 

198 

199 return table_options 

200 

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

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

203 

204 Args: 

205 trlc_package (str): The TRLC package. 

206 trlc_type (str): The TRLC type. 

207 trlc_type_attribute (str): The TRLC type attribute. 

208  

209 Returns: 

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

211 """ 

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

213 

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

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

216 

217 Args: 

218 trlc_package (str): The TRLC package. 

219 trlc_type (str): The TRLC type. 

220 trlc_type_attribute (str): The TRLC type attribute. 

221  

222 Returns: 

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

224 """ 

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

226 

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

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

229 

230 Args: 

231 trlc_package (str): The TRLC package. 

232 trlc_type (str): The TRLC type. 

233 trlc_type_attribute (str): The TRLC type attribute. 

234 

235 Returns: 

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

237 """ 

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

239 

240 def is_format_gfm(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> bool: 

241 """Checks if the given TRLC package, type and attribute should be rendered in GitHub Flavored Markdown format. 

242 

243 Args: 

244 trlc_package (str): The TRLC package. 

245 trlc_type (str): The TRLC type. 

246 trlc_type_attribute (str): The TRLC type attribute. 

247  

248 Returns: 

249 bool: True if the given TRLC attribute has GitHub Flavored Markdown format, otherwise False. 

250 """ 

251 return self.get_format_specifier(trlc_package, trlc_type, trlc_type_attribute) == self.FORMAT_SPECIFIER_GFM 

252 

253 def is_format_xhtml(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> bool: 

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

255 

256 Args: 

257 trlc_package (str): The TRLC package. 

258 trlc_type (str): The TRLC type. 

259 trlc_type_attribute (str): The TRLC type attribute. 

260 

261 Returns: 

262 bool: True if the given TRLC attribute has XHTML format, otherwise False. 

263 """ 

264 return self.get_format_specifier(trlc_package, trlc_type, trlc_type_attribute) == self.FORMAT_SPECIFIER_XHTML 

265 

266 def is_format_path(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> bool: 

267 """Checks if the given TRLC package, type and attribute should be rendered as a file path. 

268 

269 Args: 

270 trlc_package (str): The TRLC package. 

271 trlc_type (str): The TRLC type. 

272 trlc_type_attribute (str): The TRLC type attribute. 

273 

274 Returns: 

275 bool: True if the given TRLC attribute has path format, otherwise False. 

276 """ 

277 return self.get_format_specifier(trlc_package, trlc_type, trlc_type_attribute) == self.FORMAT_SPECIFIER_PATH 

278 

279 

280# Functions ******************************************************************** 

281 

282# Main *************************************************************************