diff --git a/sanic/config.py b/sanic/config.py index 719528e4..d9eba5ab 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -4,6 +4,7 @@ import types from sanic.exceptions import PyFileError from sanic.helpers import import_string + SANIC_PREFIX = "SANIC_" @@ -104,6 +105,9 @@ class Config(dict): from yourapplication import default_config app.config.from_object(default_config) + or also: + app.config.from_object('myproject.config.MyConfigClass') + You should not use this function to load the actual configuration but rather configuration defaults. The actual config should be loaded with :meth:`from_pyfile` and ideally from a location not within the diff --git a/sanic/helpers.py b/sanic/helpers.py index d3e54529..75c19152 100644 --- a/sanic/helpers.py +++ b/sanic/helpers.py @@ -1,6 +1,7 @@ """Defines basics of HTTP standard.""" from importlib import import_module + STATUS_CODES = { 100: b"Continue", 101: b"Switching Protocols", @@ -135,6 +136,17 @@ def remove_entity_headers(headers, allowed=("content-location", "expires")): def import_string(module_name): - module, obj = module_name.rsplit('.', 1) + """ + import a module or class by string path. + + :module_name: str with path of module or path to import and + instanciate a class + :returns: a module object or one instance from class if + module_name is a valid path to class + + """ + module, klass = module_name.rsplit(".", 1) module = import_module(module) - return getattr(module, obj)() + if hasattr(module, klass): + return getattr(module, klass)() + return module