Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from rochacbruno/add_yaml_support
Added YAML support
- Loading branch information
Showing
15 changed files
with
240 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# coding: utf-8 | ||
try: | ||
import yaml | ||
except ImportError as e: # pragma: no cover | ||
yaml = None | ||
|
||
IDENTIFIER = 'yaml_loader' | ||
|
||
|
||
def load(obj, namespace=None, silent=True, key=None, filename=None): | ||
""" | ||
Reads and loads in to "settings" a single key or all keys from yaml file | ||
:param obj: the settings instance | ||
:param namespace: settings namespace default='DYNACONF' | ||
:param silent: if errors should raise | ||
:param key: if defined load a single key, else load all in namespace | ||
:return: None | ||
""" | ||
if yaml is None: # pragma: no cover | ||
raise RuntimeError( | ||
"PyYAML package is not installed in your environment.\n" | ||
"To use this loader you have to install it with\n" | ||
"pip install PyYAML\n" | ||
"or\n" | ||
"pip install dynaconf[yaml]" | ||
) | ||
|
||
filename = filename or obj.get('YAML') | ||
if not filename: | ||
return | ||
|
||
namespace = namespace or obj.DYNACONF_NAMESPACE | ||
|
||
clean(obj, namespace, identifier=filename) | ||
|
||
if filename.endswith(('.yaml', '.yml')): # pragma: no cover | ||
yaml_data = yaml.load(open(filename)) | ||
else: | ||
# for tests it is possible to pass YAML string | ||
yaml_data = yaml.load(filename) | ||
|
||
yaml_data = {key.lower(): value for key, value in yaml_data.items()} | ||
|
||
try: | ||
data = yaml_data[namespace.lower()] | ||
except KeyError: | ||
raise KeyError( | ||
'%s namespace not defined in %s' % (namespace, filename) | ||
) | ||
|
||
if not key: | ||
obj.update(data, loader_identifier=filename) | ||
else: | ||
obj.set(key, data.get(key)) | ||
|
||
def clean(obj, namespace, silent=True, identifier=IDENTIFIER): # noqa | ||
for key in obj.loaded_by_loaders.get(identifier, {}): | ||
obj.unset(key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from dynaconf import settings | ||
|
||
# export DYNACONF_SETTINGS=settings.yaml | ||
# or run $ source env.sh | ||
|
||
# print all values in dynaconf: namespace of yaml file | ||
print(settings.HOST) | ||
print(settings.PORT) | ||
print(settings.USERNAME) | ||
print(settings.PASSWORD) | ||
print(settings.LEVELS) | ||
print(settings.TEST_LOADERS) | ||
print(settings.MONEY) | ||
print(settings.AGE) | ||
print(settings.ENABLED) | ||
|
||
# using development: namespace values for context | ||
with settings.using_namespace('DEVELOPMENT'): | ||
print(settings.ENVIRONMENT) | ||
print(settings.HOST) | ||
|
||
# back to default dynaconf: namespace | ||
print(settings.get('ENVIRONMENT')) | ||
print(settings.HOST) | ||
|
||
# set namespace to development: | ||
settings.namespace('development') | ||
print(settings.HOST) | ||
print(settings.ENVIRONMENT) | ||
|
||
# back to default namespace again | ||
settings.namespace() | ||
print(settings.HOST) | ||
print(settings.get('ENVIRONMENT')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env bash | ||
export DYNACONF_SETTINGS=settings.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
dynaconf: | ||
host: server.com | ||
port: 5000 | ||
username: admin | ||
PASSWORD: Secret | ||
levels: | ||
- debug | ||
- info | ||
- warning | ||
test_loaders: | ||
dev: test_dev | ||
prod: test_prod | ||
money: 500.50 | ||
age: 42 | ||
enabled: true | ||
development: | ||
environment: this is development | ||
host: dev_server.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from dynaconf import settings | ||
|
||
print(settings.YAML) | ||
print(settings.HOST) | ||
print(settings.PORT) | ||
|
||
|
||
# using development: namespace values for context | ||
with settings.using_namespace('DEVELOPMENT'): | ||
print(settings.ENVIRONMENT) | ||
print(settings.HOST) | ||
|
||
# back to default dynaconf: namespace | ||
print(settings.get('ENVIRONMENT')) | ||
print(settings.HOST) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env bash | ||
# optional if not defined in settings.py | ||
export DYNACONF_YAML=extra_settings.yaml |
6 changes: 6 additions & 0 deletions
6
example/yaml_example/yaml_as_extra_config/extra_settings.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
dynaconf: | ||
host: server.com | ||
port: 5000 | ||
development: | ||
environment: this is development | ||
host: dev_server.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
HOST = 'default.com' | ||
PORT = 9000 | ||
YAML = 'extra_settings.yaml' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os | ||
from dynaconf.loaders.env_loader import load | ||
|
||
os.environ['DYNACONF_HOSTNAME'] = 'host.com' | ||
os.environ['DYNACONF_PORT'] = '@int 5000' | ||
os.environ['DYNACONF_VALUE'] = '@float 42.1' | ||
os.environ['DYNACONF_ALIST'] = '@json ["item1", "item2", "item3"]' | ||
os.environ['DYNACONF_ADICT'] = '@json {"key": "value"}' | ||
os.environ['DYNACONF_DEBUG'] = '@bool true' | ||
os.environ['PROJECT1_HOSTNAME'] = 'otherhost.com' | ||
os.environ['PROJECT1_PORT'] = '@int 8080' | ||
|
||
from dynaconf import settings # noqa | ||
settings.configure() | ||
|
||
def test_env_loader(): | ||
assert settings.HOSTNAME == 'host.com' | ||
|
||
|
||
def test_single_key(): | ||
load(settings, namespace='PROJECT1', key='HOSTNAME') | ||
assert settings.HOSTNAME == 'otherhost.com' | ||
assert settings.PORT == 5000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import pytest | ||
from dynaconf import LazySettings | ||
from dynaconf.loaders.yaml_loader import load | ||
|
||
settings = LazySettings( | ||
DYNACONF_NAMESPACE='EXAMPLE', | ||
) | ||
|
||
|
||
YAML = """ | ||
example: | ||
host: server.com | ||
port: 8080 | ||
development: | ||
host: dev_server.com | ||
""" | ||
|
||
def test_load_from_yaml(): | ||
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' | ||
|
||
|
||
def test_no_filename_is_none(): | ||
assert load(settings) is None | ||
|
||
|
||
def test_error_on_invalid_namespace(): | ||
with pytest.raises(KeyError): | ||
load(settings, filename=YAML, namespace='FOOBAR') | ||
|
||
|
||
def test_load_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 | ||
|
||
|
||
def test_extra_yaml(): | ||
load(settings, filename=YAML) | ||
yaml = """ | ||
example: | ||
hello: world | ||
""" | ||
settings.set('YAML', yaml) | ||
settings.execute_loaders(namespace='EXAMPLE') | ||
assert settings.HELLO == 'world' |