remove dependence and implmented import_string

This commit is contained in:
Jotagê Sales 2018-12-26 21:19:54 -02:00
parent 19b304b0fc
commit a33ebbaf11
5 changed files with 25 additions and 4 deletions

View File

@ -1,10 +1,8 @@
import os import os
import types import types
import import_string
from sanic.exceptions import PyFileError from sanic.exceptions import PyFileError
from sanic.helpers import import_string
SANIC_PREFIX = "SANIC_" SANIC_PREFIX = "SANIC_"

View File

@ -1,4 +1,5 @@
"""Defines basics of HTTP standard.""" """Defines basics of HTTP standard."""
from importlib import import_module
STATUS_CODES = { STATUS_CODES = {
100: b"Continue", 100: b"Continue",
@ -131,3 +132,9 @@ def remove_entity_headers(headers, allowed=("content-location", "expires")):
if not is_entity_header(header) or header.lower() in allowed if not is_entity_header(header) or header.lower() in allowed
} }
return headers return headers
def import_string(module_name):
module, obj = module_name.rsplit('.', 1)
module = import_module(module)
return getattr(module, obj)()

View File

@ -63,7 +63,6 @@ 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")

View File

@ -35,6 +35,11 @@ def test_load_from_object_string(app):
assert 'not_for_config' not in app.config assert 'not_for_config' not in app.config
def test_load_from_object_string_exception(app):
with pytest.raises(ImportError):
app.config.from_object('test_config.Config.test')
def test_auto_load_env(): def test_auto_load_env():
environ["SANIC_TEST_ANSWER"] = "42" environ["SANIC_TEST_ANSWER"] = "42"
app = Sanic() app = Sanic()

View File

@ -1,4 +1,5 @@
from sanic import helpers from sanic import helpers
import pytest
def test_has_message_body(): def test_has_message_body():
@ -72,3 +73,14 @@ def test_remove_entity_headers():
for header, expected in tests: for header, expected in tests:
assert helpers.remove_entity_headers(header) == expected assert helpers.remove_entity_headers(header) == expected
def test_import_string():
from sanic.config import Config
obj = helpers.import_string('sanic.config.Config')
assert isinstance(obj, Config)
def test_import_string_exception():
with pytest.raises(ImportError):
helpers.import_string('test.test.test')