Source code for ppc_robot_lib.utils.config

import os
from typing import Any


secrets_dir = os.environ.get('PPC_ROBOT_SECRETS_DIR', '/run/secrets')
"""Directory with secrets."""


[docs] def convert_to_bool(value: Any) -> bool: """ Convert value to a boolean. Following strings are considered ``True``: * ``1`` * ``yes`` * ``true`` * ``y`` All comparisons are case in-sensitive. Any other strings are considered ``False``. Other datatypes are converted using `bool(value)`. ``None`` will be converted to ``False``. :param value: String value, boolean, or any value that can be converted to boolean. :return: Boolean value. """ if isinstance(value, str): value_lower = value.lower() return value_lower == '1' or value_lower == 'yes' or value_lower == 'true' or value_lower == 'y' return bool(value)
[docs] def convert_to_int(value: Any) -> int | None: """ Convert a value to integer. :param value: String value, integer, or any value that can be converted to integer. :return: Integer value, or ``None`` for ``None`` and empty strings. """ if value is None or value == '': return None return int(value)
[docs] def env(name: str, default=None) -> str: """ Get a value of environment variable. Multiple lookups are performed, and the value can be loaded from files. If you pass ``<NAME>``, the following lookups might be performed: * ``<NAME>`` - if this env is set, its value is used directly. * ``<NAME>_FILE`` - if this env is set, contents of the file given in the value will be loaded as value. * ``<NAME>_SECRET`` - if this env is set to ``<value>``, contents of the file at ``/run/secrets/<value>`` will be loaded as value. The lookups are performed in the order as listed, and the first value found will be used. If no variable is set, default value will be used. :param name: Name of the environment variable to find. :param default: Default value. :return: Variable value, or the default value. """ if name in os.environ: return os.environ[name] else: file_name = None name_file = f'{name}_FILE' name_secret = f'{name}_SECRET' if name_file in os.environ: file_name = os.environ[name_file] elif name_secret in os.environ: file_name = os.path.join(secrets_dir, os.environ[name_secret]) if file_name is not None: with open(file_name) as f: contents = f.read() return contents.strip() return default
[docs] def has_env(name: str) -> bool: """ Checks if the environment variable is set, or if any of the following variables is set: * ``<NAME>`` * ``<NAME>_FILE`` * ``<NAME>_SECRET`` :param name: Environment variable name. :return: ``True`` if the environment variable is set. """ if name in os.environ: return True name_file = f'{name}_FILE' if name_file in os.environ: return True name_secret = f'{name}_SECRET' if name_secret in os.environ: return True return False
[docs] def socket_address_to_tuple(host_str: str | None, default_host: str, default_port: int) -> tuple[str, int]: """ Convert address with optional port to a (host, port) tuple. :param host_str: Address with `:` delimited port. :param default_host: Default host. :param default_port: Default port. :return: (host, port) tuple. """ if not host_str: return default_host, int(default_port) host_parts = host_str.split(':', 2) if len(host_parts) == 1: return host_str, int(default_port) else: return host_parts[0] if host_parts[0] else default_host, int(host_parts[1])