Skip to content

fxutils

check_and_create_lock_file

check_and_create_lock_file(lock_file_path: str) -> bool

Check for an existing lock and handle it appropriately.

Parameters:

Name Type Description Default
lock_file_path str

The path to the lock file.

required

Returns:

Name Type Description
bool bool

True if the lock file was created, False otherwise (aka another instance is running).

get_configuration_file_values

get_configuration_file_values(
    file_name: str, sections_options: Dict[str, List[str]]
) -> Dict[str, Dict[str, Optional[str]]]

Reads values from a configuration file for given sections and their options.

Parameters:

Name Type Description Default
file_name str

The name of the configuration file.

required
sections_options Dict[str, List[str]]

A dictionary where keys are section names and values are lists of options in those sections.

required

Returns:

Type Description
Dict[str, Dict[str, Optional[str]]]

Dict[str, Dict[str, Optional[str]]]: A nested dictionary where the first level keys are section names, and their values are dictionaries with options as keys and their values as values. If an option is not found, its value will be None.

Examples:

>>> config = {
...     "settings": ["theme", "language"],
...     "database": ["host", "port"],
... }
>>> get_configuration_file_values("fxquinox.cfg", config)
{
    'settings': {'theme': 'dark', 'language': 'en'},
    'database': {'host': 'localhost', 'port': '3306'}
}

is_process_running

is_process_running(pid: int) -> bool

Check if there's a running process with the given PID.

Parameters:

Name Type Description Default
pid int

The process ID to check.

required

Returns:

Name Type Description
bool bool

True if the process is running, False otherwise.

is_valid_string

is_valid_string(input_string: str) -> bool

Check if a string is valid for use. Match only alphanumeric characters, underscores, or hyphens.

Parameters:

Name Type Description Default
input_string str

The string to check.

required

Returns:

Name Type Description
bool bool

True if the string is valid, False otherwise.

open_directory

open_directory(path: str) -> None

Opens the given file or directory in the system file manager.

remove_lock_file

remove_lock_file(lock_file_path: str) -> None

Remove the lock file.

Parameters:

Name Type Description Default
lock_file_path str

The path to the lock file.

required

transform_to_valid_string

transform_to_valid_string(input_string: str) -> str

Transform a string into a valid format by keeping only alphanumeric characters, underscores, or hyphens.

Parameters:

Name Type Description Default
input_string str

The string to transform.

required

Returns:

Name Type Description
str str

The transformed string with only valid characters.

update_configuration_file

update_configuration_file(
    file_name: str, sections_options_values: dict, temporary: bool = False
) -> None

Updates or creates multiple values in a configuration file based on a nested dictionary.

Parameters:

Name Type Description Default
file_name str

The name of the configuration file.

required
sections_options_values dict

A dictionary where keys are section names and values are dictionaries of options and their values to update or create.

required
temporary bool

If True, the configuration file will be stored in the temporary directory. Defaults to False.

False

Examples:

>>> config = {
...     "settings": {"theme": "dark", "language": "en"},
...     "database": {"host": "localhost", "port": "3306"},
... }
>>> update_configuration_file("fxquinox.cfg", config)