Coverage for src/pyTRLCConverter/translator.py: 69%

29 statements  

« prev     ^ index     » next       coverage.py v7.10.3, created at 2025-08-14 10:59 +0000

1""" 

2This module implements the requirement attribute translator. 

3 

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

5""" 

6 

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

8# Copyright (c) 2024 - 2025 NewTec GmbH 

9# 

10# This file is part of pyTRLCConverter program. 

11# 

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

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

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

15# 

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

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

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

19# 

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

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

22 

23# Imports ********************************************************************** 

24import json 

25from typing import Dict, Optional 

26from pyTRLCConverter.logger import log_verbose 

27 

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

29 

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

31 

32class Translator(): 

33 # lobster-trace: SwRequirements.sw_req_translation 

34 """ 

35 This class implements the requirement attribute translator. 

36 """ 

37 

38 def __init__(self): 

39 """ 

40 Constructs the requirement attribute translator. 

41 """ 

42 self._translation = {} 

43 

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

45 """ 

46 Load the trasnlation JSON file. 

47 

48 Args: 

49 file_name (str): The name of the JSON file to load. 

50 

51 Returns: 

52 bool: True if the file was loaded successfully, False otherwise. 

53 """ 

54 status = False 

55 

56 log_verbose(f"Loading translation file {file_name}.") 

57 

58 # Load the JSON file 

59 try: 

60 with open(file_name, 'r', encoding="utf-8") as file: 

61 self._translation = json.load(file) 

62 

63 status = True 

64 

65 except FileNotFoundError as e: 

66 log_verbose(f"Failed to load file {file_name}: {e}") 

67 

68 return status 

69 

70 def get_translation(self, req_type_name: str) -> Optional[Dict]: 

71 """ 

72 Get the translation for a specific requirement type. 

73 

74 Args: 

75 req_type_name (str): The name of the requirement type. 

76 

77 Returns: 

78 Optional[Dict]: The translation dictionary for the requirement type, or None if not found. 

79 """ 

80 translation = None 

81 

82 if req_type_name in self._translation: 

83 translation = self._translation[req_type_name] 

84 

85 return translation 

86 

87 

88 def translate(self, req_type_name: str, attr_name: str) -> str: 

89 """ 

90 Translate the requirement attribute. 

91 

92 Args: 

93 req_type_name (str): The name of the requirement type. 

94 attr_name (str): The name of the attribute to translate. 

95 

96 Returns: 

97 str: The translated attribute name. 

98 """ 

99 translation = attr_name 

100 

101 if req_type_name not in self._translation: 

102 log_verbose(f"Failed to translate {req_type_name}: No translation available.") 

103 

104 else: 

105 

106 if attr_name not in self._translation[req_type_name]: 

107 log_verbose(f"Failed to translate {req_type_name}.{attr_name}: No translation available.") 

108 else: 

109 translation = self._translation[req_type_name][attr_name] 

110 

111 return translation 

112 

113# Functions ******************************************************************** 

114 

115 

116# Main *************************************************************************