forked from cream/melange
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpserver.py
89 lines (71 loc) · 3.1 KB
/
httpserver.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import os
import urlparse
import bjoern
from bjoern import run
from cream.util import cached_property
class MelangeResponse(bjoern.Response):
response_headers = ()
response_status = '200 Alles super'
@cached_property
def GET(self):
return dict((header_name, header_values[0]) for header_name, header_values in
urlparse.parse_qs(self.environ['QUERY_STRING']).iteritems())
def route(url_regex):
def decorator(func):
if not hasattr(func, '__bjoern_routes__'):
func.__bjoern_routes__ = list()
func.__bjoern_routes__.append(url_regex)
return func
return decorator
class HttpServer(object):
def __init__(self, melange):
self._melange = melange
self._setup_routes()
def _setup_routes(self):
for name, attr in ((name, getattr(self, name)) for name in dir(self)):
if hasattr(attr, '__bjoern_routes__'):
for route in attr.__bjoern_routes__:
bjoern.route(route)(attr)
def run(self, host, port):
bjoern.run(host, port, MelangeResponse)
def _get_widget_theme(self, request):
widget_id = request.GET.get('instance')
if widget_id:
return self._melange.widgets[widget_id].get_current_theme()
else:
return self._melange.themes.get_by_id(self._melange.config.default_theme)
@route(r'/thingy/(?P<file>.*)')
def thingy_files(self, env, request, file):
path = os.path.join(self._melange.context.working_directory, 'data/thingy')
return open(os.path.join(path, file))
@route(r'/widget/(?P<file>.*)')
def widget_files(self, env, request, file):
return open(os.path.join(self._melange.widgets[request.GET['instance']].get_skin_path(), file))
@route(r'/common/(?P<file>.*)')
def common_files(self, env, request, file):
widget_theme = self._get_widget_theme(request)
return open(os.path.join(widget_theme['path'], file))
@route(r'/widget/tmp/(?P<file>.*)')
def tmp_files(self, env, request, file):
path = self._melange.widgets[request.GET['instance']].get_tmp()
return open(os.path.join(path, file))
@route(r'/chrome/(?P<file>.*)')
def chrome_files(self, env, request, file):
widget_theme = self._get_widget_theme(request)
return open(os.path.join(widget_theme['path'], 'chrome', file))