Exemple #1
0
def module_loader(obj, settings_module=None, namespace=None, silent=False):
    """Loads from defined settings module, path or yaml"""
    settings_module = settings_module or obj.settings_module
    if not settings_module:  # pragma: no cover
        return

    if settings_module.endswith(('.yaml', '.yml')):
        yaml_loader.load(obj,
                         filename=settings_module,
                         namespace=namespace,
                         silent=silent)
        return

    # load from default defined module if exists (never gets cleaned)
    load_from_module(obj, settings_module)

    if namespace and namespace != obj.DYNACONF_NAMESPACE:
        if settings_module.endswith('.py'):
            dirname = os.path.dirname(settings_module)
            filename, extension = os.path.splitext(
                os.path.basename(settings_module))
            new_filename = "{0}_{1}{2}".format(namespace.lower(), filename,
                                               extension)
            namespace_settings_module = os.path.join(dirname, new_filename)
        else:
            namespace_settings_module = "{0}_{1}".format(
                namespace.lower(), settings_module)

        load_from_module(obj,
                         namespace_settings_module,
                         identifier='DEFAULT_MODULE_{0}'.format(
                             namespace.upper()),
                         silent=True)
Exemple #2
0
def test_empty_env():
    settings = LazySettings(environments=True)
    _yaml = """
    default:
        foo: bar
    development:
    """
    load(settings, filename=_yaml)
    assert settings.FOO == "bar"
Exemple #3
0
def test_using_env(tmpdir):
    load(settings, filename=YAML)
    assert settings.HOST == "prodserver.com"

    tmpfile = tmpdir.mkdir("sub").join("test_using_env.yaml")
    tmpfile.write(YAML)
    with settings.using_env("DEVELOPMENT", filename=str(tmpfile)):
        assert settings.HOST == "devserver.com"
    assert settings.HOST == "prodserver.com"
Exemple #4
0
def test_extra_yaml():
    """Test loading extra yaml file"""
    load(settings, filename=YAML)
    yaml = """
    example:
       helloexample: world
    """
    settings.set("YAML", yaml)
    settings.execute_loaders(env="EXAMPLE")
    assert settings.HELLOEXAMPLE == "world"
def test_extra_yaml():
    """Test loading extra yaml file"""
    load(settings, filename=YAML)
    yaml = """
    example:
       hello: world
    """
    settings.set('YAML', yaml)
    settings.execute_loaders(namespace='EXAMPLE')
    assert settings.HELLO == 'world'
Exemple #6
0
 def execute_loaders(self, namespace=None, silent=None, key=None):
     """Execute all internal and registered loaders"""
     default_loader(self)
     module_loader(self, namespace=namespace)
     if self.exists('YAML'):
         yaml_loader.load(self,
                          namespace=namespace,
                          filename=self.get('YAML'))
     silent = silent or self.DYNACONF_SILENT_ERRORS
     for loader in self.loaders:
         loader.load(self, namespace, silent=silent, key=key)
Exemple #7
0
def test_load_single_key():
    """Test loading a single key"""
    yaml = """
    foo:
       bar: blaz
       zaz: naz
    """
    load(settings, filename=yaml, env="FOO", key="bar")
    assert settings.BAR == "blaz"
    assert settings.exists("BAR") is True
    assert settings.exists("ZAZ") is False
Exemple #8
0
def test_envless():
    settings = LazySettings()
    _yaml = """
    a: a,b
    colors__white__code: "#FFFFFF"
    COLORS__white__name: "white"
    """
    load(settings, filename=_yaml)
    assert settings.a == "a,b"
    assert settings.COLORS.white.code == "#FFFFFF"
    assert settings.COLORS.white.name == "white"
def test_load_single_key():
    """Test loading a single key"""
    yaml = """
    foo:
       bar: blaz
       zaz: naz
    """
    load(settings, filename=yaml, namespace='FOO', key='bar')
    assert settings.BAR == 'blaz'
    assert settings.exists('BAR') is True
    assert settings.exists('ZAZ') is False
