Skip to content

adjust_settings

Classes¤

ChangeSettings ¤

ChangeSettings(file_name)

Bases: object

Parameters:

Name Type Description Default
file_name str

Name of the file with settings.

required
Source code in common/adjust_settings.py
def __init__(self, file_name: str) -> None:
    """Class for reading and changing settings in ASTRA run and distribution files

    Args:
        file_name (str): Name of the file with settings.
    """
    self.file = file_name
    # TODO: check if exists
    self.file_lock = filelock.FileLock(self.file.parent / (self.file.name + ".lock"))

    self.file_content = self.load_current_settings()

Functions¤

find_setting ¤
find_setting(parameter)

Searches for the position of the parameter in given file.

Parameters:

Name Type Description Default
parameter str

name of parameter to be searched for

required

Returns:

Name Type Description
list list

list pythocontaining start and end index of parameter including its value in the input file

Source code in common/adjust_settings.py
def find_setting(self, parameter: str) -> list:
    """Searches for the position of the parameter in given file.

    Args:
        parameter (str): name of parameter to be searched for

    Returns:
        list : list pythocontaining start and end index of parameter including its value in the input file
    """
    # Setting can be separated by a new line or by comma -> two approaches and the shorter one is chosen

    # If searched for the keyword, brackets are not allowed -> escape character has to be used
    search_parameter = parameter.replace("(", "\(").replace(")", "\)")

    # add space to the beginning for correct re search (and word separation, see the [\W])
    file_content = " "+self.file_content
    # Search for both options
    # [\W] search for non alphanumeric character for correct word separation
    new_line_separated = re.search(
        f"[\W]{search_parameter}\s*=.+?\n", file_content)
    comma_separated = re.search(
        f"[\W]{search_parameter}\s*=.+?,", file_content)

    if new_line_separated is None and comma_separated is None:
        return None

    # If only one is valid (is not None)
    if new_line_separated is None and comma_separated is not None:
        # - 1 to account for the one whitespace added to the begining
        # only at the end, as the the beginning is tackled by the [\W] in re.search
        return [comma_separated.start(), comma_separated.end()-1]
    if new_line_separated is not None and comma_separated is None:
        # - 1 to remove the non aplhanumeric character added by [\W]
        # only at the end, as the the beginning is tackled by the [\W] in re.search
        return [new_line_separated.start(), new_line_separated.end()-1]

    # Both are valid -> the shortes is picked
    if new_line_separated.end() - new_line_separated.start() > comma_separated.end() - comma_separated.start():
        # - 1 to remove the non aplhanumeric character added by [\W]
        # only at the end, as the the beginning is tackled by the [\W] in re.search
        return [comma_separated.start(), comma_separated.end()-1]
    else:
        # - 1 to remove the non aplhanumeric character added by [\W]
        # only at the end, as the the beginning is tackled by the [\W] in re.search
        return [new_line_separated.start(), new_line_separated.end()-1]
load_current_settings ¤
load_current_settings()

Function to load the current settings from given file

Returns:

Name Type Description
str str

Content of the file (settings)-

Source code in common/adjust_settings.py
def load_current_settings(self) -> str:
    """Function to load the current settings from given file

    Returns:
        str: Content of the file (settings)-
    """
    output = ""
    with self.file_lock:
        with open(self.file, "r") as f:
            output = f.read()
    return output
read_setting ¤
read_setting(parameter)

Looks the given parameter and returns the value of this parameter fro the input file.

Parameters:

Name Type Description Default
parameter str

parameter to search for

required

Returns:

Name Type Description
str str

value of given parameter

Source code in common/adjust_settings.py
def read_setting(self, parameter: str) -> str:
    """Looks the given parameter and returns the value of this parameter fro the input file.

    Args:
        parameter (str): parameter to search for

    Returns:
        str: value of given parameter
    """
    # Get position of the setting
    setting_position = self.find_setting(parameter)

    # If setting not found
    if setting_position is None:
        print(self.file_content)
        print("Parameter not found!")
        exit(1)

    # Get the parameter + value without the last character (comma / new line)
    output = self.file_content[setting_position[0]: setting_position[1]-1]
    # Return output after = sign
    return output[re.search("=", output).end():].strip()
replace_setting ¤
replace_setting(parameter, new_value)

Replaces the value of given parameter by new_value

Parameters:

Name Type Description Default
parameter str

parameter to be adjusted

required
new_value str

new value of the parameter

required
Source code in common/adjust_settings.py
def replace_setting(self, parameter: str, new_value: str) -> None:
    """Replaces the value of given parameter by new_value

    Args:
        parameter (str): parameter to be adjusted
        new_value (str): new value of the parameter
    """
    # Get position of the setting
    setting_position = self.find_setting(parameter)

    # If setting not found
    if setting_position is None:
        print(self.file_content)
        print("Parameter not found!")
        exit(1)

    # If found, replace it by the new setting
    self.file_content = self.file_content.replace(
        self.file_content[setting_position[0]: setting_position[1]-1], f"{parameter}={new_value}")
save_updated_settings ¤
save_updated_settings()

Saves updated settings.

Source code in common/adjust_settings.py
def save_updated_settings(self) -> None:
    """Saves updated settings.
    """
    with self.file_lock:
        with open(self.file, 'w') as f:
            f.write(self.file_content)
update ¤
update(parameter, new_value)

Sets parameter to n_value

Parameters:

Name Type Description Default
parameter str

parameter to be changed

required
new_value str

new value of the parameter

required
Source code in common/adjust_settings.py
def update(self, parameter, new_value):
    """Sets parameter to n_value

    Args:
        parameter (str): parameter to be changed
        new_value (str): new value of the parameter
    """
    self.replace_setting(parameter, new_value)
    self.save_updated_settings()

Last update: October 31, 2023
Created: October 31, 2023