def test_list_entries_from_yaml_should_not_duplicate_when_merged(tmpdir): data = { "default": { "SOME_KEY": "value", "SOME_LIST": ["item_1", "item_2", "item_3"], }, "other": { "SOME_KEY": "new_value", "SOME_LIST": ["item_4", "item_5"] }, } yaml_loader.write(str(tmpdir.join("test_settings.yaml")), data) settings = Dynaconf( settings_files="test_settings.yaml", environments=True, merge_enabled=True, ) expected_default_value = BoxList(["item_1", "item_2", "item_3"]) expected_other_value = BoxList( ["item_1", "item_2", "item_3", "item_4", "item_5"]) assert settings.from_env("default").SOME_LIST == expected_default_value assert settings.from_env("other").SOME_LIST == expected_other_value
class Config(object): _settings = Dynaconf( load_dotenv=True, # 是否读取.env dotenv_path='/app/.env' if os.path.exists('/app/.env') else '/app/.env.example', envvar_prefix=False, # 变量变成是否需前缀 dotenv_override=False, # 设置.env配置是否覆盖环境变量 ) def __call__(self, key, default=None, cast=None, mandatory=True, fresh=False, dotted_lookup=True, parent=None): value = self._settings.get(key, default=default, cast=cast, fresh=fresh, dotted_lookup=dotted_lookup, parent=None) if mandatory and value is None: raise Exception(f'config key: {key} is missing') return value
def __init__(self): if not TEST_INSTANCE: settings = Dynaconf( settings_files=[tkb_default_file, tkb_file], envvar_prefix="TKB", validators=[ Validator("data_path", must_exist=True), Validator("rebuild_features", condition=is_bool, default=False), Validator("enable_tensorflow", condition=is_bool, default=True), ], ) try: settings.validators.validate() except validator.ValidationError as e: print("Error in configuration:", e) exit(1) self.DATA_PATH = settings.data_path self.REBUILD_FEATURES = settings.REBUILD_FEATURES self.ENABLE_TENSORFLOW = settings.enable_tensorflow else: self.DATA_PATH = DATA_PATH self.REBUILD_FEATURES = REBUILD_FEATURES self.ENABLE_TENSORFLOW = ENABLE_TENSORFLOW
def __new__(cls, **dynaconf_kwargs): cls.global_settings: Dynaconf = Dynaconf(**dynaconf_kwargs) cls.factory_settings: DynaBox = cls.global_settings.get( f'{cls.GLOBAL_PREFIX}', {}).get(f'{cls.FACTORY_PREFIX}') if cls.factory_settings is None: path = f'{cls.GLOBAL_PREFIX}.{cls.FACTORY_PREFIX}' raise DYInvalidConfigurationException(path) if not cls.factory_settings.get(cls.TYPE_POSTFIX): path = f'{cls.GLOBAL_PREFIX}.{cls.FACTORY_PREFIX}.{cls.TYPE_POSTFIX}' raise DYInvalidConfigurationException(path) requested_classname = str(cls.factory_settings.get( cls.TYPE_POSTFIX)).upper() subclasses = { klass.__name__.upper(): klass for klass in cls.__subclasses__() } requested_class = subclasses.get(requested_classname) if not requested_class: raise DYNotFoundException( dependency_name=requested_classname, allowed_values=', '.join(subclasses.keys()), ) return super().__new__(requested_class)
def main(): ## Default list of settings files for Dynaconf to parse. settings_files = ["config.yaml", "config.yml"] parser = argparse.ArgumentParser() parser.add_argument("--config", "-c", help="path to a configuration file") args = parser.parse_args() if args.config: if not args.config.endswith("yaml") and not args.config.endswith( "yml"): sys.exit("Please provide a `yaml` or `yml` configuration file.") ## Allow users to provide a custom config file that takes precedence. settings_files = [args.config] config = Dynaconf( settings_files=settings_files, load_dotenv=True, envvar_prefix="THREATBUS", ) try: validate_threatbus_config(config) except Exception as e: sys.exit(f"Invalid config: {e}") start(config)
def __init_config() -> Dynaconf: config = Dynaconf( settings_files=[get_config_file(), get_secrets_file()], # secrets=[], merge=True, environments=False, ) __merge_secrets(config) return config
def main(): global matcher_name ## Default list of settings files for Dynaconf to parse. settings_files = ["config.yaml", "config.yml"] parser = argparse.ArgumentParser() parser.add_argument("--config", "-c", help="path to a configuration file") args = parser.parse_args() if args.config: if not args.config.endswith("yaml") and not args.config.endswith( "yml"): sys.exit("Please provide a `yaml` or `yml` configuration file.") ## Allow users to provide a custom config file that takes precedence. settings_files = [args.config] config = Dynaconf( settings_files=settings_files, load_dotenv=True, envvar_prefix="VAST_THREATBUS", ) try: validate_config(config) except Exception as e: sys.exit(ValueError(f"Invalid config: {e}")) setup_logging_with_config(config.logging) # TODO: Pass matcher name as an argument instead of global var if config.live_match: matcher_name = config.matcher_name while True: try: asyncio.run( start( config.vast_binary, config.vast, config.threatbus, config.snapshot, config.live_match, config.retro_match, config.retro_match_max_events, config.retro_match_timeout, config.max_background_tasks, config.metrics.interval, config.metrics.filename, config.transform_context, config.sink, )) except (KeyboardInterrupt, SystemExit): return except asyncio.CancelledError: if user_exit: # Tasks were cancelled because the user stopped the app. return logger.info("Restarting vast-threatbus ...")
def resetPsw(self, password): try: import toml data = toml.load(open('settings.toml')) data['authentication']['password'] = password toml.dump(data, open('settings.toml', 'w')) global settings settings = Dynaconf(settings_files=['settings.toml']) return True except: return False
def load_configuration(env: Optional[APP_ENV] = None) -> Configuration: if not env: env = cast(APP_ENV, os.getenv("ENV_FOR_DYNACONF", "development")) settings = Dynaconf( settings_files=["default.yaml", f"{env}.yaml", ".secrets.yaml"], load_dotenv=True, merge_enabled=True, root_path=str(PROJECT_ROOT / "config"), envvar_prefix="FASTAPI_BACKEND", ) config_dict = {"environment": env} for key in Configuration.__fields__.keys(): if settings.get(key) is not None: config_dict[key] = settings[key] config = Configuration(**config_dict) return config
def non_dev_check(_config: Dynaconf): requireds = ( 'SMTP.SERVER', 'SMTP.NOREPLY', 'SMTP.NOREPLY_PASSWORD', ) for req in requireds: if _config.get(req) is None: raise ValidationError( f'{req} are required in env {_config["ENV"]}') if _config['JWT']['SECRET'] == DEFAULT_SECRET: raise ValidationError(f'Use default secret in env {_config["ENV"]}') _config['DEBUG'] = False _config['TESTING'] = False
def __merge_secrets(config: Union[Dynaconf, DynaBox], src_config: Dynaconf = None): if src_config is None: src_config = config for key, val in config.items(): if isinstance(val, dict): __merge_secrets(val, src_config) continue # NOTE value must be an exact match to avoid interfering # with other templates if isinstance(val, str) and (val[0:2] == "${" and val[-1] == "}"): ref_key = val[2:-1] ref_val = src_config.get(ref_key, None) if ref_val is not None: config[key] = ref_val continue
def test_default_eq_env_lvl_1(): """Tests when the env value equals the default value.""" VAR_NAME = "test" ENV = "DYNATESTRUN_TEST" settings = Dynaconf( environments=False, envvar_prefix="DYNATESTRUN", validators=[ Validator( VAR_NAME, default=True, is_type_of=bool, ), ], ) os.environ[ENV] = "true" assert settings.test is True del os.environ[ENV]
def configuration(): settings = Dynaconf( envvar_prefix="TCHESS", settings_files=['settings.toml'], load_dotenv=True, validators = [ Validator('LICHESS.API_KEY', must_exist=True) | Validator('LICHESS.API_KEY_CMD', must_exist=True), ] ) settings.validators.validate() if settings.lichess.get("api_key_cmd") and not settings.lichess.get("api_key"): settings.lichess.api_key = pass_cmd(settings.lichess.api_key_cmd) return settings
def main(): ## Default list of settings files for Dynaconf to parse. settings_files = ["config.yaml", "config.yml"] parser = argparse.ArgumentParser() parser.add_argument("--config", "-c", help="path to a configuration file") args = parser.parse_args() if args.config: if not args.config.endswith("yaml") and not args.config.endswith( "yml"): sys.exit("Please provide a `yaml` or `yml` configuration file.") ## Allow users to provide a custom config file that takes precedence. settings_files = [args.config] config = Dynaconf( settings_files=settings_files, load_dotenv=True, envvar_prefix="SURICATA_THREATBUS", ) try: validate_config(config) except Exception as e: sys.exit(ValueError(f"Invalid config: {e}")) setup_logging_with_config(config.logging) while True: try: asyncio.run( start( config.threatbus, config.snapshot, config.socket, config.rules_file, config.reload_interval, )) except (KeyboardInterrupt, SystemExit): return except asyncio.CancelledError: if user_exit: # Tasks were cancelled because the user stopped the app. return logger.info("Restarting Suricata app ...")
def load_config_at_path(path: Pathy) -> Dynaconf: """Load config at exact path Args: path: path to config file Returns: dict: config dict """ path = pathlib.Path(path) if path.exists() and path.is_file(): options = DYNACONF_OPTIONS.copy() options.update({ 'root_path': str(path.parent), 'settings_file': str(path.name), }) return Dynaconf(**options) else: raise ConfigurationError( f'Couldn\'t find ballet.yml config file at {path!s}')
def _get_settings() -> Dynaconf: """load default settings instance""" with importlib_resources_path("paquo", ".paquo.defaults.toml") as default_config: settings = Dynaconf( envvar_prefix="PAQUO", settings_file=[PAQUO_CONFIG_FILENAME], root_path=Path.cwd(), core_loaders=['TOML'], preload=[str(default_config.absolute())], validators=[ Validator("java_opts", is_type_of=(list, tuple, str)), Validator("qupath_search_dirs", is_type_of=(list, tuple, str)), Validator("qupath_search_conda", is_type_of=(bool, int), is_in=(0, 1)), Validator("qupath_prefer_conda", is_type_of=(bool, int), is_in=(0, 1)), Validator("safe_truncate", is_type_of=(bool, int), is_in=(0, 1)), Validator("mock_backend", is_type_of=(bool, int), is_in=(0, 1)), Validator("cli_force_log_level_error", is_type_of=(bool, int), is_in=(0, 1)), ] ) return settings
def test_default_lvl_2(): """Tests if the default works propperly with one nested level. Uses different values for the default and the environment variable. """ VAR_NAME = "nested.test" ENV = "DYNATESTRUN_NESTED__TEST" settings = Dynaconf( environments=False, envvar_prefix="DYNATESTRUN", validators=[ Validator( VAR_NAME, default=True, is_type_of=bool, ), ], ) os.environ[ENV] = "false" assert settings.nested.test is False del os.environ[ENV]
def build_settings(validators: Optional[list[Validator]] = None): """ Creates a Dynaconf base object. Configuration files are checked in this order: 1. User configuration directory. On Linux that's `$XDG_CONFIG_HOME/mobilizon_reshare/<mobilizon-reshare-version>`; 2. System configuration directory. On Linux that's the first element of `$XDG_CONFIG_DIRS` + `/mobilizon_reshare/<mobilizon-reshare-version>`. 3. The default configuration distributed with the package. The first available configuration file will be loaded. """ ENVVAR_PREFIX = "MOBILIZON_RESHARE" config = Dynaconf( environments=True, envvar_prefix=ENVVAR_PREFIX, settings_files=get_settings_files_paths(), validators=validators or [], ) # TODO use validation control in dynaconf 3.2.0 once released config.validators.validate() return config
def test_use_default_value_when_yaml_is_empty_and_explicitly_marked(tmpdir): tmpfile = tmpdir.join("settings.yaml") tmpfile.write(YAML) settings = Dynaconf( settings_file=str(tmpfile), validators=[ # Explicitly say thar default must be applied to None Validator( "hasemptyvalues.key1", default="value1", apply_default_on_none=True, ), # The following 2 defaults must be ignored Validator("hasemptyvalues.key2", default="value2"), Validator("hasemptyvalues.key3", default="value3"), # This one must be set because on YAML key is set to `@empty` Validator("hasemptyvalues.key4", default="value4"), ], ) assert settings.hasemptyvalues.key1 == "value1" assert settings.hasemptyvalues.key2 is None assert settings.hasemptyvalues.key3 is None assert settings.hasemptyvalues.key4 == "value4"
def _get_settings(self) -> Mapping: """ Parse the settings file from the conf/ folder """ # Warn the user if a configuration target is missing targets = { "globals.yaml": warnings.w011, "locals.yaml": warnings.w012, } for target, w in targets.items(): if not (self._root / self._stati_config.configuration / target).exists(): w(__name__) # Fetch all the config file, in the reversed preceding order (to allow for variables shadowing) base = self._root / self._stati_config.configuration settings = Dynaconf( settings_files=[base / target for target in targets.keys()], load_dotenv=False, ) return settings
from dynaconf import Dynaconf settings = Dynaconf( envvar_prefix="DOCUMENTO_PRINTSERVER", settings_files=["/etc/documento/printserver/settings.toml"], )
for req in requireds: if _config.get(req) is None: raise ValidationError( f'{req} are required in env {_config["ENV"]}') if _config['JWT']['SECRET'] == DEFAULT_SECRET: raise ValidationError(f'Use default secret in env {_config["ENV"]}') _config['DEBUG'] = False _config['TESTING'] = False config = Dynaconf( envvar_prefix='PYSHARE', settings_files=[ 'settings.yaml', 'settings.prod.yaml', ], includes=[ '.secrets.yaml', ], validators=[ Validator('ENV', default='development'), Validator('JWT.SECRET', default=DEFAULT_SECRET), # If no specified, decided by env Validator('DEBUG', default=True), Validator('TESTING', default=True), ], ) if config['ENV'] != 'development': non_dev_check(config)
from dynaconf import Dynaconf settingsenv = Dynaconf(environments=True) settings = Dynaconf()
def test_local_set_merge_dict(): settings = Dynaconf() settings.set("DATABASE", {"host": "localhost", "port": 666}) # calling twice does not change anything settings.set("DATABASE", {"host": "localhost", "port": 666}) assert settings.DATABASE == {"host": "localhost", "port": 666} settings.set("DATABASE", { "host": "new", "user": "******", "dynaconf_merge": True }) assert settings.DATABASE == {"host": "new", "port": 666, "user": "******"} assert settings.DATABASE.HOST == "new" assert settings.DATABASE.user == "admin" settings.set("DATABASE.attrs", {"a": ["b", "c"]}) settings.set("DATABASE.attrs", {"dynaconf_merge": {"foo": "bar"}}) assert settings.DATABASE.attrs == {"a": ["b", "c"], "foo": "bar"} settings.set("DATABASE.attrs", {"yeah": "baby", "dynaconf_merge": True}) assert settings.DATABASE.attrs == { "a": ["b", "c"], "foo": "bar", "yeah": "baby", } settings.set( "DATABASE.attrs", { "a": ["d", "e", "dynaconf_merge"], "dynaconf_merge": True }, ) assert settings.DATABASE.attrs == { "a": ["b", "c", "d", "e"], "foo": "bar", "yeah": "baby", } settings.set("DATABASE.attrs.a", ["d", "e", "f", "dynaconf_merge_unique"]) assert settings.DATABASE.attrs.a == ["b", "c", "d", "e", "f"]
from __future__ import annotations from dynaconf import Dynaconf settings = Dynaconf(settings_files="settings.yaml", environments=True) print(settings.get("level1").get("level2").get("level3")) print(settings.get("level1").get("level2")) print(settings.get("level1")) print(settings.get("level1.level2.level3")) print(settings.get("level1.level2")) print(settings.get("level1")) print(settings.get("template")) print(settings.get("value0")) print(settings.get("level1.value1")) print(settings.get("level1.level2.value2")) print(settings.get("level1.level2.level3.value3")) assert settings.get("level1.level2.level3").to_dict() == { "value3": "Whoohoo world/level3/jinja" } assert settings.get("level1.level2").to_dict() == { "value2": "HELLO world/level2/jinja", "level3": { "value3": "Whoohoo world/level3/jinja" }, } assert settings.get("level1").to_dict() == { "template2": "Whoohoo @tpl@",
Returns: The created `AnsibleDistribution`. """ # Create a distribution. body = gen_distribution() body["repository"] = repo.pulp_href distribution_create = self.distributions_api.create(body) created_resources = monitor_task( distribution_create.task).created_resources distribution = self.distributions_api.read(created_resources[0]) self.addCleanup(self.distributions_api.delete, distribution.pulp_href) return distribution settings = Dynaconf(settings_files=[ "pulp_ansible/tests/assets/func_test_settings.py", "/etc/pulp/settings.py" ]) def get_psql_smash_cmd(sql_statement): """ Generate a command in a format for pulp smash cli client to execute the specified SQL statement. The implication is that PostgreSQL is always running on localhost. Args: sql_statement(str): An SQL statement to execute. Returns: tuple: a command in the format for pulp smash cli client """ host = "localhost" user = settings.DATABASES["default"]["USER"]
from dynaconf import Dynaconf settings = Dynaconf( envvar_prefix="DYNACONF", settings_files=['settings.toml', '.secrets.toml'], )
from dynaconf import Dynaconf settings = Dynaconf(settings_files=["tsp.toml"])
# -*- coding: utf-8 -*- from dynaconf import Dynaconf settings = Dynaconf( envvar_prefix="DYNACONF", settings_files=["salver/agent/settings.yaml"], environments=True, )
from typing import Optional from fastapi import FastAPI from dynaconf import Dynaconf settings = Dynaconf( settings_file="config.toml", # location of config file environments=["ansible", "puppet"], # available modes/environments envvar_prefix="TEFLO", # prefix for exporting env vars env_switcher="TEFLO_MODE", # Variable that controls mode switch env="ANSIBLE", # Initial env/mode ) app = FastAPI() @app.get("/settings") def read_root(): return settings @app.get("/settings/{setting_name}") def read_item(setting_name: str): return {setting_name: settings.setting_name} if settings.current_env == "ANSIBLE": # this is the initial default assert settings.data_folder == "ansible/.teflo" if settings.current_env == "PUPPET": # only when TEFLO_MODE=puppet