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
« prev ^ index » next coverage.py v7.12.0, created at 2025-11-21 12:06 +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 - 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/>.
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"
39 FORMAT_SPECIFIER_MD = "md"
40 FORMAT_SPECIFIER_RST = "rst"
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 = {}
51 def load(self, file_name: str) -> bool:
52 """Loads the render configuration from the given file.
54 Args:
55 file_name (str): Path to the render configuration file.
57 Returns:
58 bool: True if successful, False otherwise.
59 """
60 status = False
62 log_verbose(f"Loading render configuration {file_name}.")
64 try:
65 with open(file_name, 'r', encoding='utf-8') as f:
66 self._cfg = json.load(f)
67 status = True
69 except FileNotFoundError:
70 pass
72 return status
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.
77 Args:
78 item (dict): Single render configuration item.
79 trlc_package (str): The TRLC package.
81 Returns:
82 bool: If the given TRLC package matches the package pattern in the given item.
83 """
84 it_matches = False
86 if "package" in item:
87 if re.match(item["package"], trlc_package):
88 it_matches = True
90 return it_matches
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.
95 Args:
96 item (dict): Single render configuration item.
97 trlc_type (str): The TRLC type.
99 Returns:
100 bool: If the given TRLC type matches the type pattern in the given item.
101 """
102 it_matches = False
104 if "type" in item:
105 if re.match(item["type"], trlc_type):
106 it_matches = True
108 return it_matches
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.
113 Args:
114 item (dict): Single render configuration item.
115 trlc_type (str): The TRLC type attribute.
117 Returns:
118 bool: If the given TRLC type attribute matches the type attribute pattern in the given item.
119 """
120 it_matches = False
122 if "attribute" in item:
123 if re.match(item["attribute"], trlc_type_attribute):
124 it_matches = True
126 return it_matches
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.
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.
136 Returns:
137 str: The format specifier for the given TRLC package, type and attribute.
138 """
139 format_specifier = RenderConfig.FORMAT_SPECIFIER_PLAIN
141 if "renderCfg" in self._cfg:
142 for item in self._cfg["renderCfg"]:
143 match_list = []
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))
149 # All of the available ones must match!
150 if all(match_list):
151 if "format" in item:
152 format_specifier = item["format"]
154 # First match wins.
155 break
157 return format_specifier
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.
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.
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
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.
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.
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
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.
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.
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
198# Functions ********************************************************************
200# Main *************************************************************************