add few ways to load config

This commit is contained in:
Kotyara 2017-04-08 12:27:29 +03:00
parent 7e3496f8aa
commit 06bd4a3251
2 changed files with 74 additions and 5 deletions

View File

@ -1,7 +1,12 @@
import os import os
import errno
import types import types
try:
import ujson as json
except ImportError:
import json
SANIC_PREFIX = 'SANIC_' SANIC_PREFIX = 'SANIC_'
@ -97,12 +102,53 @@ class Config(dict):
if key.isupper(): if key.isupper():
self[key] = getattr(obj, key) self[key] = getattr(obj, key)
def load_environment_vars(self): def from_json(self, filename, silent=False):
for k, v in os.environ.items(): """Updates the values in the config from a JSON file. This function
behaves as if the JSON object was a dictionary and passed to the
:meth:`from_mapping` function.
:param filename: the filename of the JSON file. This can either be an
absolute filename or a filename relative to the
root path.
:param silent: set to ``True`` if you want silent failure for missing
files.
""" """
Looks for any SANIC_ prefixed environment variables and applies try:
with open(filename) as json_file:
obj = json.loads(json_file.read())
except IOError as e:
if silent and e.errno in (errno.ENOENT, errno.EISDIR):
return False
e.strerror = 'Unable to load configuration file (%s)' % e.strerror
raise
return self.from_mapping(obj)
def from_mapping(self, *mapping, **kwargs):
"""Updates the config like :meth:`update` ignoring items with non-upper
keys.
"""
mappings = []
if len(mapping) == 1:
if hasattr(mapping[0], 'items'):
mappings.append(mapping[0].items())
else:
mappings.append(mapping[0])
elif len(mapping) > 1:
raise TypeError(
'expected at most 1 positional argument, got %d' % len(mapping)
)
mappings.append(kwargs.items())
for mapping in mappings:
for (key, value) in mapping:
if key.isupper():
self[key] = value
return True
def load_environment_vars(self):
"""Looks for any SANIC_ prefixed environment variables and applies
them to the configuration if present. them to the configuration if present.
""" """
for k, v in os.environ.items():
if k.startswith(SANIC_PREFIX): if k.startswith(SANIC_PREFIX):
_, config_key = k.split(SANIC_PREFIX, 1) _, config_key = k.split(SANIC_PREFIX, 1)
self[config_key] = v self[config_key] = v

View File

@ -16,18 +16,21 @@ def test_load_from_object():
assert app.config.CONFIG_VALUE == 'should be used' assert app.config.CONFIG_VALUE == 'should be used'
assert 'not_for_config' not in app.config assert 'not_for_config' not in app.config
def test_auto_load_env(): def test_auto_load_env():
environ["SANIC_TEST_ANSWER"] = "42" environ["SANIC_TEST_ANSWER"] = "42"
app = Sanic() app = Sanic()
assert app.config.TEST_ANSWER == "42" assert app.config.TEST_ANSWER == "42"
del environ["SANIC_TEST_ANSWER"] del environ["SANIC_TEST_ANSWER"]
def test_auto_load_env(): def test_auto_load_env():
environ["SANIC_TEST_ANSWER"] = "42" environ["SANIC_TEST_ANSWER"] = "42"
app = Sanic(load_env=False) app = Sanic(load_env=False)
assert getattr(app.config, 'TEST_ANSWER', None) == None assert getattr(app.config, 'TEST_ANSWER', None) == None
del environ["SANIC_TEST_ANSWER"] del environ["SANIC_TEST_ANSWER"]
def test_load_from_file(): def test_load_from_file():
app = Sanic('test_load_from_file') app = Sanic('test_load_from_file')
config = b""" config = b"""
@ -74,6 +77,7 @@ def test_load_from_missing_envvar():
def test_overwrite_exisiting_config(): def test_overwrite_exisiting_config():
app = Sanic('test_overwrite_exisiting_config') app = Sanic('test_overwrite_exisiting_config')
app.config.DEFAULT = 1 app.config.DEFAULT = 1
class Config: class Config:
DEFAULT = 2 DEFAULT = 2
@ -85,3 +89,22 @@ def test_missing_config():
app = Sanic('test_missing_config') app = Sanic('test_missing_config')
with pytest.raises(AttributeError): with pytest.raises(AttributeError):
app.config.NON_EXISTENT app.config.NON_EXISTENT
def test_load_from_json():
app = Sanic('test_load_from_file')
config = b'{"VALUE": "some value"}'
with NamedTemporaryFile(mode='wb') as config_file:
config_file.write(config)
config_file.seek(0)
app.config.from_json(config_file.name)
assert 'VALUE' in app.config
assert app.config.VALUE == 'some value'
def test_load_from_mapping():
app = Sanic('test_load_from_file')
config = {"VALUE": "some value"}
app.config.from_mapping(config)
assert 'VALUE' in app.config
assert app.config.VALUE == 'some value'