1
|
from Defaults import *
|
2
|
|
3
|
import sys
|
4
|
|
5
|
import lasso
|
6
|
if not hasattr(lasso, 'SAML2_SUPPORT'):
|
7
|
lasso.SAML2_SUPPORT = False
|
8
|
|
9
|
try:
|
10
|
from larpe_cfg import *
|
11
|
except ImportError:
|
12
|
pass
|
13
|
|
14
|
import os
|
15
|
import time
|
16
|
import lasso
|
17
|
import imp
|
18
|
import __builtin__
|
19
|
__builtin__.__dict__['_'] = lambda x: x
|
20
|
|
21
|
import gettext, locale
|
22
|
|
23
|
translations = {
|
24
|
'en': gettext.NullTranslations(),
|
25
|
}
|
26
|
|
27
|
for lang in ['es', 'fr']:
|
28
|
try:
|
29
|
translations[lang] = gettext.translation('larpe', languages=[lang])
|
30
|
except IOError:
|
31
|
pass
|
32
|
__builtin__.__dict__['N_'] = lambda x: x
|
33
|
|
34
|
from quixote import enable_ptl
|
35
|
from quixote.publish import Publisher, get_response
|
36
|
enable_ptl()
|
37
|
|
38
|
import misc
|
39
|
|
40
|
import errors
|
41
|
import logger
|
42
|
import sessions
|
43
|
|
44
|
import roles
|
45
|
import users
|
46
|
|
47
|
import template
|
48
|
from root import RootDirectory
|
49
|
|
50
|
|
51
|
extra_dirs = []
|
52
|
def load_extra_items():
|
53
|
global extra_dirs
|
54
|
|
55
|
extra_dir = os.path.join(os.path.dirname(__file__), 'extra')
|
56
|
if len(extra_dirs) == 0:
|
57
|
extra_dirs = [extra_dir]
|
58
|
|
59
|
for extra_dir in extra_dirs:
|
60
|
if not os.path.exists(extra_dir):
|
61
|
continue
|
62
|
sys.path.append(extra_dir)
|
63
|
for filename in os.listdir(extra_dir):
|
64
|
if not filename.endswith('.py'):
|
65
|
continue
|
66
|
modulename = filename[:-3]
|
67
|
fp, pathname, description = imp.find_module(modulename, [extra_dir])
|
68
|
try:
|
69
|
imp.load_module(modulename, fp, pathname, description)
|
70
|
except:
|
71
|
raise
|
72
|
logger.warn('failed to load extra module: %s' % modulename)
|
73
|
if fp:
|
74
|
fp.close()
|
75
|
|
76
|
|
77
|
class LarpePublisher(Publisher):
|
78
|
def format_publish_error(self, exc):
|
79
|
get_response().filter = {}
|
80
|
if isinstance(exc, errors.AccessError) and hasattr(exc, 'render'):
|
81
|
return exc.render()
|
82
|
return errors.format_publish_error(exc)
|
83
|
|
84
|
def finish_failed_request(self):
|
85
|
result = Publisher.finish_failed_request(self)
|
86
|
try:
|
87
|
logger.error('internal server error')
|
88
|
except:
|
89
|
pass
|
90
|
if not self.config.display_exceptions == 'text-in-html':
|
91
|
return result
|
92
|
# result is here the text output
|
93
|
get_response().set_header("Content-Type", "text/html") # set back content-type
|
94
|
(exc_type, exc_value, tb) = sys.exc_info()
|
95
|
if exc_type is NotImplementedError:
|
96
|
return template.error_page(
|
97
|
_('This feature is not yet implemented.'),
|
98
|
error_title = _('Internal Server Error'),
|
99
|
exception = result)
|
100
|
else:
|
101
|
return template.error_page(
|
102
|
_('The server encountered an internal error and was unable to complete your request.'),
|
103
|
error_title = _('Internal Server Error'),
|
104
|
exception = result)
|
105
|
|
106
|
def filter_output(self, request, output):
|
107
|
response = get_response()
|
108
|
if response.content_type != 'text/html':
|
109
|
return output
|
110
|
if not hasattr(response, 'filter'):
|
111
|
return output
|
112
|
return template.decorate(output, response)
|
113
|
|
114
|
def install_lang(self, lang = None):
|
115
|
if lang is None:
|
116
|
gettext.install('larpe') # will use environment variable to get language
|
117
|
else:
|
118
|
translations[lang].install()
|
119
|
_1 = _
|
120
|
__builtin__.__dict__['_'] = lambda x: unicode(_1(str(x)), 'utf-8').encode('iso-8859-1')
|
121
|
__builtin__.__dict__['_1'] = _1
|
122
|
|
123
|
def set_config(self):
|
124
|
misc.reload_cfg()
|
125
|
debug_cfg = misc.cfg.get('debug', {})
|
126
|
self.logger.error_email = debug_cfg.get('error_email')
|
127
|
self.config.display_exceptions = debug_cfg.get('display_exceptions')
|
128
|
self.config.form_tokens = True
|
129
|
if debug_cfg.get('logger', False):
|
130
|
logger.enable()
|
131
|
else:
|
132
|
logger.disable()
|
133
|
|
134
|
def try_publish(self, request):
|
135
|
lang = misc.cfg.get('language', {}).get('language', None)
|
136
|
if lang == 'HTTP':
|
137
|
lang = None
|
138
|
accepted_languages = request.get_header('Accept-Language')
|
139
|
if accepted_languages:
|
140
|
accepted_languages = [x.strip() for x in accepted_languages.split(',')]
|
141
|
# forget about subtag and quality value
|
142
|
accepted_languages = [x.split('-')[0] for x in accepted_languages]
|
143
|
for l in accepted_languages:
|
144
|
if l in translations.keys():
|
145
|
lang = l
|
146
|
break
|
147
|
self.install_lang(lang)
|
148
|
return Publisher.try_publish(self, request)
|
149
|
|
150
|
|
151
|
class LarpeVHostPublisher(LarpePublisher):
|
152
|
def try_publish(self, request):
|
153
|
self.app_dir = os.path.join(APP_DIR, request.get_server().split(':')[0])
|
154
|
if not os.path.exists(self.app_dir):
|
155
|
os.mkdir(self.app_dir)
|
156
|
self.set_config()
|
157
|
return LarpePublisher.try_publish(self, request)
|
158
|
|
159
|
|
160
|
def create_publisher(publisher_class = LarpePublisher):
|
161
|
load_extra_items()
|
162
|
publisher = publisher_class(RootDirectory(),
|
163
|
session_cookie_name = 'larpe',
|
164
|
session_cookie_path = '/',
|
165
|
error_log = ERROR_LOG)
|
166
|
publisher.app_dir = APP_DIR
|
167
|
publisher.data_dir = DATA_DIR
|
168
|
if not os.path.exists(publisher.app_dir):
|
169
|
os.mkdir(publisher.app_dir)
|
170
|
publisher.set_session_manager(sessions.StorageSessionManager())
|
171
|
publisher.set_config()
|
172
|
return publisher
|
173
|
|
174
|
def create_vhost_publisher():
|
175
|
return create_publisher(LarpeVHostPublisher)
|
176
|
|