make python-magic optional

This commit is contained in:
Izalia Mae 2021-10-30 19:52:56 -04:00
parent 9be0387eed
commit 7023f4fa40
3 changed files with 33 additions and 3 deletions

View file

@ -22,6 +22,8 @@ frontend = Path(__file__).resolve().parent.parent.join('http_frontend')
class Application:
ctx = DotDict()
def __init__(self, loop=None, views=[], middleware=[], **kwargs):
if loop:
self.loop = loop
@ -69,6 +71,14 @@ class Application:
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'):
return self.router(path, method.upper())

View file

@ -2,7 +2,7 @@ import json, traceback
from datetime import datetime
from .misc import Cookies, Headers
from .misc import Cookies, Headers, CookieItem
from ..dotdict import MultiDotDict
@ -85,6 +85,14 @@ class 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):
self.body = body
@ -151,6 +159,10 @@ class Response:
return self
def set_cookie(self, key, value, **kwargs):
self.cookies[key] = CookieItem(key, value, **kwargs)
def compile(self):
data = bytes(f'HTTP/1.1 {self.status}', 'utf-8')

View file

@ -1,4 +1,4 @@
import magic, mimetypes
import mimetypes
from . import http_methods, error
@ -10,6 +10,9 @@ from ..exceptions import (
MethodNotHandledException
)
try: import magic
except ImportError: magic = None
default_theme = DotDict(
primary = Color('#e7a'),
@ -56,7 +59,12 @@ def Static(src):
with open(src_path, 'rb') as fd:
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:
raise error.NotFound('Static file not found')