def test_try_to_load_from_py_module_name(tmpdir): settings = DynaconfDict() dummy_folder = tmpdir.mkdir("dummy") dummy_folder.join("dummy_module.py").write('FOO = "bar"') dummy_folder.join("__init__.py").write('print("initing dummy...")') try_to_load_from_py_module_name(settings, "dummy.dummy_module") assert settings.exists("FOO")
def test_py_loader_from_file(tmpdir): settings = DynaconfDict() dummy_path = tmpdir.join("dummy_module.py") with open(str(dummy_path), "w", encoding=default_settings.ENCODING_FOR_DYNACONF) as f: f.write('FOO = "bar"') load(settings, "dummy_module.py") os.remove("dummy_module.py") load(settings, "dummy_module.py") # will be ignored not found assert settings.get("FOO") == "bar"
def _one_pipeline_setup(pipelines_config=None, set_source_el=True): if pipelines_config is None: pipelines_config = _get_pipelines_config() # override source op with a mock test class if set_source_el: log.info("set source=_TestSourceElement") interpreter.Pipeline.PIPELINE_OPS["source"] = _TestSourceElement return interpreter.get_pipelines(pipelines_config=DynaconfDict(pipelines_config))
def test_py_loader(): settings = DynaconfDict(_no_project_root=True) with open('dummy_module.py', 'w') as f: f.write('FOO = "bar"') load(settings, 'dummy_module') os.remove('dummy_module.py') load(settings, 'dummy_module.py')
def test_py_loader(): settings = DynaconfDict(_no_project_root=True) with io.open('dummy_module.py', 'w', encoding=default_settings.ENCODING_FOR_DYNACONF) as f: f.write('FOO = "bar"') load(settings, 'dummy_module') os.remove('dummy_module.py') load(settings, 'dummy_module.py')
def write(settings_path, settings_data, merge=True): """Write data to a settings file. :param settings_path: the filepath :param settings_data: a dictionary with data :param merge: boolean if existing file should be merged with new data """ if settings_path.exists() and merge: # pragma: no cover existing = DynaconfDict() load(existing, str(settings_path)) settings_data = dictmerge(existing, settings_data) with open(str(settings_path), 'w') as f: f.writelines([ "{} = {}\n".format(k.upper(), repr(v)) for k, v in settings_data.items() ])
def write(settings_path, settings_data, merge=True): """Write data to a settings file. :param settings_path: the filepath :param settings_data: a dictionary with data :param merge: boolean if existing file should be merged with new data """ settings_path = Path(settings_path) if settings_path.exists() and merge: # pragma: no cover existing = DynaconfDict() load(existing, str(settings_path)) object_merge(existing, settings_data) with io.open( str(settings_path), "w", encoding=default_settings.ENCODING_FOR_DYNACONF, ) as f: f.writelines( [f"{upperfy(k)} = {repr(v)}\n" for k, v in settings_data.items()])
def test_silently_try_to_load_from_py_module_name(tmpdir): settings = DynaconfDict() try_to_load_from_py_module_name(settings, "foo.bar.dummy", silent=True) assert settings.exists("FOO") is False
def test_negative_try_to_load_from_py_module_name(tmpdir): settings = DynaconfDict() with pytest.raises(ImportError): try_to_load_from_py_module_name(settings, "foo.bar.dummy")
def configure_dynaconf(app): # extra vars in .secrets.yml override values on quokka.yml # secrets file is a hidden file and must be excluded on .gitignore # all password, token and other sensitive must go there # or exported as env var ex: QUOKKA_SECRET_KEY=12345 settings_file = 'quokka.yml,.secrets.yml' initial_envmode = app.config.get('ENVMODE') # Extension is supposed to override envmode FlaskDynaconf( app, ENVVAR_FOR_DYNACONF="QUOKKA_SETTINGS_MODULE", GLOBAL_ENV_FOR_DYNACONF='QUOKKA', SETTINGS_MODULE_FOR_DYNACONF=settings_file, SILENT_ERRORS_FOR_DYNACONF=True ) # Configure extra environment envmode = initial_envmode or app.config.get('ENVMODE') if envmode is not None: yaml_loader.load( obj=app.config, env=envmode, filename=settings_file ) # overload with envvars env_loader.load_from_env( identifier=envmode, key=None, env=f'quokka_{envmode}', obj=app.config, silent=True ) # configure theme options app.theme_context = DynaconfDict({ 'JINJA_ENVIRONMENT': app.jinja_env, 'DEFAULT_LANG': app.config.get('BABEL_DEFAULT_LOCALE'), 'default_locale': app.config.get('BABEL_DEFAULT_LOCALE'), 'PAGES': [], 'pages': [], 'tags': [], 'articles': [], 'categories': [], # https://github.com/getpelican/pelican-plugins/tree/master/tag_cloud 'tag_cloud': [], 'CATEGORIES_URL': 'categories/index.html', 'JINJA_EXTENSIONS': app.jinja_env.extensions, 'USE_LESS': False, # For some themes like bootstrap3 theme SITEURL must be '' 'SITEURL': 'http://localhost:5000', 'THEME_STATIC_DIR': 'theme', 'FAVICON': 'favicon.ico', 'FAVICON_IE': 'favicon.ico', 'FAVICON_FILENAME': 'favicon.ico', # 'AVATAR': 'LOAD FROM UPLOADS', 'NEWEST_FIRST_ARCHIVES': True }) default_loader(app.theme_context) # load theme variables from YAML file yaml_loader.load( obj=app.theme_context, env='theme', filename=app.config.get('SETTINGS_MODULE_FOR_DYNACONF') ) # overrride with QUOKKA_THEME_ prefixed env vars if exist env_loader.load_from_env( identifier='theme', key=None, env='quokka_theme', obj=app.theme_context, silent=True ) # remove prefix for pelican-themes active = app.theme_context.get('ACTIVE', 'default') if active.startswith('pelican'): active = active.lstrip('pelican-') app.theme_context['ACTIVE'] = active # load theme specific variables from YAML yaml_loader.load( obj=app.theme_context, env=f'theme_{app.theme_context.get("ACTIVE")}', filename=app.config.get('SETTINGS_MODULE_FOR_DYNACONF') ) # overrride with QUOKKA_THEME_THEMENAME prefixed env vars if exist env_loader.load_from_env( identifier=f'theme_{app.theme_context.get("ACTIVE")}', key=None, env=f'quokka_theme_{app.theme_context.get("ACTIVE")}', obj=app.theme_context, silent=True ) # mark strings as safe Markup for k, v in app.theme_context.items(): if isinstance(v, str): app.theme_context[k] = Markup(v)