Exemple #10
0
def test_prefix():
    settings = LazySettings(filter_strategy=PrefixFilter("prefix"))
    _yaml = """
    prefix_a: a,b
    prefix_colors__white__code: "#FFFFFF"
    COLORS__white__name: "white"
    """
    load(settings, filename=_yaml)
    assert settings.a == "a,b"
    assert settings.COLORS.white.code == "#FFFFFF"
    with pytest.raises(AttributeError):
        settings.COLORS.white.name
Exemple #11
0
 def load_extra_yaml(self, namespace, silent, key):
     if self.get('YAML') is not None:
         self.logger.warning(
             "The use of YAML var is deprecated, please define multiple "
             "filepaths instead: "
             "e.g: SETTINGS_MODULE_FOR_DYNACONF = "
             "'settings.py,settings.yaml,settings.toml'")
         yaml_loader.load(self,
                          namespace=namespace,
                          filename=self.get('YAML'),
                          silent=silent,
                          key=key)
Exemple #12
0
def test_load_dunder():
    """Test load with dunder settings"""
    yaml = """
    foo:
       bar: blaz
       zaz: naz
       colors__black__code: '#000000'
       COLORS__black__name: Black
    """
    load(settings, filename=yaml, env="FOO")
    assert settings.COLORS.black.code == "#000000"
    assert settings.COLORS.black.name == "Black"
def test_multi_extra_yaml():
    """Test loading extra yaml file"""
    load(settings, filename=YAMLS)
    yaml = """
    example:
       hello: world
    """
    yaml2 = """
    example:
       foo: bar
    """
    settings.set('YAML', [yaml, yaml2])
    settings.execute_loaders(namespace='EXAMPLE')
    assert settings.HELLO == 'world'
    assert settings.FOO == 'bar'
Exemple #14
0
    def load_extra_yaml(self, env, silent, key):
        """This is deprecated, kept for compat

        .. deprecated:: 1.0.0
            Use multiple settings files instead.
        """
        if self.get('YAML') is not None:
            self.logger.warning(
                "The use of YAML var is deprecated, please define multiple "
                "filepaths instead: "
                "e.g: SETTINGS_MODULE_FOR_DYNACONF = "
                "'settings.py,settings.yaml,settings.toml'")
            yaml_loader.load(self,
                             env=env,
                             filename=self.get('YAML'),
                             silent=silent,
                             key=key)
Exemple #15
0
    def load_extra_yaml(self, env, silent, key):
        """This is deprecated, kept for compat

        .. deprecated:: 1.0.0
            Use multiple settings or INCLUDES_FOR_DYNACONF files instead.
        """
        if self.get("YAML") is not None:
            self.logger.warning(
                "The use of YAML var is deprecated, please define multiple "
                "filepaths instead: "
                "e.g: SETTINGS_FILE_FOR_DYNACONF = "
                "'settings.py,settings.yaml,settings.toml' or "
                "INCLUDES_FOR_DYNACONF=['path.toml', 'folder/*']")
            yaml_loader.load(
                self,
                env=env,
                filename=self.find_file(self.get("YAML")),
                silent=silent,
                key=key,
            )
Exemple #16
0
def configure_dynaconf(app):
    settings_file = 'quokka.yml'
    initial_envmode = app.config.get('ENVMODE')

    # Extension is supposed to override envmode
    FlaskDynaconf(
        app,
        ENVVAR_FOR_DYNACONF="QUOKKA_SETTINGS_MODULE",
        DYNACONF_NAMESPACE='QUOKKA',
        SETTINGS_MODULE_FOR_DYNACONF=settings_file,
        # extra yaml file override values on settings.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
        YAML='.secrets.yml')

    # Configure extra environment
    envmode = initial_envmode or app.config.get('ENVMODE')
    if envmode is not None:
        yaml_loader.load(obj=app.config,
                         namespace=envmode,
                         filename=settings_file)
Exemple #17
0
def test_load_from_yaml():
    """Assert loads from YAML string"""
    load(settings, filename=YAML)
    assert settings.HOST == 'server.com'
    assert settings.PORT == 8080
    load(settings, filename=YAML, namespace='DEVELOPMENT')
    assert settings.HOST == 'dev_server.com'
    load(settings, filename=YAML)
    assert settings.HOST == 'server.com'
Exemple #18
0
def test_load_from_multiple_yaml():
    """Assert loads from YAML string"""
    load(settings, filename=YAMLS)
    assert settings.HOST == 'otheryaml.com'
    assert settings.PASSWORD == 123456
    assert settings.SECRET == 42.0
    assert settings.PORT == 8080
    assert settings.SERVICE['url'] == 'service.com'
    assert settings.SERVICE.url == 'service.com'
    assert settings.SERVICE.port == 80
    assert settings.SERVICE.auth.password == 'qwerty'
    assert settings.SERVICE.auth.test == 1234
    load(settings, filename=YAMLS, env='DEVELOPMENT')
    assert settings.PORT == 8080
    assert settings.HOST == 'otheryaml.com'
    load(settings, filename=YAMLS)
    assert settings.HOST == 'otheryaml.com'
    assert settings.PASSWORD == 123456
    load(settings, filename=YAML, env='DEVELOPMENT')
    assert settings.PORT == 8080
    assert settings.HOST == 'devserver.com'
    load(settings, filename=YAML)
    assert settings.HOST == 'prodserver.com'
    assert settings.PASSWORD == 11111
Exemple #19
0
def test_load_from_multiple_yaml():
    """Assert loads from YAML string"""
    load(settings, filename=YAMLS)
    assert settings.HOST == "otheryaml.com"
    assert settings.PASSWORD == 123456
    assert settings.SECRET == 42.0
    assert settings.PORT == 8080
    assert settings.SERVICE["url"] == "service.com"
    assert settings.SERVICE.url == "service.com"
    assert settings.SERVICE.port == 80
    assert settings.SERVICE.auth.password == "qwerty"
    assert settings.SERVICE.auth.test == 1234
    load(settings, filename=YAMLS, env="DEVELOPMENT")
    assert settings.PORT == 8080
    assert settings.HOST == "otheryaml.com"
    load(settings, filename=YAMLS)
    assert settings.HOST == "otheryaml.com"
    assert settings.PASSWORD == 123456
    load(settings, filename=YAML, env="DEVELOPMENT")
    assert settings.PORT == 8080
    assert settings.HOST == "devserver.com"
    load(settings, filename=YAML)
    assert settings.HOST == "prodserver.com"
    assert settings.PASSWORD == 11111
def test_load_from_yaml():
    """Assert loads from YAML string"""
    load(settings, filename=YAML)
    assert settings.HOST == 'server.com'
    assert settings.PORT == 8080
    assert settings.SERVICE['url'] == 'service.com'
    assert settings.SERVICE.url == 'service.com'
    assert settings.SERVICE.port == 80
    assert settings.SERVICE.auth.password == 'qwerty'
    assert settings.SERVICE.auth.test == 1234
    load(settings, filename=YAML, namespace='DEVELOPMENT')
    assert settings.HOST == 'dev_server.com'
    load(settings, filename=YAML)
    assert settings.HOST == 'server.com'
def test_cleaner():
    load(settings, filename=YAML)
    assert settings.HOST == 'server.com'
    assert settings.PORT == 8080
    assert settings.SERVICE['url'] == 'service.com'
    assert settings.SERVICE.url == 'service.com'
    assert settings.SERVICE.port == 80
    assert settings.SERVICE.auth.password == 'qwerty'
    assert settings.SERVICE.auth.test == 1234
    load(settings, filename=YAML, namespace='DEVELOPMENT')
    assert settings.HOST == 'dev_server.com'
    load(settings, filename=YAML)
    assert settings.HOST == 'server.com'
    clean(settings, settings.namespace)
Exemple #22
0
def test_load_from_yaml():
    """Assert loads from YAML string"""
    load(settings, filename=YAML)
    assert settings.HOST == "prodserver.com"
    assert settings.PORT == 8080
    assert settings.ALIST == ["item1", "item2", 23]
    assert settings.SERVICE["url"] == "service.com"
    assert settings.SERVICE.url == "service.com"
    assert settings.SERVICE.port == 80
    assert settings.SERVICE.auth.password == "qwerty"
    assert settings.SERVICE.auth.test == 1234
    load(settings, filename=YAML, env="DEVELOPMENT")
    assert settings.HOST == "devserver.com"
    load(settings, filename=YAML)
    assert settings.HOST == "prodserver.com"
Exemple #23
0
def test_load_from_yaml():
    """Assert loads from YAML string"""
    load(settings, filename=YAML)
    assert settings.HOST == 'prodserver.com'
    assert settings.PORT == 8080
    assert settings.ALIST == ['item1', 'item2', 23]
    assert settings.SERVICE['url'] == 'service.com'
    assert settings.SERVICE.url == 'service.com'
    assert settings.SERVICE.port == 80
    assert settings.SERVICE.auth.password == 'qwerty'
    assert settings.SERVICE.auth.test == 1234
    load(settings, filename=YAML, env='DEVELOPMENT')
    assert settings.HOST == 'devserver.com'
    load(settings, filename=YAML)
    assert settings.HOST == 'prodserver.com'
Exemple #24
0
def test_cleaner():
    load(settings, filename=YAML)
    assert settings.HOST == 'prodserver.com'
    assert settings.PORT == 8080
    assert settings.ALIST == ['item1', 'item2', 23]
    assert settings.SERVICE['url'] == 'service.com'
    assert settings.SERVICE.url == 'service.com'
    assert settings.SERVICE.port == 80
    assert settings.SERVICE.auth.password == 'qwerty'
    assert settings.SERVICE.auth.test == 1234
    load(settings, filename=YAML, env='DEVELOPMENT')
    assert settings.HOST == 'devserver.com'
    load(settings, filename=YAML)
    assert settings.HOST == 'prodserver.com'

    settings.clean()
    with pytest.raises(AttributeError):
        assert settings.HOST == 'prodserver.com'
Exemple #25
0
def test_cleaner():
    load(settings, filename=YAML)
    assert settings.HOST == "prodserver.com"
    assert settings.PORT == 8080
    assert settings.ALIST == ["item1", "item2", 23]
    assert settings.SERVICE["url"] == "service.com"
    assert settings.SERVICE.url == "service.com"
    assert settings.SERVICE.port == 80
    assert settings.SERVICE.auth.password == "qwerty"
    assert settings.SERVICE.auth.test == 1234
    load(settings, filename=YAML, env="DEVELOPMENT")
    assert settings.HOST == "devserver.com"
    load(settings, filename=YAML)
    assert settings.HOST == "prodserver.com"

    settings.clean()
    with pytest.raises(AttributeError):
        assert settings.HOST == "prodserver.com"
def test_no_key_error_on_invalid_namespace():
    """Assert error raised if namespace is not found in YAML"""
    load(settings, filename=YAML, namespace='FOOBAR', silent=True)
def test_key_error_on_invalid_namespace():
    """Assert error raised if namespace is not found in YAML"""
    with pytest.raises(KeyError):
        load(settings, filename=YAML, namespace='FOOBAR', silent=False)
def test_no_filename_is_none():
    """Assert if passed no filename return is None"""
    assert load(settings) is None
def test_multiple_filenames():
    load(settings, filename="a.yaml,b.yml,c.yaml,d.yml")
def test_empty_value():
    load(settings, filename="")
Exemple #31
0
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",
        DYNACONF_NAMESPACE='QUOKKA',
        SETTINGS_MODULE_FOR_DYNACONF=settings_file,
        DYNACONF_SILENT_ERRORS=True
    )

    # Configure extra environment
    envmode = initial_envmode or app.config.get('ENVMODE')
    if envmode is not None:
        yaml_loader.load(
            obj=app.config,
            namespace=envmode,
            filename=settings_file
        )
        # overload with envvars
        env_loader.load_from_env(
            identifier=envmode,
            key=None,
            namespace=f'quokka_{envmode}',
            obj=app.config,
            silent=True
        )

    # configure theme options
    app.theme_context = {
        '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
    }
    # load theme variables from YAML file
    yaml_loader.load(
        obj=app.theme_context,
        namespace='theme',
        filename=app.config.get('SETTINGS_MODULE')
    )
    # overrride with QUOKKA_THEME_ prefixed env vars if exist
    env_loader.load_from_env(
        identifier='theme',
        key=None,
        namespace='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,
        namespace=f'theme_{app.theme_context.get("ACTIVE")}',
        filename=app.config.get('SETTINGS_MODULE')
    )
    # 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,
        namespace=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)