Beispiel #1
0
 def init_app(self, app, **kwargs):
     """kwargs holds initial dynaconf configuration"""
     self.kwargs.update(kwargs)
     self.settings = self.dynaconf_instance or dynaconf.LazySettings(
         **self.kwargs)
     dynaconf.settings = self.settings  # rebind customized settings
     app.config = self.make_config(app)
     app.dynaconf = self.settings
def load_config(workspace: Optional[str] = None) -> dynaconf.LazySettings:
    """Load package config for the selected workspace."""
    settings = dynaconf.LazySettings(
        ENV_FOR_DYNACONF=workspace or get_workspace(),
        ROOT_PATH_FOR_DYNACONF=os.path.dirname(__file__),
        LOADERS_FOR_DYNACONF=[
            "sentiment_flanders.config.terraform_loader",
            "dynaconf.loaders.env_loader",
        ],
    )
    return settings
Beispiel #3
0
    def init_app(self, app, **kwargs):
        """kwargs holds initial dynaconf configuration"""
        self.kwargs.update(kwargs)
        self.settings = self.dynaconf_instance or dynaconf.LazySettings(
            **self.kwargs)
        dynaconf.settings = self.settings  # rebind customized settings
        app.config = self.make_config(app)
        app.dynaconf = self.settings

        if self.extensions_list:
            if not isinstance(self.extensions_list, str):
                self.extensions_list = "EXTENSIONS"
            app.config.load_extensions(self.extensions_list)
Beispiel #4
0
 def __readTestData(self):
     filePath = self._testCache.config_service.get('testdatapath')
     self._testCache.logger_service.logger.debug("Reading test data from - " + filePath)
     self.__defaultdata = dynaconf.LazySettings(SETTINGS_FILE_FOR_DYNACONF=filePath)
Beispiel #5
0
https://docs.djangoproject.com/en/2.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""

import dynaconf  # type: ignore
from pathlib import Path
from typing import List

# Use dynaconf explicit mode
# See https://www.dynaconf.com/django/#explicit-mode
settings = dynaconf.LazySettings(
    warn_dynaconf_global_settings=True,
    environments=True,
    lowercase_read=False,
    load_dotenv=True,
    default_settings_paths=dynaconf.constants.DEFAULT_SETTINGS_FILES,
)

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = Path(__file__).resolve().parent.parent.parent.as_posix()
DATA_DIR = Path(BASE_DIR, 'data').as_posix()

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = settings.get('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
Beispiel #6
0
def load(django_settings_module_name=None, **kwargs):  # pragma: no cover
    if not django_installed:
        raise RuntimeError("To use this extension django must be installed "
                           "install it with: pip install django")

    try:
        django_settings_module = sys.modules[django_settings_module_name]
    except KeyError:
        django_settings_module = sys.modules[
            os.environ["DJANGO_SETTINGS_MODULE"]]

    settings_file = os.path.abspath(django_settings_module.__file__)
    _root_path = os.path.dirname(settings_file)

    # 1) Create the lazy settings object reusing settings_module consts
    options = {
        k: v
        for k, v in django_settings_module.__dict__.items() if k.isupper()
    }
    options.update(kwargs)
    options.setdefault("SKIP_FILES_FOR_DYNACONF",
                       [settings_file, "dynaconf_merge"])
    options.setdefault("ROOT_PATH_FOR_DYNACONF", _root_path)
    options.setdefault("ENVVAR_PREFIX_FOR_DYNACONF", "DJANGO")
    options.setdefault("ENV_SWITCHER_FOR_DYNACONF", "DJANGO_ENV")
    options.setdefault("ENVIRONMENTS_FOR_DYNACONF", True)
    options.setdefault("load_dotenv", True)
    options.setdefault("default_settings_paths",
                       dynaconf.DEFAULT_SETTINGS_FILES)

    class UserSettingsHolder(dynaconf.LazySettings):
        _django_override = True

    lazy_settings = dynaconf.LazySettings(**options)
    dynaconf.settings = lazy_settings  # rebind the settings

    # 2) Set all settings back to django_settings_module for 'django check'
    lazy_settings.populate_obj(django_settings_module)

    # 3) Bind `settings` and `DYNACONF`
    setattr(django_settings_module, "settings", lazy_settings)
    setattr(django_settings_module, "DYNACONF", lazy_settings)

    # 4) keep django original settings
    dj = {}
    for key in dir(django_settings):
        if (key.isupper() and (key != "SETTINGS_MODULE")
                and key not in lazy_settings.store):
            dj[key] = getattr(django_settings, key, None)
        dj["ORIGINAL_SETTINGS_MODULE"] = django_settings.SETTINGS_MODULE

    lazy_settings.update(dj)

    # 5) Patch django.conf.settings
    class Wrapper:

        # lazy_settings = conf.settings.lazy_settings

        def __getattribute__(self, name):
            if name == "settings":
                return lazy_settings
            if name == "UserSettingsHolder":
                return UserSettingsHolder
            return getattr(conf, name)

    # This implementation is recommended by Guido Van Rossum
    # https://mail.python.org/pipermail/python-ideas/2012-May/014969.html
    sys.modules["django.conf"] = Wrapper()

    # 6) Enable standalone scripts to use Dynaconf
    # This is for when `django.conf.settings` is imported directly
    # on external `scripts` (out of Django's lifetime)
    for stack_item in reversed(inspect.stack()):
        if isinstance(stack_item.frame.f_globals.get("settings"),
                      conf.LazySettings):
            stack_item.frame.f_globals["settings"] = lazy_settings

    return lazy_settings