def test_env(monkeypatch, settings_dict): """ Test ``override(d, env='PRJ_')`` :param settings_dict: default fixture - patching modules and environments :param settings_dict: custom fixture - initial settings dict """ monkeypatch.setenv('PRJ_DEBUG', 'False') monkeypatch.setenv( 'PRJ_DATABASES', '{ default: { ENGINE: django.db.backends.sqlite3, NAME: prj.sqlite3 }}' ) override(settings_dict, env='PRJ_') assert settings_dict == { 'DEBUG': False, 'STATIC_ROOT': 'static', 'STATIC_URL': '/static/', 'DATABASES': { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'prj.sqlite3' } } }
def test_none(settings_dict): """ Test ``override(d)`` - shouldn't change anything :param settings_dict: custom fixture - initial settings dict """ override(settings_dict) assert settings_dict == { 'DEBUG': True, 'STATIC_ROOT': 'static', 'STATIC_URL': '/static/' }
def test_wrong_yaml_path(settings_dict): """ Test ``override(d, yaml='/wrong/path/to/settings.yaml')`` :param settings_dict: custom fixture - initial settings dict """ yaml_path = os.path.join(tests_dir, 'wrong', 'path', 'to', 'settings.yaml') override(settings_dict, yaml=yaml_path) assert settings_dict == { 'DEBUG': True, 'STATIC_ROOT': 'static', 'STATIC_URL': '/static/' }
def test_yaml_path(settings_dict): """ Test ``override(d, yaml='/path/to/settings.yaml')`` :param settings_dict: custom fixture - initial settings dict """ yaml_path = os.path.join(tests_dir, 'settings.yaml') override(settings_dict, yaml=yaml_path) assert settings_dict == { 'DEBUG': False, 'STATIC_ROOT': '/var/www/static/', 'STATIC_URL': '/static/' }
def resolve(settings, yaml=None, env=None): """ If either yaml or env is given, this delegates to settings_overrider.override to override settings with environment variables or a yaml file. :param dict settings: settings dict to be updated; usually it's ``globals()`` :param yaml: path to YAML file :type yaml: str or FileIO :param str env: prefix for environment variables """ resolver.settings = settings if (yaml is not None or env is not None): override(settings, yaml=yaml, env=env) # Resolve all the deferreds, recursively for k, v in settings.items(): if isinstance(v, Deferred): settings[k] = resolver.__getattr__(k)
def test_yaml_path_with_env(monkeypatch, settings_dict): """ Test ``override(d, yaml='/path/to/settings.yaml', env='PRJ_')`` :param settings_dict: default fixture - patching modules and environments :param settings_dict: custom fixture - initial settings dict """ monkeypatch.setenv('PRJ_DEBUG', 'True') monkeypatch.setenv('PRJ_STATIC_URL', 'http://example.com/static/') override(settings_dict, yaml=os.path.join(tests_dir, 'settings.yaml'), env='PRJ_') assert settings_dict == { 'DEBUG': True, 'STATIC_ROOT': '/var/www/static/', 'STATIC_URL': 'http://example.com/static/' }
LOGIN_REDIRECT_URL = '/' LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console': { 'class': 'logging.StreamHandler' } }, 'loggers': { 'django': { 'handlers': ['console'], 'level': 'INFO' } } } # --------------------------------------- Project settings ---------------------------------------- VAULT_BASE_URL = '/vault/' # -------------------------------------- settings-overrider --------------------------------------- yaml_settings_file = os.path.join('etc', 'settings.yaml') if os.path.exists(yaml_settings_file): print("Overriding settings from 'etc/settings.yaml' file ...") override(globals(), yaml=yaml_settings_file) override(globals(), env='DJANGO_')
'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join('var', 'data', 'db', 'db.sqlite3'), } } STATIC_URL = '/static/' STATIC_ROOT = os.path.join('var', 'data', 'static') STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'compressor.finders.CompressorFinder', ) COMPRESS_PRECOMPILERS = (('text/x-scss', 'compressor_toolkit.precompilers.SCSSCompiler'), ('module', 'compressor_toolkit.precompilers.ES6Compiler')) # --------------------------------------- Project settings ---------------------------------------- # Override settings with env variables with "DJANGO_" prefix or with local YAML file yaml_path = os.path.join('etc', 'settings.yaml') env_prefix = 'DJANGO_' if os.path.exists(yaml_path): print("Using settings from \"{}\" ...".format(yaml_path)) override(globals(), yaml=yaml_path) override(globals(), env=env_prefix)
'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join('var', 'data', 'db', 'db.sqlite3'), } } STATIC_URL = '/static/' STATIC_ROOT = os.path.join('var', 'data', 'static') STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'compressor.finders.CompressorFinder', ) COMPRESS_PRECOMPILERS = ( ('text/x-scss', 'compressor_toolkit.precompilers.SCSSCompiler'), ('module', 'compressor_toolkit.precompilers.ES6Compiler') ) # --------------------------------------- Project settings ---------------------------------------- # Override settings with env variables with "DJANGO_" prefix or with local YAML file yaml_path = os.path.join('etc', 'settings.yaml') env_prefix = 'DJANGO_' if os.path.exists(yaml_path): print("Using settings from \"{}\" ...".format(yaml_path)) override(globals(), yaml=yaml_path) override(globals(), env=env_prefix)