4.7.5.5. Setup environment variables¶
Tools for getting OS env vars.
TODO: This whole module needs to be refactored.
It was originally intended as a quick hack to preload environment variables before importing the whole package in order to overcome limitations in older versions of python.
get_env_connect should be converted to use click options, but due to time constraints CONNECT_SCHEMAS is used instead.
We will need to refactor cli/__init__.py so that the Connect options are defined in a separate module that can be imported by both cli/__init__.py and setup_env.py
- axonius_api_client.setup_env.LOGGER = <Logger axonius_api_client.setup_env (DEBUG)>¶
Logger to use
- axonius_api_client.setup_env.YES: List[str] = ['1', 'true', 't', 'yes', 'y', 'on']¶
Values that should be considered as true
- axonius_api_client.setup_env.NO: List[str] = ['0', 'false', 'f', 'no', 'n', 'off']¶
Values that should be considered as false
- axonius_api_client.setup_env.KEY_DEFAULT_PATH: str = 'AX_PATH'¶
OS env to use for
DEFAULT_PATH
instead of CWD
- axonius_api_client.setup_env.KEY_OVERRIDE: str = 'AX_ENV_OVERRIDE'¶
OS env to control ignoring OS env when loading .env file
- axonius_api_client.setup_env.KEY_FEATURES: str = 'AX_FEATURES'¶
OS env to get API features to enable from
- axonius_api_client.setup_env.KEY_CERTWARN: str = 'AX_CERTWARN'¶
OS env to get cert warning bool from
- axonius_api_client.setup_env.KEY_CERTPATH: str = 'AX_CERTPATH'¶
OS env to get cert warning bool from
- axonius_api_client.setup_env.KEY_DEBUG_PRINT: str = 'AX_DEBUG_PRINT'¶
OS env to use print() instead of LOGGER.debug()
- axonius_api_client.setup_env.KEY_USER_AGENT: str = 'AX_USER_AGENT'¶
OS env to use a custom User Agent string.
- axonius_api_client.setup_env.DEFAULT_DEBUG_PRINT: str = 'no'¶
Default for
KEY_DEBUG_PRINT
- axonius_api_client.setup_env.DEFAULT_OVERRIDE: str = 'yes'¶
Default for
KEY_OVERRIDE
- axonius_api_client.setup_env.DEFAULT_CERTWARN: str = 'yes'¶
Default for
KEY_CERTWARN
- axonius_api_client.setup_env.DEFAULT_ENV_FILE: str = '.env'¶
Default for
KEY_ENV_FILE
- axonius_api_client.setup_env.KEYS_HIDDEN: List[str] = ['AX_KEY', 'AX_SECRET', 'CF_TOKEN']¶
t.List of keys to hide in
get_env_ax()
- axonius_api_client.setup_env.KEY_MATCHES: List[str] = ['password', 'secret', 'token', 'key']¶
t.List of key partial matches to hide in
get_env_ax()
- axonius_api_client.setup_env.HIDDEN: str = '_HIDDEN_'¶
Value to use for hidden keys in
get_env_ax()
- axonius_api_client.setup_env.is_empty_object(value)[source]¶
Check if value is an empty object.
- Parameters:
value (
typing.Any
) – value to check- Returns:
True if value is an empty object
- Return type:
- axonius_api_client.setup_env.is_empty_string(value)[source]¶
Check if value is an empty string.
- Parameters:
value (
typing.Any
) – value to check- Returns:
True if value is an empty string
- Return type:
- axonius_api_client.setup_env.is_empty(value)[source]¶
Check if value is empty.
- Parameters:
value (
typing.Any
) – value to check- Returns:
True if value is empty
- Return type:
- class axonius_api_client.setup_env.Results(value)[source]¶
Bases:
Enum
Enum for find_dotenv results.
-
find_dotenv_cwd:
str
= "Walk to root from the current working directory to find a dotenv file named '.env' (override with $AX_ENV_FILE)"¶
-
find_dotenv_cwd:
- axonius_api_client.setup_env.spew(msg, debug=None, debug_print=None)[source]¶
Print a message to stdout.
- Parameters:
msg (
str
) –debug (
typing.Optional
[bool
]) –debug_print (
typing.Optional
[bool
]) –
- Return type:
- axonius_api_client.setup_env.get_file_or_dir_with_file(path, filename)[source]¶
Check if path is a file or dir with a file.
- Parameters:
path (
typing.Union
[str
,bytes
,pathlib.Path
]) – path to checkfilename (
typing.Union
[str
,bytes
,pathlib.Path
]) – filename to check for
- Returns:
path to file if found, else None
- Return type:
- axonius_api_client.setup_env.find_dotenv(ax_env=None, filename='.env', default='/home/docs/checkouts/readthedocs.org/user_builds/axonius-api-client/checkouts/latest/docs', check_ax_env=True, check_default=True, check_walk_cwd=True, check_walk_script=True, debug=True)[source]¶
Find a .env file.
- Parameters:
ax_env (
typing.Union
[str
,bytes
,pathlib.Path
,None
]) – manual path to look for .env filefilename (
typing.Optional
[str
]) – name of the .env file to look for (not a path, just a filename), override with $AX_ENV_FILEdefault (
typing.Union
[str
,bytes
,pathlib.Path
,None
]) – default path to use if ax_env or $AX_PATH is not supplied (default is CWD)check_ax_env (
bool
) – check if $value is file or $value/$filename is file from $AX_ENVcheck_default (
bool
) – check if $value is file or $value/$filename is file $AX_PATH with default as default valuecheck_walk_cwd (
bool
) – walk to root to find filename from current working directorycheck_walk_script (
bool
) – walk to root to find filename from running scripts directory (does not work in interactive mode or sys.frozen=True)debug (
bool
) – enable debug output
- Return type:
Notes
- Order of operations:
Check for ax_env for .env (or dir with .env in it)
Check for OS env var
KEY_ENV_PATH
for .env (or dir with .env in it)Check for OS env var
KEY_DEFAULT_PATH
as dir with .env in ituse dotenv.find_dotenv() to walk tree from CWD
use dotenv.find_dotenv() to walk tree from package root
- class axonius_api_client.setup_env.MSG[source]¶
Bases:
object
Messages for
load_dotenv()
andfind_dotenv()
.- not_found = 'Could not find'¶
- already_loaded = 'Override is False, not loading already loaded'¶
- loading = 'Loading .env with override'¶
- axonius_api_client.setup_env.load_dotenv(ax_env=None, override=None, debug=True, verbose=None, **kwargs)[source]¶
Load a ‘.env’ file as environment variables accessible to this package.
- Parameters:
ax_env (
typing.Union
[str
,bytes
,pathlib.Path
,None
]) – path to .env file to load, if directory will look for ‘.env’ in that directoryoverride (
typing.Optional
[bool
]) – override existing env vars with those in .env filedebug (
typing.Optional
[bool
]) – enable debug outputverbose (
typing.Optional
[bool
]) – enable verbose output in dotenv.load_dotenvkwargs – additional keyword arguments to pass to
find_dotenv()
- Return type:
- axonius_api_client.setup_env.get_env_ax_env()[source]¶
Get the value of the OS env var
KEY_ENV_PATH
.- Return type:
- axonius_api_client.setup_env.get_env_str(key, default=None, empty_ok=False, lower=False, strip=True, description=None)[source]¶
Get an OS env var.
- Parameters:
key (
str
) – OS env keydefault (
typing.Optional
[typing.Any
]) – default to use if not foundempty_ok (
bool
) – do not throw an exc if the key’s value is emptylower (
bool
) – lowercase the valuestrip (
bool
) – strip the valuedescription (
typing.Optional
[str
]) – description of the env var
- Raises:
ValueError – OS env var value is empty and empty_ok is False
- Return type:
- axonius_api_client.setup_env.get_env_bool(key, default=None, description=None)[source]¶
Get an OS env var and turn convert it to a boolean.
- Parameters:
key (
str
) – OS env keydefault (
typing.Optional
[typing.Any
]) – default to use if not founddescription (
typing.Optional
[str
]) – description of env var for error message
- Raises:
ValueError – OS env var value is not bool
- Return type:
- axonius_api_client.setup_env.get_env_extra_warn(ax_env=None, **kwargs)[source]¶
Get AX_CERT_VERIFY from OS env vars.
- Parameters:
ax_env (
typing.Union
[str
,bytes
,pathlib.Path
,None
]) – path to .env file to load, if not supplied will find a ‘.env’kwargs – passed to
load_dotenv()
- Return type:
- axonius_api_client.setup_env.get_env_path(key, default=None, get_dir=True)[source]¶
Get a path from an OS env var.
- Parameters:
key (
str
) – OS env var to get path fromdefault (
typing.Optional
[str
]) – default path to use if OS env var not setget_dir (
bool
) – if path is file, return directory containing file
- Return type:
- axonius_api_client.setup_env.get_env_csv(key, default=None, empty_ok=False, lower=False)[source]¶
Get an OS env var as a CSV.
- Parameters:
key (
str
) – OS env keydefault (
typing.Optional
[str
]) – default to use if not foundempty_ok (
bool
) – do not throw an exc if the key’s value is emptylower (
bool
) – lowercase the value
- Return type:
- axonius_api_client.setup_env.get_env_user_agent(ax_env=None, **kwargs)[source]¶
Get AX_USER_AGENT from OS env vars.
- Parameters:
ax_env (
typing.Union
[str
,bytes
,pathlib.Path
,None
]) – path to .env file to load, if not supplied will find a ‘.env’**kwargs (
typing.Any
) – passed toload_dotenv()
- Return type:
- axonius_api_client.setup_env.load_schema(schema, kwargs=None)[source]¶
Load a schema from an OS env var.
- Parameters:
schema (
dict
) –kwargs (
typing.Optional
[dict
]) –
- Return type:
- axonius_api_client.setup_env.get_env_connect(ax_env=None, load_override=None, load_filename='.env', load_default='/home/docs/checkouts/readthedocs.org/user_builds/axonius-api-client/checkouts/latest/docs', load_check_ax_env=True, load_check_default=True, load_check_walk_cwd=True, load_check_walk_script=True, debug=True, **kwargs)[source]¶
Get connect arguments that start with AX_ or CF_ from OS env vars.
Notes
Arguments are defined in
CONNECT_SCHEMAS
.- Parameters:
ax_env (
typing.Union
[str
,bytes
,pathlib.Path
,None
]) – path to .env file to load, if not supplied will find a ‘.env’debug (
typing.Optional
[bool
]) – if True, will print debug messagesload_override (
typing.Optional
[bool
]) – if True, will override any existing OS env vars with valuesload_filename (
typing.Optional
[str
]) – filename to load fromload_default (
typing.Union
[str
,bytes
,pathlib.Path
,None
]) – default path to use if OS env var not setload_check_ax_env (
bool
) – if True, will check for AX_ENV_PATH in OS env varsload_check_default (
bool
) – if True, will check for DEFAULT_ENV_FILE in OS env varsload_check_walk_cwd (
bool
) – if True, will walk up from cwd looking for DEFAULT_ENV_FILEload_check_walk_script (
bool
) – if True, will walk up from script looking for DEFAULT_ENV_FILE**kwargs – checked for argument defaults to use instead of schema defaults
- Return type:
- axonius_api_client.setup_env.get_env_features(ax_env=None, **kwargs)[source]¶
Get list of features to enable from OS env vars.
- Parameters:
ax_env (
typing.Union
[str
,bytes
,pathlib.Path
,None
]) – path to .env file to load, if not supplied will find a ‘.env’kwargs – passed to
load_dotenv()
- Return type:
- axonius_api_client.setup_env.hide_value(key, value)[source]¶
Hide sensitive values.
- Parameters:
key (
str
) –value (
typing.Any
) –
- Return type:
- axonius_api_client.setup_env.set_env(key, value, ax_env=None, quote_mode='always', export=False, encoding='utf-8')[source]¶
Set an environment variable in .env file.
- Parameters:
key (
str
) –value (
typing.Any
) –ax_env (
typing.Union
[str
,bytes
,pathlib.Path
,None
]) –quote_mode (
str
) –export (
bool
) –encoding (
str
) –
- Return type:
- axonius_api_client.setup_env.bytes_to_str(value, encoding='utf-8', errors='ignore')[source]¶
Convert bytes to str.
- Parameters:
value (
typing.Any
) –encoding (
str
) –errors (
str
) –
- Return type: