Coverage for src / pyTRLCConverter / render_config.py: 100%
59 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-02 12:20 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-02 12:20 +0000
1""" Provides the render configuration to all converters.
3 Author: Andreas Merkle (andreas.merkle@newtec.de)
4"""
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/>.
22# Imports **********************************************************************
23import json
24import re
26from pyTRLCConverter.logger import log_verbose
28# Variables ********************************************************************
30# Classes **********************************************************************
33class RenderConfig():
34 # lobster-trace: SwRequirements.sw_req_render_configuration
35 """Render configuration provider.
36 """
38 FORMAT_SPECIFIER_PLAIN = "plain" # Plain text format; no special formatting, e.g. for line breaks, lists, etc.
39 FORMAT_SPECIFIER_MD = "md" # CommonMark Markdown format; https://spec.commonmark.org/0.31.2/
40 FORMAT_SPECIFIER_RST = "rst" # ReStructuredText format; https://docutils.sourceforge.io/rst.html
41 FORMAT_SPECIFIER_GFM = "gfm" # GitHub Flavored Markdown format; https://github.github.com/gfm/
43 def __init__(self):
44 """Constructs the render configuration provider.
45 """
46 # The render configuration as dict.
47 #
48 # Example in JSON format:
49 # { "renderCfg": [{ "package": "XX", "type": "YY", "attribute": "ZZ", "format": "md" }] }
50 self._cfg = {}
52 def load(self, file_name: str) -> bool:
53 """Loads the render configuration from the given file.
55 Args:
56 file_name (str): Path to the render configuration file.
58 Returns:
59 bool: True if successful, False otherwise.
60 """
61 status = False
63 log_verbose(f"Loading render configuration {file_name}.")
65 try:
66 with open(file_name, 'r', encoding='utf-8') as f:
67 self._cfg = json.load(f)
68 status = True
70 except FileNotFoundError:
71 pass
73 return status
75 def _is_package_match(self, item: dict, trlc_package: str) -> bool:
76 """Checks if the given TRLC package matches the package pattern in the given item.
78 Args:
79 item (dict): Single render configuration item.
80 trlc_package (str): The TRLC package.
82 Returns:
83 bool: If the given TRLC package matches the package pattern in the given item.
84 """
85 it_matches = False
87 if "package" in item:
88 if re.match(item["package"], trlc_package):
89 it_matches = True
91 return it_matches
93 def _is_type_match(self, item: dict, trlc_type: str) -> bool:
94 """Checks if the given TRLC type matches the type pattern in the given item.
96 Args:
97 item (dict): Single render configuration item.
98 trlc_type (str): The TRLC type.
100 Returns:
101 bool: If the given TRLC type matches the type pattern in the given item.
102 """
103 it_matches = False
105 if "type" in item:
106 if re.match(item["type"], trlc_type):
107 it_matches = True
109 return it_matches
111 def _is_type_attribute_match(self, item: dict, trlc_type_attribute: str) -> bool:
112 """Checks if the given TRLC type attribute matches the type attribute pattern in the given item.
114 Args:
115 item (dict): Single render configuration item.
116 trlc_type (str): The TRLC type attribute.
118 Returns:
119 bool: If the given TRLC type attribute matches the type attribute pattern in the given item.
120 """
121 it_matches = False
123 if "attribute" in item:
124 if re.match(item["attribute"], trlc_type_attribute):
125 it_matches = True
127 return it_matches
129 def get_format_specifier(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> str:
130 """Returns the format specifier for the given TRLC package, type and attribute.
132 Args:
133 trlc_package (str): The TRLC package.
134 trlc_type (str): The TRLC type.
135 trlc_type_attribute (str): The TRLC type attribute.
137 Returns:
138 str: The format specifier for the given TRLC package, type and attribute.
139 """
140 format_specifier = RenderConfig.FORMAT_SPECIFIER_PLAIN
142 if "renderCfg" in self._cfg:
143 for item in self._cfg["renderCfg"]:
144 match_list = []
146 match_list.append(self._is_package_match(item, trlc_package))
147 match_list.append(self._is_type_match(item, trlc_type))
148 match_list.append(self._is_type_attribute_match(item, trlc_type_attribute))
150 # All of the available ones must match!
151 if all(match_list):
152 if "format" in item:
153 format_specifier = item["format"]
155 # First match wins.
156 break
158 return format_specifier
160 def is_format_plain(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> bool:
161 """Checks if the given TRLC package, type and attribute should be rendered in plain text format.
163 Args:
164 trlc_package (str): The TRLC package.
165 trlc_type (str): The TRLC type.
166 trlc_type_attribute (str): The TRLC type attribute.
168 Returns:
169 bool: True if the given TRLC attribute has plain text format, otherwise False.
170 """
171 return self.get_format_specifier(trlc_package, trlc_type, trlc_type_attribute) == self.FORMAT_SPECIFIER_PLAIN
173 def is_format_md(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> bool:
174 """Checks if the given TRLC package, type and attribute should be rendered in markdown format.
176 Args:
177 trlc_package (str): The TRLC package.
178 trlc_type (str): The TRLC type.
179 trlc_type_attribute (str): The TRLC type attribute.
181 Returns:
182 bool: True if the given TRLC attribute has Markdown format, otherwise False.
183 """
184 return self.get_format_specifier(trlc_package, trlc_type, trlc_type_attribute) == self.FORMAT_SPECIFIER_MD
186 def is_format_rst(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> bool:
187 """Checks if the given TRLC package, type and attribute should be rendered in reStructuredText format.
189 Args:
190 trlc_package (str): The TRLC package.
191 trlc_type (str): The TRLC type.
192 trlc_type_attribute (str): The TRLC type attribute.
194 Returns:
195 bool: True if the given TRLC attribute has reStructuredText format, otherwise False.
196 """
197 return self.get_format_specifier(trlc_package, trlc_type, trlc_type_attribute) == self.FORMAT_SPECIFIER_RST
199 def is_format_gfm(self, trlc_package: str, trlc_type: str, trlc_type_attribute: str) -> bool:
200 """Checks if the given TRLC package, type and attribute should be rendered in GitHub Flavored Markdown format.
202 Args:
203 trlc_package (str): The TRLC package.
204 trlc_type (str): The TRLC type.
205 trlc_type_attribute (str): The TRLC type attribute.
207 Returns:
208 bool: True if the given TRLC attribute has GitHub Flavored Markdown format, otherwise False.
209 """
210 return self.get_format_specifier(trlc_package, trlc_type, trlc_type_attribute) == self.FORMAT_SPECIFIER_GFM
212# Functions ********************************************************************
214# Main *************************************************************************