scistag.common.config_stag.ConfigStag

class ConfigStag[source]

Bases: object

Methods

get

Returns a single value from the configuration tree, a list or a dictionary of values from the configuration.

import_environment

Imports all environment variables beginning with environment into the configuration, starting at branch

load_config_file

Loads a configuration file and attaches it to the global configuration tree

map_environment

Maps a single value from an environment variable to a configuration value (if it was set).

set

Changes a single value in the configuration tree.

Attributes

__dict__

__doc__

__module__

__weakref__

list of weak references to the object (if defined)

access_lock

Multithreading access lock

root_branch

The root branch

classmethod _get_branch(cur_branch, source, may_create=True)[source]

Returns the branch for a specific path in the format mainBranch.subBranch.nextBranch.

If may_create is set to true all missing branches in between will be created. If not and the path can not be resolved None will be returned.

Parameters
  • cur_branch (dict) – The current start branch

  • source (str | list[str]) – The path to the dictionary, a list or a string separated by dots, e.g. azure.storage.connectionString

  • may_create – Defines if the branch may be created if it does not exist yet

Return type

dict | None

Returns

The dictionary for the branch

classmethod get(name, default_value=None)[source]

Returns a single value from the configuration tree, a list or a dictionary of values from the configuration.

Note that if you receive an advanced type (such as a list of a dict) you will always receive a copy of the data to prevent accidental modifications via a reference.

Parameters
  • name (str) – The value’s name

  • default_value – The default value to return if the key does not exist

Return type

Union[str, int, float, dict, list, bool]

Returns

The current value

classmethod import_environment(base_branch='', environment='SC_')[source]
Imports all environment variables beginning with environment into the configuration, starting at branch

base_branch.

All underscores in the name will automatically be replaced by dots.

Example: SC_dataBase_connectionString is the environment variables name, our base_branch is “settings” and environment has the value "SC_" then the content will be stored in settings.dataBase.connectionString.

If no parameters are passed automatically all variables beginning with SC_ will be imported into the main branch.

Parameters
  • base_branch (str) – The branch in which the data shall be stored

  • environment (str) – The string with which the environment variables have to begin to be recognized

classmethod load_config_file(source, base_branch='', required=True, environment=None)[source]

Loads a configuration file and attaches it to the global configuration tree

Parameters
  • source (str) – The file source (accessible via FileStag)

  • base_branch (str) – The branch name to which this element shall be attached, separated by dots.

  • required – Defines if an exception shall be raised if this file does not exist

  • environment (str | None) – If defined environment variables beginning with given shortcut will be imported. UnderScores will be replaced by dots. All variables will be imported relative to base_branch

classmethod map_environment(environ_name, config_name)[source]

Maps a single value from an environment variable to a configuration value (if it was set).

If the environment variable does not exist nothing will be changed.

Parameters
  • environ_name – The environment variable name (the source)

  • config_name – The target name, e.g. azure.connection.credentials.connectionString

Return type

bool

Returns

True if the variable was updated

classmethod set(name, value)[source]

Changes a single value in the configuration tree.

Note that if you map an advanced type (such as a list of a dict) you will always receive a copy of the data to prevent accidental modifications via a reference.

Parameters
Return type

None

access_lock = <unlocked _thread.RLock object owner=0 count=0>

Multithreading access lock

root_branch = {}

The root branch