qgis_deployment_toolbelt.utils.win32utils module

Utilities specific for Windows.

Author: Julien Moura (https://github.com/guts)

Inspired from py-setenv: <https://github.com/beliaev-maksim/py_setenv> (MIT)

class qgis_deployment_toolbelt.utils.win32utils.ExtendedNameFormat(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Enum

Possible values for user name in a Windows Active Directory context. See references:

NameCanonical = 7
NameCanonicalEx = 9
NameDisplay = 3
NameDnsDomain = 12
NameFullyQualifiedDN = 1
NameGivenName = 13
NameSamCompatible = 2
NameServicePrincipal = 10
NameSurname = 14
NameUniqueId = 6
NameUnknown = 0
NameUserPrincipal = 8
qgis_deployment_toolbelt.utils.win32utils.delete_environment_variable(envvar_name: str, scope: str = 'user') bool

Deletes environment variable.

Parameters:
  • envvar_name (str) – environment variable name (= key) to delete

  • scope (str, optional) – environment variable scope. Must be “user” or “system”, defaults to “user”. Defaults to “user”.

Returns:

True is the variable has been successfully deleted

Return type:

bool

qgis_deployment_toolbelt.utils.win32utils.get_current_user_extended_data(extended_name_format: ExtendedNameFormat) str

Get current user full data extended with Active Directory informations.

This method uses the ctypes module since the upper-level method implemented in pywin32.win32api (win32api.GetUserNameEx(extended_name_format.value)) raises an error when the specified format is not reachable.

Inspired from: https://stackoverflow.com/a/70182936/2556577

Returns:

use data in the specified format

Return type:

str

Example

from qgis_deployment_toolbelt.utils.win32utils import (
    ExtendedNameFormat,
    get_current_user_extended_data,
)

user_data = {
    k.name: get_current_user_extended_data(k)
    for k in ExtendedNameFormat
}

print(user_data)
qgis_deployment_toolbelt.utils.win32utils.get_environment_variable(envvar_name: str, scope: str = 'user') str | None

Get environment variable from Windows registry.

Parameters:
  • envvar_name (str) – environment variable name (= key) to retrieve

  • scope (str, optional) – environment variable scope. Must be “user” or “system”, defaults to “user”. Defaults to “user”.

Returns:

environment variable value or None if not found

Return type:

Optional[str]

qgis_deployment_toolbelt.utils.win32utils.normalize_path(input_path: Path, add_trailing_slash_if_dir: bool = True) str

Returns a path as normalized and fully escaped for Windows old-school file style.

Parameters:
  • input_path (Path) – path to normalize

  • add_trailing_slash_if_dir (bool) – add a trailing slash if the input is a folder,defaults to True

Return str:

normalized path as string

Example:

t = Path(r'C:\Users\risor\Documents\GitHub\Geotribu\qtribu\qtribu\resources\images')
print(normalize_path(t))
> C:\\Users\\risor\\Documents\\GitHub\\Geotribu\\qtribu\\qtribu\\resources\\images\\
qgis_deployment_toolbelt.utils.win32utils.refresh_environment() bool

This ensures that changes to Windows registry are immediately propagated. Useful to refresh after have updated the environment variables.

A method by Geoffrey Faivre-Malloy and Ronny Lipshitz. Source: https://gist.github.com/apetrone/5937002

Returns:

True if the environment has been refreshed

Return type:

bool

qgis_deployment_toolbelt.utils.win32utils.set_environment_variable(envvar_name: str, envvar_value: str, scope: str = 'user') bool

Creates/replaces environment variable.

Parameters:
  • envvar_name (str) – name (= key) of environment variable to set or replace.

  • envvar_value (str) – value to set for the environment variable

  • scope (str, optional) – environment variable scope. Must be “user” or “system”, defaults to “user”. Defaults to “user”.

Returns:

True is the variable has been successfully set

Return type:

bool