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.CF_PRE: str = 'CF_'

Prefix for axonapi related OS env vars

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_ENV_FILE: str = 'AX_ENV_FILE'

OS env to use for .env file name

axonius_api_client.setup_env.KEY_ENV_PATH: str = 'AX_ENV'

OS env to use for path to ‘.env’ file

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_URL: str = 'AX_URL'

OS env to get API URL from

axonius_api_client.setup_env.KEY_KEY: str = 'AX_KEY'

OS env to get API key from

axonius_api_client.setup_env.KEY_SECRET: str = 'AX_SECRET'

OS env to get API secret from

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: str = 'AX_DEBUG'

OS env to enable debug logging

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: str = 'no'

Default for KEY_DEBUG

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:

bool

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:

bool

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:

bool

class axonius_api_client.setup_env.Results(value)[source]

Bases: Enum

Enum for find_dotenv results.

supplied: str = 'user supplied .env file as find_dotenv(ax_env=...)'
env_path: str = 'OS environment variable $AX_ENV'
default_path: str = 'OS environment variable $AX_PATH or current working directory'
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_script: str = "Walk to root from the directory of the currently running script to find a dotenv file named '.env' (override with $AX_ENV_FILE) (does not work in interactive mode or `sys.frozen=True`)"
not_found: str = "No dotenv file named '.env' (override with $AX_ENV_FILE) found"
axonius_api_client.setup_env.spew(msg, debug=None, debug_print=None)[source]

Print a message to stdout.

Parameters:
Return type:

None

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:
Returns:

path to file if found, else None

Return type:

pathlib.Path

axonius_api_client.setup_env.find_dotenv(ax_env=None, filename='.env', default='/home/docs/checkouts/readthedocs.org/user_builds/axonius-api-client/checkouts/stable/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 file

  • filename (typing.Optional[str]) – name of the .env file to look for (not a path, just a filename), override with $AX_ENV_FILE

  • default (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_ENV

  • check_default (bool) – check if $value is file or $value/$filename is file $AX_PATH with default as default value

  • check_walk_cwd (bool) – walk to root to find filename from current working directory

  • check_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:

typing.Tuple[str, str]

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 it

  • use 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() and find_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:
Return type:

str

axonius_api_client.setup_env.get_env_ax_env()[source]

Get the value of the OS env var KEY_ENV_PATH.

Return type:

str

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 key

  • default (typing.Optional[typing.Any]) – default to use if not found

  • empty_ok (bool) – do not throw an exc if the key’s value is empty

  • lower (bool) – lowercase the value

  • strip (bool) – strip the value

  • description (typing.Optional[str]) – description of the env var

Raises:

ValueError – OS env var value is empty and empty_ok is False

Return type:

str

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:
Raises:

ValueError – OS env var value is not bool

Return type:

bool

axonius_api_client.setup_env.get_env_extra_warn(ax_env=None, **kwargs)[source]

Get AX_CERT_VERIFY from OS env vars.

Parameters:
Return type:

bool

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 from

  • default (typing.Optional[str]) – default path to use if OS env var not set

  • get_dir (bool) – if path is file, return directory containing file

Return type:

typing.Union[pathlib.Path, str]

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 key

  • default (typing.Optional[str]) – default to use if not found

  • empty_ok (bool) – do not throw an exc if the key’s value is empty

  • lower (bool) – lowercase the value

Return type:

typing.List[str]

axonius_api_client.setup_env.get_env_user_agent(ax_env=None, **kwargs)[source]

Get AX_USER_AGENT from OS env vars.

Parameters:
Return type:

str

axonius_api_client.setup_env.load_schema(schema, kwargs=None)[source]

Load a schema from an OS env var.

Parameters:
Return type:

typing.Any

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/stable/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 messages

  • load_override (typing.Optional[bool]) – if True, will override any existing OS env vars with values

  • load_filename (typing.Optional[str]) – filename to load from

  • load_default (typing.Union[str, bytes, pathlib.Path, None]) – default path to use if OS env var not set

  • load_check_ax_env (bool) – if True, will check for AX_ENV_PATH in OS env vars

  • load_check_default (bool) – if True, will check for DEFAULT_ENV_FILE in OS env vars

  • load_check_walk_cwd (bool) – if True, will walk up from cwd looking for DEFAULT_ENV_FILE

  • load_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:

dict

axonius_api_client.setup_env.get_env_features(ax_env=None, **kwargs)[source]

Get list of features to enable from OS env vars.

Parameters:
Return type:

typing.List[str]

axonius_api_client.setup_env.hide_value(key, value)[source]

Hide sensitive values.

Parameters:
Return type:

typing.Any

axonius_api_client.setup_env.hide_values(data)[source]

Hide sensitive values.

Parameters:

data (dict) –

Return type:

dict

axonius_api_client.setup_env.get_env_ax(hide=True)[source]

Get all axonapi related OS env vars.

Parameters:

hide (bool) –

Return type:

dict

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:
Return type:

typing.Tuple[typing.Optional[bool], str, str]

axonius_api_client.setup_env.bytes_to_str(value, encoding='utf-8', errors='ignore')[source]

Convert bytes to str.

Parameters:
Return type:

typing.Any

axonius_api_client.setup_env.DEBUG_PRINT: bool = False

Use print() instead of LOGGER.debug().

axonius_api_client.setup_env.DEBUG: bool = False

Enable package wide debugging.

axonius_api_client.setup_env.DEFAULT_PATH: str = '/home/docs/checkouts/readthedocs.org/user_builds/axonius-api-client/checkouts/stable/docs'

Default path to use throughout this package