make python-magic optional
This commit is contained in:
parent
9be0387eed
commit
7023f4fa40
|
@ -22,6 +22,8 @@ frontend = Path(__file__).resolve().parent.parent.join('http_frontend')
|
||||||
|
|
||||||
|
|
||||||
class Application:
|
class Application:
|
||||||
|
ctx = DotDict()
|
||||||
|
|
||||||
def __init__(self, loop=None, views=[], middleware=[], **kwargs):
|
def __init__(self, loop=None, views=[], middleware=[], **kwargs):
|
||||||
if loop:
|
if loop:
|
||||||
self.loop = loop
|
self.loop = loop
|
||||||
|
@ -69,6 +71,14 @@ class Application:
|
||||||
self.template = None
|
self.template = None
|
||||||
|
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return self.ctx[key]
|
||||||
|
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
self.ctx[key] = value
|
||||||
|
|
||||||
|
|
||||||
def get_route(self, path, method='GET'):
|
def get_route(self, path, method='GET'):
|
||||||
return self.router(path, method.upper())
|
return self.router(path, method.upper())
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import json, traceback
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from .misc import Cookies, Headers
|
from .misc import Cookies, Headers, CookieItem
|
||||||
|
|
||||||
from ..dotdict import MultiDotDict
|
from ..dotdict import MultiDotDict
|
||||||
|
|
||||||
|
@ -85,6 +85,14 @@ class Response:
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def new_redir(cls, path, status=302, **kwargs):
|
||||||
|
response = cls(**kwargs)
|
||||||
|
response.set_redir(path, status)
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
def set_text(self, body=b'', status=None):
|
def set_text(self, body=b'', status=None):
|
||||||
self.body = body
|
self.body = body
|
||||||
|
|
||||||
|
@ -151,6 +159,10 @@ class Response:
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
def set_cookie(self, key, value, **kwargs):
|
||||||
|
self.cookies[key] = CookieItem(key, value, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def compile(self):
|
def compile(self):
|
||||||
data = bytes(f'HTTP/1.1 {self.status}', 'utf-8')
|
data = bytes(f'HTTP/1.1 {self.status}', 'utf-8')
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import magic, mimetypes
|
import mimetypes
|
||||||
|
|
||||||
from . import http_methods, error
|
from . import http_methods, error
|
||||||
|
|
||||||
|
@ -10,6 +10,9 @@ from ..exceptions import (
|
||||||
MethodNotHandledException
|
MethodNotHandledException
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try: import magic
|
||||||
|
except ImportError: magic = None
|
||||||
|
|
||||||
|
|
||||||
default_theme = DotDict(
|
default_theme = DotDict(
|
||||||
primary = Color('#e7a'),
|
primary = Color('#e7a'),
|
||||||
|
@ -56,7 +59,12 @@ def Static(src):
|
||||||
|
|
||||||
with open(src_path, 'rb') as fd:
|
with open(src_path, 'rb') as fd:
|
||||||
data = fd.read()
|
data = fd.read()
|
||||||
mime = magic.from_buffer(data[:2048], mime=True) or mimetypes.guess_type(path)
|
|
||||||
|
if magic:
|
||||||
|
mime = magic.from_buffer(data[:2048], mime=True) or mimetypes.guess_type(path)
|
||||||
|
|
||||||
|
else:
|
||||||
|
mime = mimetypes.guess_type(path)
|
||||||
|
|
||||||
except (FileNotFoundError, IsADirectoryError) as e:
|
except (FileNotFoundError, IsADirectoryError) as e:
|
||||||
raise error.NotFound('Static file not found')
|
raise error.NotFound('Static file not found')
|
||||||
|
|
Loading…
Reference in a new issue