Config
config
¶
Read configuration (settings) of application.
Configuration is read from the sources in the order described below. Every step overwrites configuration values obtained in previous steps. - 'defaults' directory of the package - current working directory - environment variables with 'PCA_' prefix, i.e., export PCA_FOO=bar
will assign value 'bar' to configuration item 'foo' - configuration files provided in command line arguments
Implementation uses Dynaconf package, read more about Dynaconf: - https://www.dynaconf.com
__DEFAULT_PUBLIC_SETTINGS_FILE = __PACKAGE_PATH / __DEFAULT_SETTINGS_DIR / __PUBLIC_SETTINGS_FILE
module-attribute
¶
__DEFAULT_SECRET_SETTINGS_FILE = __PACKAGE_PATH / __DEFAULT_SETTINGS_DIR / __SECRET_SETTINGS_FILE
module-attribute
¶
__DEFAULT_SETTINGS_DIR = 'defaults'
module-attribute
¶
__ENV_VAR_PREFIX = 'PCA'
module-attribute
¶
__PACKAGE_PATH = importlib.resources.files(__package__)
module-attribute
¶
__PUBLIC_SETTINGS_FILE = 'settings.pca.toml'
module-attribute
¶
__SECRET_SETTINGS_FILE = '.secrets.pca.toml'
module-attribute
¶
__settings: Settings | None = None
module-attribute
¶
load_config_file(config_file: str | PathLike) -> None
¶
Source code in src/python_cli_app_template/config.py
def load_config_file(config_file: str | PathLike) -> None:
config_file_path = Path(config_file).resolve()
if not config_file_path.exists():
logging.getLogger(__name__).warning(f'{config_file_path} does not exist')
return
logging.getLogger(__name__).debug(f'Loading config file: {config_file_path}')
settings().load_file(config_file_path)
settings() -> Settings
¶
Source code in src/python_cli_app_template/config.py
def settings() -> Settings:
global __settings # noqa: PLW0603
if not __settings:
__settings = Dynaconf(
root_path=os.getenv('HOME'),
settings_files=[
__DEFAULT_PUBLIC_SETTINGS_FILE,
__DEFAULT_SECRET_SETTINGS_FILE,
Path(__PUBLIC_SETTINGS_FILE).resolve(),
Path(__SECRET_SETTINGS_FILE).resolve(),
],
envvar_prefix=__ENV_VAR_PREFIX,
)
return __settings
settings_as_str() -> str
¶
Source code in src/python_cli_app_template/config.py
def settings_as_str() -> str:
result: list[str] = []
for k, v in settings().as_dict().items():
result.append(f'{k}: {v}')
return '\n'.join(result)
settings_history() -> str
¶
Source code in src/python_cli_app_template/config.py
def settings_history() -> str:
result: list[str] = []
for item, value in settings().as_dict().items():
result.append(f'{item}: {value}')
for event in _get_history(item):
if event['loader'] == 'toml':
try:
rel_path = Path(event['identifier']).relative_to(Path.cwd())
except ValueError:
rel_path = Path(event['identifier']).resolve()
result.append(f' file({rel_path}): {event["value"]}')
else:
result.append(f' event({event["loader"]}): {event["value"]}')
return '\n'.join(result)