configure app from object by path string

This commit is contained in:
Jotagê Sales 2018-12-26 16:23:16 -02:00
parent 4efd450b32
commit e978121d58
2 changed files with 6 additions and 1 deletions

View File

@ -1,6 +1,8 @@
import os import os
import types import types
import import_string
from sanic.exceptions import PyFileError from sanic.exceptions import PyFileError
@ -111,6 +113,8 @@ class Config(dict):
:param obj: an object holding the configuration :param obj: an object holding the configuration
""" """
if isinstance(obj, str):
obj = import_string(obj)
for key in dir(obj): for key in dir(obj):
if key.isupper(): if key.isupper():
self[key] = getattr(obj, key) self[key] = getattr(obj, key)

View File

@ -4,7 +4,7 @@ Sanic
import codecs import codecs
import os import os
import re import re
from distutils.errors import DistutilsPlatformError
from distutils.util import strtobool from distutils.util import strtobool
from setuptools import setup from setuptools import setup
@ -63,6 +63,7 @@ requirements = [
'aiofiles>=0.3.0', 'aiofiles>=0.3.0',
'websockets>=6.0,<7.0', 'websockets>=6.0,<7.0',
'multidict>=4.0,<5.0', 'multidict>=4.0,<5.0',
'import-string>=0.1.0'
] ]
if strtobool(os.environ.get("SANIC_NO_UJSON", "no")): if strtobool(os.environ.get("SANIC_NO_UJSON", "no")):
print("Installing without uJSON") print("Installing without uJSON")