genvid.toolbox.Profile
See Managing Multiple Environments for more information on the Profile tool.
- class genvid.toolbox.Profile
Bases:
object
API for manipulating profiles.
If
GENVID_PROFILE
is defined, we search for the matching profile by looking up each path defined byGENVID_PROFILES_PATH
in order.If a profile is defined,
GENVID_PROFILE_PATH
is set to point to the file containing the profile.The profiles are HCL files with the following format:
profile "PROFILE_NAME" { ENV_0 = "ENV_0_VALUE" ENV_1 = "ENV_1_VALUE" [...] ENV_N = "ENV_N_VALUE" }
There can be multiple profiles in a single file or multiple profile files in an arbitrary directory structure: the search is recursive.
Note
Filenames ending in
.backup
are ignored when loading profile files.- Raises:
RuntimeError – If a named profile is defined but cannot be found.
RuntimeError – If we find duplicate profiles within a path.
ValueError – If the profile file contains invalid HCL.
OSError – On any filesystem error that is encountered.
New in version 1.20.0.
- name
The named profile to load. The value is taken from
GENVID_PROFILE
or set to the empty string.
- lookup_paths
The ordered list of paths to the profile files. Initialized from
GENVID_PROFILES_PATH
or set to~/.genvid/profile
.
- path
The path to the profile file containing the loaded profile.
Once resolved,
GENVID_PROFILE_PATH
is set to the same value.Note
If
GENVID_PROFILE
is the empty string, then so is this variable.
- static new(name, path)
Create a new (empty) profile in the specified profile file.
- Parameters:
name (str) – Name to give to the new profile.
path – Path to the profile file to use.
Note
A new profile file will be created if it does not already exists. This include any required parent directories.
- Raises:
OSError – On any filesystem error that is encountered.
- check(*, throw=False)
Check whether a profile is loaded, optionally raising an exception.
- Parameters:
throw (bool) – Whether to raise an exception if no profile is loaded.
- Returns:
True if and only if a profile is loaded.
- Return type:
- Raises:
RuntimeError – If throw is True and no profile is loaded.
- apply()
Apply the loaded profile, if any.
For each environment variable defined within a found profile, we use its value to set the environment if and only if that environment variable is not already set.
- insert(**environment)
Insert key/value pairs into the profile, overriding if necessary.
Note
This method updates the profile file on disk as a side-effect. However, it first creates a timestamped backup file in the same directory.
- Parameters:
environment – The key/value pairs to insert into the profile.
- Raises:
RuntimeError – If no profile is in use.
ValueError – If the profile file contains invalid HCL.
OSError – On any filesystem error that is encountered.
- remove(*variables)
Remove the specified variables from the profile.
Note
This method updates the profile file on disk as a side-effect. However, it first creates a timestamped backup file in the same directory.
- Parameters:
variables – An iterator over variable names that should be removed from the profile.
- Raises:
RuntimeError – If no profile is in use.
RuntimeError – If a variable does not exist in the profile.
ValueError – If the profile file contains invalid HCL.
OSError – On any filesystem error that is encountered.
- class profile.Profile
Implementation of
genvid.toolbox.Profile