Index: suite_leahi/shared/scripts/configuration/application_init.py =================================================================== diff -u -r4f71dc71ca95ab916947fafdc17c9e1e4f74c47e -r86e92e4903095be7e338be9e914e9a2384451f9c --- suite_leahi/shared/scripts/configuration/application_init.py (.../application_init.py) (revision 4f71dc71ca95ab916947fafdc17c9e1e4f74c47e) +++ suite_leahi/shared/scripts/configuration/application_init.py (.../application_init.py) (revision 86e92e4903095be7e338be9e914e9a2384451f9c) @@ -7,6 +7,8 @@ from leahi_dialin.ui import utils from configuration import config from configuration import utility +from typing import Dict +from typing import Optional # WARNING: these variables has to match with the ones in the run.sh and in UI code POSTMSG_POSTFIX_PASSED = " passed" @@ -187,3 +189,145 @@ aut_version = aut_name_and_version.split(" ") aut_version_only = aut_version[1] return aut_version_only + +def reset_system_setting(setting_key): + """ + Resets a specific setting in System.conf to its default value from System.dflt. + """ + source_file = configuration_folder_path() + "Settings/System.dflt" + target_file = configuration_folder_path() + "Settings/System.conf" + new_value = None + search_pattern = f"{setting_key} =" + + # Read the default value from .dflt + with open(source_file, 'r') as f: + for line in f: + if search_pattern in line: + parts = line.split("=") + if len(parts) >= 2: + new_value = parts[1].strip() + # If split fails (len < 2), new_value remains None and loop continues/breaks + break + + if new_value is None: + test.log(f"Setting '{setting_key}' not found in {source_file.name}") + return + + # Read the current .conf file + with open(target_file, 'r') as f: + conf_lines = f.readlines() + + # Update the value in memory + updated_lines = [] + for line in conf_lines: + if line.strip().startswith(setting_key): + updated_lines.append(f"{setting_key} = {new_value}\n") + else: + updated_lines.append(line) + + # Write back to .conf + with open(target_file, 'w') as f: + f.writelines(updated_lines) + +def get_section_text(section_name: str = '[Installation^Pressure Lines and Dialyzer]'): + """ + Reads the configuration file and extracts key-value pairs + from the specified section. + """ + location = Path.home() / config.CONFIGURATIONS_PATH / "Instructions/Instructions.conf" + result = {} + + # Check if file exists to prevent FileNotFoundError + if not os.path.exists(location): + return result + + with open(location, 'r', encoding='utf-8') as f: + in_target_section = False + + for line in f: + # Remove leading/trailing whitespace + stripped_line = line.strip() + + # Skip empty lines + if not stripped_line: + continue + + # Check for Section Header, for example: [Section Name] + if stripped_line.startswith('[') and stripped_line.endswith(']'): + if stripped_line == section_name: + in_target_section = True + else: + # If already inside target section and a new section starts, stop reading + if in_target_section: + break + in_target_section = False + + continue + + # Parse key-value pairs inside the target section + if in_target_section and '=' in stripped_line: + # Split only on the first '=' to preserve '=' inside the value + key, value = stripped_line.split('=', 1) + result[key.strip()] = value.strip() + + return result + +def get_instructions_title(section_name: str = '[Installation^Pressure Lines and Dialyzer]'): + """ + Reads the configuration file and extracts the 'Title' value + from the specified section. + + Args: + section_name: The section header to search for in the config file. + + Returns: + The Title value if found, otherwise None. + """ + location = Path.home() / config.CONFIGURATIONS_PATH / "Instructions/Instructions.conf" + # Check if file exists before opening + if not os.path.exists(location): + return None + + with open(location, 'r', encoding='utf-8') as f: + in_target_section = False + + for line in f: + # Remove leading/trailing whitespace + stripped_line = line.strip() + + # Skip empty lines + if not stripped_line: + continue + + # Check for section header + if stripped_line.startswith('[') and stripped_line.endswith(']'): + if stripped_line == section_name: + in_target_section = True + else: + # If already in target section and a new section starts, stop reading + if in_target_section: + break + in_target_section = False + + continue + + # If inside the target section, look for Title + if in_target_section and stripped_line.startswith('Title'): + if '=' in stripped_line: + _, value = stripped_line.split('=', 1) + return value.strip() + + return None + +def configuration_folder_path(): + """ + Check for Path.home() is /home/denali then return configuration folder location + else return Device configuration folder location + """ + home_dir = str(Path.home()) + if home_dir == "/home/denali": + return "/home/denali/Public/luis/config/configurations/" + else: + return "/opt/leahi/config/configurations/" + + \ No newline at end of file