author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 12 Nov 2009 12:07:59 +0100 | |
branch | stable |
changeset 3823 | 94aa250cff9b |
parent 3642 | 8bc800f8b173 |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
1 |
"""common web configuration for twisted/modpython instances |
0 | 2 |
|
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
2087 | 9 |
_ = unicode |
0 | 10 |
|
11 |
import os |
|
1132 | 12 |
from os.path import join, exists, split |
0 | 13 |
|
14 |
from logilab.common.configuration import Method |
|
3642 | 15 |
from logilab.common.decorators import cached |
0 | 16 |
|
17 |
from cubicweb.toolsutils import read_config |
|
18 |
from cubicweb.cwconfig import CubicWebConfiguration, register_persistent_options, merge_options |
|
19 |
||
20 |
||
21 |
register_persistent_options( ( |
|
22 |
# site-wide only web ui configuration |
|
23 |
('site-title', |
|
24 |
{'type' : 'string', 'default': 'unset title', |
|
25 |
'help': _('site title'), |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
26 |
'sitewide': True, 'group': 'ui', |
0 | 27 |
}), |
28 |
('main-template', |
|
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
447
diff
changeset
|
29 |
{'type' : 'string', 'default': 'main-template', |
0 | 30 |
'help': _('id of main template used to render pages'), |
31 |
'sitewide': True, 'group': 'ui', |
|
32 |
}), |
|
33 |
# user web ui configuration |
|
34 |
('fckeditor', |
|
35 |
{'type' : 'yn', 'default': True, |
|
36 |
'help': _('should html fields being edited using fckeditor (a HTML ' |
|
37 |
'WYSIWYG editor). You should also select text/html as default ' |
|
38 |
'text format to actually get fckeditor.'), |
|
39 |
'group': 'ui', |
|
40 |
}), |
|
41 |
# navigation configuration |
|
42 |
('page-size', |
|
43 |
{'type' : 'int', 'default': 40, |
|
44 |
'help': _('maximum number of objects displayed by page of results'), |
|
45 |
'group': 'navigation', |
|
46 |
}), |
|
47 |
('related-limit', |
|
48 |
{'type' : 'int', 'default': 8, |
|
49 |
'help': _('maximum number of related entities to display in the primary ' |
|
50 |
'view'), |
|
51 |
'group': 'navigation', |
|
52 |
}), |
|
53 |
('combobox-limit', |
|
54 |
{'type' : 'int', 'default': 20, |
|
55 |
'help': _('maximum number of entities to display in related combo box'), |
|
56 |
'group': 'navigation', |
|
57 |
}), |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
58 |
|
0 | 59 |
)) |
60 |
||
61 |
||
62 |
class WebConfiguration(CubicWebConfiguration): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
63 |
"""the WebConfiguration is a singleton object handling instance's |
0 | 64 |
configuration and preferences |
65 |
""" |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2552
diff
changeset
|
66 |
cubicweb_appobject_path = CubicWebConfiguration.cubicweb_appobject_path | set(['web/views']) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2552
diff
changeset
|
67 |
cube_appobject_path = CubicWebConfiguration.cube_appobject_path | set(['views']) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
68 |
|
0 | 69 |
options = merge_options(CubicWebConfiguration.options + ( |
70 |
('anonymous-user', |
|
71 |
{'type' : 'string', |
|
72 |
'default': None, |
|
73 |
'help': 'login of the CubicWeb user account to use for anonymous user (if you want to allow anonymous)', |
|
74 |
'group': 'main', 'inputlevel': 1, |
|
75 |
}), |
|
76 |
('anonymous-password', |
|
77 |
{'type' : 'string', |
|
78 |
'default': None, |
|
79 |
'help': 'password of the CubicWeb user account to use for anonymous user, ' |
|
80 |
'if anonymous-user is set', |
|
81 |
'group': 'main', 'inputlevel': 1, |
|
82 |
}), |
|
83 |
('query-log-file', |
|
84 |
{'type' : 'string', |
|
85 |
'default': None, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
86 |
'help': 'web instance query log file', |
0 | 87 |
'group': 'main', 'inputlevel': 2, |
88 |
}), |
|
89 |
# web configuration |
|
90 |
('https-url', |
|
91 |
{'type' : 'string', |
|
92 |
'default': None, |
|
93 |
'help': 'web server root url on https. By specifying this option your '\ |
|
94 |
'site can be available as an http and https site. Authenticated users '\ |
|
95 |
'will in this case be authenticated and once done navigate through the '\ |
|
96 |
'https site. IMPORTANTE NOTE: to do this work, you should have your '\ |
|
97 |
'apache redirection include "https" as base url path so cubicweb can '\ |
|
98 |
'differentiate between http vs https access. For instance: \n'\ |
|
99 |
'RewriteRule ^/demo/(.*) http://127.0.0.1:8080/https/$1 [L,P]\n'\ |
|
100 |
'where the cubicweb web server is listening on port 8080.', |
|
101 |
'group': 'main', 'inputlevel': 2, |
|
102 |
}), |
|
103 |
('auth-mode', |
|
104 |
{'type' : 'choice', |
|
105 |
'choices' : ('cookie', 'http'), |
|
106 |
'default': 'cookie', |
|
107 |
'help': 'authentication mode (cookie / http)', |
|
108 |
'group': 'web', 'inputlevel': 1, |
|
109 |
}), |
|
110 |
('realm', |
|
111 |
{'type' : 'string', |
|
112 |
'default': 'cubicweb', |
|
113 |
'help': 'realm to use on HTTP authentication mode', |
|
114 |
'group': 'web', 'inputlevel': 2, |
|
115 |
}), |
|
116 |
('http-session-time', |
|
117 |
{'type' : 'int', |
|
118 |
'default': 0, |
|
119 |
'help': 'duration in seconds for HTTP sessions. 0 mean no expiration. '\ |
|
120 |
'Should be greater than RQL server\'s session-time.', |
|
121 |
'group': 'web', 'inputlevel': 2, |
|
122 |
}), |
|
123 |
('cleanup-session-time', |
|
124 |
{'type' : 'int', |
|
125 |
'default': 43200, |
|
126 |
'help': 'duration in seconds for which unused connections should be '\ |
|
127 |
'closed, to limit memory consumption. This is different from '\ |
|
128 |
'http-session-time since in some cases you may have an unexpired http '\ |
|
129 |
'session (e.g. valid session cookie) which will trigger transparent '\ |
|
130 |
'creation of a new session. In other cases, sessions may never expire \ |
|
131 |
and cause memory leak. Should be smaller than http-session-time, '\ |
|
132 |
'unless it\'s 0. Default to 12 h.', |
|
133 |
'group': 'web', 'inputlevel': 2, |
|
134 |
}), |
|
135 |
('cleanup-anonymous-session-time', |
|
136 |
{'type' : 'int', |
|
137 |
'default': 120, |
|
138 |
'help': 'Same as cleanup-session-time but specific to anonymous '\ |
|
139 |
'sessions. Default to 2 min.', |
|
140 |
'group': 'web', 'inputlevel': 2, |
|
141 |
}), |
|
2552
1ea2f2ff5dca
add force-html-content-type config boolean variable
Florent <florent@secondweb.fr>
parents:
2476
diff
changeset
|
142 |
('force-html-content-type', |
1ea2f2ff5dca
add force-html-content-type config boolean variable
Florent <florent@secondweb.fr>
parents:
2476
diff
changeset
|
143 |
{'type' : 'yn', |
1ea2f2ff5dca
add force-html-content-type config boolean variable
Florent <florent@secondweb.fr>
parents:
2476
diff
changeset
|
144 |
'default': False, |
1ea2f2ff5dca
add force-html-content-type config boolean variable
Florent <florent@secondweb.fr>
parents:
2476
diff
changeset
|
145 |
'help': 'force text/html content type for your html pages instead of cubicweb user-agent based'\ |
1ea2f2ff5dca
add force-html-content-type config boolean variable
Florent <florent@secondweb.fr>
parents:
2476
diff
changeset
|
146 |
'deduction of an appropriate content type', |
1ea2f2ff5dca
add force-html-content-type config boolean variable
Florent <florent@secondweb.fr>
parents:
2476
diff
changeset
|
147 |
'group': 'web', 'inputlevel': 2, |
1ea2f2ff5dca
add force-html-content-type config boolean variable
Florent <florent@secondweb.fr>
parents:
2476
diff
changeset
|
148 |
}), |
0 | 149 |
('embed-allowed', |
150 |
{'type' : 'regexp', |
|
151 |
'default': None, |
|
152 |
'help': 'regular expression matching URLs that may be embeded. \ |
|
153 |
leave it blank if you don\'t want the embedding feature, or set it to ".*" \ |
|
154 |
if you want to allow everything', |
|
155 |
'group': 'web', 'inputlevel': 1, |
|
156 |
}), |
|
157 |
('submit-mail', |
|
158 |
{'type' : 'string', |
|
159 |
'default': None, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
160 |
'help': ('Mail used as recipient to report bug in this instance, ' |
0 | 161 |
'if you want this feature on'), |
162 |
'group': 'web', 'inputlevel': 2, |
|
163 |
}), |
|
164 |
||
165 |
('language-negociation', |
|
166 |
{'type' : 'yn', |
|
167 |
'default': True, |
|
168 |
'help': 'use Accept-Language http header to try to set user '\ |
|
169 |
'interface\'s language according to browser defined preferences', |
|
170 |
'group': 'web', 'inputlevel': 2, |
|
171 |
}), |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
172 |
|
0 | 173 |
('print-traceback', |
174 |
{'type' : 'yn', |
|
3638
648d6dbec630
system/user modes + CWDEV instead of installed/dev mixed modes. Fix behaviour when setting CW_MODE explicitly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3539
diff
changeset
|
175 |
'default': CubicWebConfiguration.mode != 'system', |
0 | 176 |
'help': 'print the traceback on the error page when an error occured', |
177 |
'group': 'web', 'inputlevel': 2, |
|
178 |
}), |
|
179 |
)) |
|
180 |
||
890
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
181 |
def fckeditor_installed(self): |
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
182 |
return exists(self.ext_resources['FCKEDITOR_PATH']) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
183 |
|
890
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
184 |
def eproperty_definitions(self): |
893 | 185 |
for key, pdef in super(WebConfiguration, self).eproperty_definitions(): |
890
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
186 |
if key == 'ui.fckeditor' and not self.fckeditor_installed(): |
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
187 |
continue |
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
188 |
yield key, pdef |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
189 |
|
0 | 190 |
# method used to connect to the repository: 'inmemory' / 'pyro' |
191 |
# Pyro repository by default |
|
192 |
repo_method = 'pyro' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
193 |
|
0 | 194 |
# don't use @cached: we want to be able to disable it while this must still |
195 |
# be cached |
|
196 |
def repository(self, vreg=None): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
197 |
"""return the instance's repository object""" |
0 | 198 |
try: |
199 |
return self.__repo |
|
200 |
except AttributeError: |
|
201 |
from cubicweb.dbapi import get_repository |
|
202 |
if self.repo_method == 'inmemory': |
|
203 |
repo = get_repository('inmemory', vreg=vreg, config=self) |
|
204 |
else: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
205 |
repo = get_repository('pyro', self['pyro-instance-id'], |
0 | 206 |
config=self) |
207 |
self.__repo = repo |
|
208 |
return repo |
|
209 |
||
210 |
def vc_config(self): |
|
211 |
return self.repository().get_versions() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
212 |
|
0 | 213 |
# mapping to external resources (id -> path) (`external_resources` file) ## |
214 |
ext_resources = { |
|
215 |
'FAVICON': 'DATADIR/favicon.ico', |
|
216 |
'LOGO': 'DATADIR/logo.png', |
|
217 |
'RSS_LOGO': 'DATADIR/rss.png', |
|
218 |
'HELP': 'DATADIR/help.png', |
|
219 |
'CALENDAR_ICON': 'DATADIR/calendar.gif', |
|
220 |
'SEARCH_GO':'DATADIR/go.png', |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
221 |
|
0 | 222 |
'FCKEDITOR_PATH': '/usr/share/fckeditor/', |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
223 |
|
0 | 224 |
'IE_STYLESHEETS': ['DATADIR/cubicweb.ie.css'], |
225 |
'STYLESHEETS': ['DATADIR/cubicweb.css'], |
|
226 |
'STYLESHEETS_PRINT': ['DATADIR/cubicweb.print.css'], |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
227 |
|
0 | 228 |
'JAVASCRIPTS': ['DATADIR/jquery.js', |
30
25ef1dddaab8
round corners are back
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
229 |
'DATADIR/jquery.corner.js', |
25ef1dddaab8
round corners are back
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
230 |
'DATADIR/jquery.json.js', |
0 | 231 |
'DATADIR/cubicweb.compat.js', |
232 |
'DATADIR/cubicweb.python.js', |
|
233 |
'DATADIR/cubicweb.htmlhelpers.js'], |
|
234 |
} |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
235 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
236 |
|
0 | 237 |
def anonymous_user(self): |
238 |
"""return a login and password to use for anonymous users. None |
|
239 |
may be returned for both if anonymous connections are not allowed |
|
240 |
""" |
|
241 |
try: |
|
242 |
user = self['anonymous-user'] |
|
243 |
passwd = self['anonymous-password'] |
|
244 |
except KeyError: |
|
245 |
user, passwd = None, None |
|
246 |
if user is not None: |
|
247 |
user = unicode(user) |
|
248 |
return user, passwd |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
249 |
|
0 | 250 |
def has_resource(self, rid): |
251 |
"""return true if an external resource is defined""" |
|
252 |
return bool(self.ext_resources.get(rid)) |
|
253 |
||
254 |
@cached |
|
255 |
def locate_resource(self, rid): |
|
256 |
"""return the directory where the given resource may be found""" |
|
257 |
return self._fs_locate(rid, 'data') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
258 |
|
0 | 259 |
@cached |
260 |
def locate_doc_file(self, fname): |
|
261 |
"""return the directory where the given resource may be found""" |
|
262 |
return self._fs_locate(fname, 'wdoc') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
263 |
|
0 | 264 |
def _fs_locate(self, rid, rdirectory): |
265 |
"""return the directory where the given resource may be found""" |
|
266 |
path = [self.apphome] + self.cubes_path() + [join(self.shared_dir())] |
|
267 |
for directory in path: |
|
268 |
if exists(join(directory, rdirectory, rid)): |
|
269 |
return join(directory, rdirectory) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
270 |
|
0 | 271 |
def locate_all_files(self, rid, rdirectory='wdoc'): |
272 |
"""return all files corresponding to the given resource""" |
|
273 |
path = [self.apphome] + self.cubes_path() + [join(self.shared_dir())] |
|
274 |
for directory in path: |
|
275 |
fpath = join(directory, rdirectory, rid) |
|
276 |
if exists(fpath): |
|
277 |
yield join(fpath) |
|
278 |
||
279 |
def load_configuration(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
280 |
"""load instance's configuration files""" |
0 | 281 |
super(WebConfiguration, self).load_configuration() |
282 |
# load external resources definition |
|
283 |
self._build_ext_resources() |
|
284 |
self._init_base_url() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
285 |
|
0 | 286 |
def _init_base_url(self): |
287 |
# normalize base url(s) |
|
288 |
baseurl = self['base-url'] |
|
289 |
if baseurl and baseurl[-1] != '/': |
|
290 |
baseurl += '/' |
|
291 |
self.global_set_option('base-url', baseurl) |
|
292 |
httpsurl = self['https-url'] |
|
293 |
if httpsurl and httpsurl[-1] != '/': |
|
294 |
httpsurl += '/' |
|
295 |
self.global_set_option('https-url', httpsurl) |
|
296 |
||
297 |
def _build_ext_resources(self): |
|
298 |
libresourcesfile = join(self.shared_dir(), 'data', 'external_resources') |
|
299 |
self.ext_resources.update(read_config(libresourcesfile)) |
|
300 |
for path in reversed([self.apphome] + self.cubes_path()): |
|
301 |
resourcesfile = join(path, 'data', 'external_resources') |
|
302 |
if exists(resourcesfile): |
|
303 |
self.debug('loading %s', resourcesfile) |
|
304 |
self.ext_resources.update(read_config(resourcesfile)) |
|
252
8cd0c2111783
search external_resources in application home
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
151
diff
changeset
|
305 |
resourcesfile = join(self.apphome, 'external_resources') |
8cd0c2111783
search external_resources in application home
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
151
diff
changeset
|
306 |
if exists(resourcesfile): |
8cd0c2111783
search external_resources in application home
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
151
diff
changeset
|
307 |
self.debug('loading %s', resourcesfile) |
8cd0c2111783
search external_resources in application home
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
151
diff
changeset
|
308 |
self.ext_resources.update(read_config(resourcesfile)) |
0 | 309 |
for resource in ('STYLESHEETS', 'STYLESHEETS_PRINT', |
310 |
'IE_STYLESHEETS', 'JAVASCRIPTS'): |
|
311 |
val = self.ext_resources[resource] |
|
312 |
if isinstance(val, str): |
|
313 |
files = [w.strip() for w in val.split(',') if w.strip()] |
|
314 |
self.ext_resources[resource] = files |
|
151
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
315 |
|
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
316 |
|
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
317 |
# static files handling ################################################### |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
318 |
|
151
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
319 |
@property |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
320 |
def static_directory(self): |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
321 |
return join(self.appdatahome, 'static') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
322 |
|
151
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
323 |
def static_file_exists(self, rpath): |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
324 |
return exists(join(self.static_directory, rpath)) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
325 |
|
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
326 |
def static_file_open(self, rpath, mode='wb'): |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
327 |
staticdir = self.static_directory |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
328 |
rdir, filename = split(rpath) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
329 |
if rdir: |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
330 |
staticdir = join(staticdir, rdir) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
331 |
os.makedirs(staticdir) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
332 |
return file(join(staticdir, filename), mode) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
333 |
|
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
334 |
def static_file_add(self, rpath, data): |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
335 |
stream = self.static_file_open(rpath) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
336 |
stream.write(data) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
337 |
stream.close() |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
338 |
|
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
339 |
def static_file_del(self, rpath): |
447 | 340 |
if self.static_file_exists(rpath): |
151
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
341 |
os.remove(join(self.static_directory, rpath)) |