Skip to content

environment

dev_tool.tools.environment

log = logging.getLogger(__name__) module-attribute

EnvironmentVariables

A class for managing environment variables.

This class provides static methods for interacting with system environment variables.

get_environment_variables_from_registry staticmethod

A method that retrieves environment variables from the Windows registry.

Parameters:

  • root (int) –

    The registry root key.

Returns:

  • dict[str, str]

    A dictionary of environment variables.

Source code in dev_tool/tools/environment.py
@staticmethod
def get_environment_variables_from_registry(root: int) -> dict[str, str]:
    """
    A method that retrieves environment variables from the Windows registry.

    :param root: The registry root key.
    :return: A dictionary of environment variables.
    """

    environment = {}

    if root == winreg.HKEY_LOCAL_MACHINE:
        path = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
    else:
        path = r'Environment'

    key = winreg.OpenKey(root, path)
    count, _, _ = winreg.QueryInfoKey(key)

    for i in range(count):
        name, value, _ = winreg.EnumValue(key, i)
        environment[name] = value

    winreg.CloseKey(key)

    return environment

refresh_environment staticmethod

A method that refreshes the environment variables from the Windows registry.

Returns:

  • dict[str, str]

    A dictionary of refreshed environment variables.

Source code in dev_tool/tools/environment.py
@staticmethod
def refresh_environment() -> dict[str, str]:
    """
    A method that refreshes the environment variables from the Windows registry.

    :return: A dictionary of refreshed environment variables.
    """

    system_environment = EnvironmentVariables.get_environment_variables_from_registry(winreg.HKEY_LOCAL_MACHINE)
    user_environment = EnvironmentVariables.get_environment_variables_from_registry(winreg.HKEY_CURRENT_USER)
    environment = {**system_environment, **user_environment}

    if 'PATH' in system_environment and 'PATH' in user_environment:
        environment['PATH'] = (
            system_environment['PATH'] +
            os.pathsep +
            user_environment['PATH']
        )

    for name, value in environment.items():
        os.environ[name] = value

    return environment