From 1f1aec3c00d904e220f5ce31bf11211973c8c588 Mon Sep 17 00:00:00 2001
From: IvanLuLyf <835498692@qq.com>
Date: Sun, 15 Mar 2020 13:18:44 +0800
Subject: [PATCH] add error page
---
MANIFEST.in | 1 +
asset/error.html | 61 ++++++++++++++++++++++++++++++++++++++++++++++++
bunnypy.py | 22 +++++++++++------
setup.py | 1 +
4 files changed, 78 insertions(+), 7 deletions(-)
create mode 100644 MANIFEST.in
create mode 100644 asset/error.html
diff --git a/MANIFEST.in b/MANIFEST.in
new file mode 100644
index 0000000..96fa23c
--- /dev/null
+++ b/MANIFEST.in
@@ -0,0 +1 @@
+include asset/*.html
\ No newline at end of file
diff --git a/asset/error.html b/asset/error.html
new file mode 100644
index 0000000..85916af
--- /dev/null
+++ b/asset/error.html
@@ -0,0 +1,61 @@
+
+
+
+
+ BunnyPy Error
+
+
+
+
+
+BunnyPy Error
+{{ bunny_error }}
+Powered By BunnyPy
+
+
\ No newline at end of file
diff --git a/bunnypy.py b/bunnypy.py
index 70746f3..2cb08dd 100644
--- a/bunnypy.py
+++ b/bunnypy.py
@@ -1,10 +1,11 @@
+import os
import re
from pathlib import Path
from html import escape
from urllib.parse import parse_qs, unquote
from wsgiref.simple_server import make_server
-__version__ = "0.2.1"
+__version__ = "0.2.2"
__default_html__ = '''Welcome to BunnyPy
@@ -28,6 +29,8 @@
'''
+__asset_dir__ = os.path.dirname(os.path.abspath(__file__)) + '/asset/'
+
class Bunny:
__sub_apps__ = {}
@@ -41,7 +44,7 @@ def handler(self, environ, start_response):
with file_path.open("rb") as static_file:
data = static_file.read()
except FileNotFoundError:
- data = b'404 Not Found
'
+ data = self.__render_error('404 Not Found');
start_response('200 OK', [('Content-Type', 'text/html;charset=utf-8')])
return [data]
url_array = environ['PATH_INFO'].split('/')
@@ -97,9 +100,9 @@ def __call_action__(self, req, name, action):
if ac_other is not None:
return self.__call_func__(ac_other, req)
else:
- return 'Action {0} Not Exists'.format(action)
+ return self.__render_error('Action {0} Not Exists'.format(action))
else:
- return 'Mod {0} Not Exists'.format(name)
+ return self.__render_error('Mod {0} Not Exists'.format(name))
def __call_func__(self, func, req):
try:
@@ -117,11 +120,14 @@ def __call_func__(self, func, req):
args.append(req[vn])
return func(*args)
except Exception as e:
- return 'BunnyPy Error
{0}
'.format(str(e))
+ return self.__render_error(str(e))
def render(self, view, context=None):
return self.TemplateRender(view, context).render()
+ def __render_error(self, error_str):
+ return self.TemplateRender('error.html', {'bunny_error': error_str}, __asset_dir__).render()
+
class Request:
def __init__(self, environ):
query_string = environ['QUERY_STRING']
@@ -401,9 +407,11 @@ def __str__(self):
return '\n'.join(map(str, self.lines))
class TemplateRender:
- def __init__(self, view, context=None):
+ def __init__(self, view, context=None, base_path=None):
+ if base_path is None:
+ base_path = "template/"
try:
- with open("template/" + view, "r", encoding='utf-8') as template_file:
+ with open(base_path + view, "r", encoding='utf-8') as template_file:
self.raw_text = template_file.read()
except FileNotFoundError:
self.raw_text = 'Template' + view + ' Not Found
'
diff --git a/setup.py b/setup.py
index e93cab9..6879101 100644
--- a/setup.py
+++ b/setup.py
@@ -16,6 +16,7 @@
url='https://github.com/ivanlulyf/bunnypy',
py_modules=['bunnypy'],
scripts=['bunnypy.py'],
+ include_package_data=True,
license='MIT',
platforms='any',
classifiers=['Operating System :: OS Independent',