-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
58 lines (41 loc) · 1.51 KB
/
gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
import logging
from moneta.http.http import HTTPReply
import re
from os.path import realpath
logger = logging.getLogger('moneta.plugins.gui')
def getDependencies():
""" Return modules that need to be injected to the plugin constructor """
return ['PluginRegistry', 'Server']
def init(config, registry, server):
""" Instanciate the plugin """
return GUIPlugin(config, registry, server)
class GUIPlugin(object):
""" GUI Plugin """
def __init__(self, config, registry, server):
""" Constructor """
self.config = config
self.registry = registry
self.server = server
self.server.register_route('/gui(/.*)?', self.handleGUI, {'GET'})
def handleGUI(self, request):
"""Handle requests to /gui"""
match = re.match(r'/gui(/.*)?', request.uri_path)
uri = match.group(1)
if not uri:
return HTTPReply(code = 301, headers = {'Location': '/gui/'})
if uri[-1:] == '/':
uri = uri + 'index.html'
docroot = realpath(self.config['docroot'])
path = realpath(docroot + '/' + uri)
if not path.startswith(docroot):
return HTTPReply(code = 500, message='Requested file outside of docroot')
try:
fh = open(path, 'rb')
body = fh.read()
return HTTPReply(body = body)
except IOError as e:
if e.errno == 2:
return HTTPReply(code = 404)
else:
raise