Index: suite_leahi/shared/scripts/configuration/application_init.py =================================================================== diff -u -rf05519ac142a13cbaf6f31c5a4dab88117461d00 -r5242b0f3bde261dff5670b39bdb263f084d237b6 --- suite_leahi/shared/scripts/configuration/application_init.py (.../application_init.py) (revision f05519ac142a13cbaf6f31c5a4dab88117461d00) +++ suite_leahi/shared/scripts/configuration/application_init.py (.../application_init.py) (revision 5242b0f3bde261dff5670b39bdb263f084d237b6) @@ -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" @@ -189,25 +191,26 @@ return aut_version_only def reset_system_setting(setting_key): + """ + Resets a specific setting in System.conf to its default value from System.dflt. """ - Resets a specific setting in System.conf to its default value from System.dflt. - """ - base_path = Path.home() / "Public/luis/config/configurations/Settings/" - source_file = base_path / "System.dflt" - target_file = base_path / "System.conf" - + 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: - new_value = line.split("=")[1].strip() - break - + 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: - print(f"Setting '{setting_key}' not found in {source_file.name}") + test.log(f"Setting '{setting_key}' not found in {source_file.name}") return # Read the current .conf file @@ -226,6 +229,96 @@ 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 @@ -235,11 +328,6 @@ if home_dir == "/home/denali": return "/home/denali/Public/luis/config/configurations/" else: - return "/opt/leahi/config/configurations/" - - - - - - - \ No newline at end of file + return "/opt/leahi/config/configurations/" + +