author | Alain Leufroy <alain.leufroy@logilab.fr> |
Thu, 04 Mar 2010 08:50:27 +0100 | |
branch | stable |
changeset 4781 | d12cbd0b7574 |
parent 4693 | 1eaf5ce04844 |
child 4830 | 10e8bc190695 |
child 4833 | 41a78fb4107c |
child 4859 | f65208c9dbdc |
permissions | -rw-r--r-- |
4511 | 1 |
"""Some utilities for CubicWeb server/clients. |
0 | 2 |
|
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4000
diff
changeset
|
4 |
:copyright: 2001-2010 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:
1751
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
3316 | 10 |
import sys |
11 |
import decimal |
|
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
12 |
import datetime |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
13 |
import random |
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
14 |
|
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
15 |
from logilab.mtconverter import xml_escape |
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
16 |
from logilab.common.deprecation import deprecated |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
17 |
|
1024
bb96289257bf
remove unused working_hours function, new todate function since we can't compare date and datetime objects
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
18 |
# initialize random seed from current time |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
19 |
random.seed() |
1024
bb96289257bf
remove unused working_hours function, new todate function since we can't compare date and datetime objects
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
20 |
|
3146
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
21 |
if sys.version_info[:2] < (2, 5): |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
22 |
|
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
23 |
from time import time |
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
24 |
from md5 import md5 |
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
25 |
from random import randint |
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
26 |
|
3146
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
27 |
def make_uid(key): |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
28 |
"""forge a unique identifier |
4693
1eaf5ce04844
ensure make_uid return something that may be used as dom id
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4653
diff
changeset
|
29 |
XXX not that unique on win32 |
1eaf5ce04844
ensure make_uid return something that may be used as dom id
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4653
diff
changeset
|
30 |
""" |
1eaf5ce04844
ensure make_uid return something that may be used as dom id
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4653
diff
changeset
|
31 |
key = str(key) |
1eaf5ce04844
ensure make_uid return something that may be used as dom id
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4653
diff
changeset
|
32 |
msg = key + "%.10f" % time() + str(randint(0, 1000000)) |
1eaf5ce04844
ensure make_uid return something that may be used as dom id
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4653
diff
changeset
|
33 |
return key + md5(msg).hexdigest() |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
34 |
|
3146
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
35 |
else: |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
36 |
|
3146
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
37 |
from uuid import uuid4 |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
38 |
|
3146
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
39 |
def make_uid(key): |
3364
039d1694f36d
fix uid generation for py >= 2.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
40 |
# remove dash, generated uid are used as identifier sometimes (sql table |
039d1694f36d
fix uid generation for py >= 2.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
41 |
# names at least) |
039d1694f36d
fix uid generation for py >= 2.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
42 |
return str(key) + str(uuid4()).replace('-', '') |
039d1694f36d
fix uid generation for py >= 2.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
43 |
|
0 | 44 |
|
45 |
def dump_class(cls, clsname): |
|
46 |
"""create copy of a class by creating an empty class inheriting |
|
47 |
from the given cls. |
|
48 |
||
49 |
Those class will be used as place holder for attribute and relation |
|
50 |
description |
|
51 |
""" |
|
52 |
# type doesn't accept unicode name |
|
53 |
# return type.__new__(type, str(clsname), (cls,), {}) |
|
54 |
# __autogenerated__ attribute is just a marker |
|
55 |
return type(str(clsname), (cls,), {'__autogenerated__': True}) |
|
56 |
||
57 |
||
58 |
def merge_dicts(dict1, dict2): |
|
59 |
"""update a copy of `dict1` with `dict2`""" |
|
60 |
dict1 = dict(dict1) |
|
61 |
dict1.update(dict2) |
|
62 |
return dict1 |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
63 |
|
0 | 64 |
|
3882
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
65 |
# use networkX instead ? |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
66 |
# http://networkx.lanl.gov/reference/algorithms.traversal.html#module-networkx.algorithms.traversal.astar |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
67 |
def transitive_closure_of(entity, relname, _seen=None): |
4317
b2093f8f0493
some docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
68 |
"""return transitive closure *for the subgraph starting from the given |
b2093f8f0493
some docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
69 |
entity* (eg 'parent' entities are not included in the results) |
b2093f8f0493
some docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
70 |
""" |
3882
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
71 |
if _seen is None: |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
72 |
_seen = set() |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
73 |
_seen.add(entity.eid) |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
74 |
yield entity |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
75 |
for child in getattr(entity, relname): |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
76 |
if child.eid in _seen: |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
77 |
continue |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
78 |
for subchild in transitive_closure_of(child, relname, _seen): |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
79 |
yield subchild |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
80 |
|
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
81 |
|
0 | 82 |
class SizeConstrainedList(list): |
83 |
"""simple list that makes sure the list does not get bigger |
|
84 |
than a given size. |
|
85 |
||
86 |
when the list is full and a new element is added, the first |
|
87 |
element of the list is removed before appending the new one |
|
88 |
||
89 |
>>> l = SizeConstrainedList(2) |
|
90 |
>>> l.append(1) |
|
91 |
>>> l.append(2) |
|
92 |
>>> l |
|
93 |
[1, 2] |
|
94 |
>>> l.append(3) |
|
95 |
[2, 3] |
|
96 |
""" |
|
97 |
def __init__(self, maxsize): |
|
98 |
self.maxsize = maxsize |
|
99 |
||
100 |
def append(self, element): |
|
101 |
if len(self) == self.maxsize: |
|
102 |
del self[0] |
|
103 |
super(SizeConstrainedList, self).append(element) |
|
104 |
||
105 |
def extend(self, sequence): |
|
106 |
super(SizeConstrainedList, self).extend(sequence) |
|
107 |
keepafter = len(self) - self.maxsize |
|
108 |
if keepafter > 0: |
|
109 |
del self[:keepafter] |
|
110 |
||
111 |
__iadd__ = extend |
|
112 |
||
113 |
||
114 |
class UStringIO(list): |
|
115 |
"""a file wrapper which automatically encode unicode string to an encoding |
|
116 |
specifed in the constructor |
|
117 |
""" |
|
118 |
||
119 |
def __nonzero__(self): |
|
120 |
return True |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
121 |
|
0 | 122 |
def write(self, value): |
123 |
assert isinstance(value, unicode), u"unicode required not %s : %s"\ |
|
124 |
% (type(value).__name__, repr(value)) |
|
125 |
self.append(value) |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
126 |
|
0 | 127 |
def getvalue(self): |
128 |
return u''.join(self) |
|
129 |
||
130 |
def __repr__(self): |
|
131 |
return '<%s at %#x>' % (self.__class__.__name__, id(self)) |
|
132 |
||
133 |
||
134 |
class HTMLHead(UStringIO): |
|
135 |
"""wraps HTML header's stream |
|
136 |
||
137 |
Request objects use a HTMLHead instance to ease adding of |
|
138 |
javascripts and stylesheets |
|
139 |
""" |
|
140 |
js_unload_code = u'jQuery(window).unload(unloadPageData);' |
|
141 |
||
142 |
def __init__(self): |
|
143 |
super(HTMLHead, self).__init__() |
|
144 |
self.jsvars = [] |
|
145 |
self.jsfiles = [] |
|
146 |
self.cssfiles = [] |
|
147 |
self.ie_cssfiles = [] |
|
148 |
self.post_inlined_scripts = [] |
|
149 |
self.pagedata_unload = False |
|
150 |
||
151 |
||
152 |
def add_raw(self, rawheader): |
|
153 |
self.write(rawheader) |
|
154 |
||
3838
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
155 |
def define_var(self, var, value, override=True): |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
156 |
"""adds a javascript var declaration / assginment in the header |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
157 |
|
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
158 |
:param var: the variable name |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
159 |
:param value: the variable value (as a raw python value, |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
160 |
it will be jsonized later) |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
161 |
:param override: if False, don't set the variable value if the variable |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
162 |
is already defined. Default is True. |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
163 |
""" |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
164 |
self.jsvars.append( (var, value, override) ) |
0 | 165 |
|
166 |
def add_post_inline_script(self, content): |
|
167 |
self.post_inlined_scripts.append(content) |
|
28
9b7067bfaa15
introduce html_headers.on_load() method as a shortcut for add_post_inline_script('''jQuery(document).ready(...''')
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
168 |
|
2258
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
169 |
def add_onload(self, jscode, jsoncall=False): |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
170 |
if jsoncall: |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
171 |
self.add_post_inline_script(u"""jQuery(CubicWeb).bind('ajax-loaded', function(event) { |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
172 |
%s |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
173 |
});""" % jscode) |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
174 |
else: |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
175 |
self.add_post_inline_script(u"""jQuery(document).ready(function () { |
28
9b7067bfaa15
introduce html_headers.on_load() method as a shortcut for add_post_inline_script('''jQuery(document).ready(...''')
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
176 |
%s |
9b7067bfaa15
introduce html_headers.on_load() method as a shortcut for add_post_inline_script('''jQuery(document).ready(...''')
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
177 |
});""" % jscode) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
178 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
179 |
|
0 | 180 |
def add_js(self, jsfile): |
181 |
"""adds `jsfile` to the list of javascripts used in the webpage |
|
182 |
||
183 |
This function checks if the file has already been added |
|
184 |
:param jsfile: the script's URL |
|
185 |
""" |
|
186 |
if jsfile not in self.jsfiles: |
|
187 |
self.jsfiles.append(jsfile) |
|
188 |
||
189 |
def add_css(self, cssfile, media): |
|
190 |
"""adds `cssfile` to the list of javascripts used in the webpage |
|
191 |
||
192 |
This function checks if the file has already been added |
|
193 |
:param cssfile: the stylesheet's URL |
|
194 |
""" |
|
195 |
if (cssfile, media) not in self.cssfiles: |
|
196 |
self.cssfiles.append( (cssfile, media) ) |
|
197 |
||
198 |
def add_ie_css(self, cssfile, media='all'): |
|
199 |
"""registers some IE specific CSS""" |
|
200 |
if (cssfile, media) not in self.ie_cssfiles: |
|
201 |
self.ie_cssfiles.append( (cssfile, media) ) |
|
202 |
||
203 |
def add_unload_pagedata(self): |
|
204 |
"""registers onunload callback to clean page data on server""" |
|
205 |
if not self.pagedata_unload: |
|
206 |
self.post_inlined_scripts.append(self.js_unload_code) |
|
207 |
self.pagedata_unload = True |
|
208 |
||
643
616191014b8b
[jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
28
diff
changeset
|
209 |
def getvalue(self, skiphead=False): |
0 | 210 |
"""reimplement getvalue to provide a consistent (and somewhat browser |
211 |
optimzed cf. http://stevesouders.com/cuzillion) order in external |
|
212 |
resources declaration |
|
213 |
""" |
|
214 |
w = self.write |
|
215 |
# 1/ variable declaration if any |
|
216 |
if self.jsvars: |
|
2554
3b6a6d2f9d7e
make jsvars declaration xhtml and html compatible
Fabrice <fabrice@secondweb.fr>
parents:
2361
diff
changeset
|
217 |
w(u'<script type="text/javascript"><!--//--><![CDATA[//><!--\n') |
3838
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
218 |
for var, value, override in self.jsvars: |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
219 |
vardecl = u'%s = %s;' % (var, dumps(value)) |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
220 |
if not override: |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
221 |
vardecl = (u'if (typeof %s == "undefined") {%s}' % |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
222 |
(var, vardecl)) |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
223 |
w(vardecl + u'\n') |
2554
3b6a6d2f9d7e
make jsvars declaration xhtml and html compatible
Fabrice <fabrice@secondweb.fr>
parents:
2361
diff
changeset
|
224 |
w(u'//--><!]]></script>\n') |
0 | 225 |
# 2/ css files |
226 |
for cssfile, media in self.cssfiles: |
|
227 |
w(u'<link rel="stylesheet" type="text/css" media="%s" href="%s"/>\n' % |
|
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
228 |
(media, xml_escape(cssfile))) |
0 | 229 |
# 3/ ie css if necessary |
230 |
if self.ie_cssfiles: |
|
231 |
w(u'<!--[if lt IE 8]>\n') |
|
232 |
for cssfile, media in self.ie_cssfiles: |
|
233 |
w(u'<link rel="stylesheet" type="text/css" media="%s" href="%s"/>\n' % |
|
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
234 |
(media, xml_escape(cssfile))) |
0 | 235 |
w(u'<![endif]--> \n') |
236 |
# 4/ js files |
|
237 |
for jsfile in self.jsfiles: |
|
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
238 |
w(u'<script type="text/javascript" src="%s"></script>\n' % |
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
239 |
xml_escape(jsfile)) |
0 | 240 |
# 5/ post inlined scripts (i.e. scripts depending on other JS files) |
241 |
if self.post_inlined_scripts: |
|
242 |
w(u'<script type="text/javascript">\n') |
|
243 |
w(u'\n\n'.join(self.post_inlined_scripts)) |
|
244 |
w(u'\n</script>\n') |
|
643
616191014b8b
[jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
28
diff
changeset
|
245 |
header = super(HTMLHead, self).getvalue() |
616191014b8b
[jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
28
diff
changeset
|
246 |
if skiphead: |
616191014b8b
[jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
28
diff
changeset
|
247 |
return header |
616191014b8b
[jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
28
diff
changeset
|
248 |
return u'<head>\n%s</head>\n' % header |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
249 |
|
0 | 250 |
|
251 |
class HTMLStream(object): |
|
252 |
"""represents a HTML page. |
|
253 |
||
254 |
This is used my main templates so that HTML headers can be added |
|
255 |
at any time during the page generation. |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
256 |
|
0 | 257 |
HTMLStream uses the (U)StringIO interface to be compliant with |
258 |
existing code. |
|
259 |
""" |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
260 |
|
0 | 261 |
def __init__(self, req): |
262 |
# stream for <head> |
|
263 |
self.head = req.html_headers |
|
264 |
# main stream |
|
265 |
self.body = UStringIO() |
|
266 |
self.doctype = u'' |
|
267 |
# xmldecl and html opening tag |
|
268 |
self.xmldecl = u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding |
|
269 |
self.htmltag = u'<html xmlns="http://www.w3.org/1999/xhtml" ' \ |
|
270 |
'xmlns:cubicweb="http://www.logilab.org/2008/cubicweb" ' \ |
|
271 |
'xml:lang="%s" lang="%s">' % (req.lang, req.lang) |
|
3094
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3091
diff
changeset
|
272 |
# keep main_stream's reference on req for easier text/html demoting |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3091
diff
changeset
|
273 |
req.main_stream = self |
0 | 274 |
|
275 |
def write(self, data): |
|
276 |
"""StringIO interface: this method will be assigned to self.w |
|
277 |
""" |
|
278 |
self.body.write(data) |
|
279 |
||
280 |
def getvalue(self): |
|
281 |
"""writes HTML headers, closes </head> tag and writes HTML body""" |
|
282 |
return u'%s\n%s\n%s\n%s\n%s\n</html>' % (self.xmldecl, self.doctype, |
|
283 |
self.htmltag, |
|
284 |
self.head.getvalue(), |
|
285 |
self.body.getvalue()) |
|
286 |
||
287 |
||
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
288 |
def can_do_pdf_conversion(__answer=[None]): |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
289 |
"""pdf conversion depends on |
3789
fb22b55f80f8
update docstring, xml escape exception description
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3372
diff
changeset
|
290 |
* pysixt (python package) |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
291 |
* fop 0.9x |
0 | 292 |
""" |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
293 |
if __answer[0] is not None: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
294 |
return __answer[0] |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
295 |
try: |
2936
f35b64718d02
follow name change, fix pdf template registration, add debian package tracking
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2935
diff
changeset
|
296 |
import pysixt |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
297 |
except ImportError: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
298 |
__answer[0] = False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
299 |
return False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
300 |
from subprocess import Popen, STDOUT |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
301 |
import os |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
302 |
try: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
303 |
Popen(['/usr/bin/fop', '-q'], |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
304 |
stdout=open(os.devnull, 'w'), |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
305 |
stderr=STDOUT) |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
306 |
except OSError, e: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
307 |
print e |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
308 |
__answer[0] = False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
309 |
return False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
310 |
__answer[0] = True |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
311 |
return True |
3231
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
312 |
|
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
313 |
try: |
3372 | 314 |
# may not be there if cubicweb-web not installed |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
315 |
from simplejson import dumps, JSONEncoder |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
316 |
except ImportError: |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
317 |
pass |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
318 |
else: |
3231
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
319 |
|
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
320 |
class CubicWebJsonEncoder(JSONEncoder): |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
321 |
"""define a simplejson encoder to be able to encode yams std types""" |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
322 |
def default(self, obj): |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
323 |
if isinstance(obj, datetime.datetime): |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
324 |
return obj.strftime('%Y/%m/%d %H:%M:%S') |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
325 |
elif isinstance(obj, datetime.date): |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
326 |
return obj.strftime('%Y/%m/%d') |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
327 |
elif isinstance(obj, datetime.time): |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
328 |
return obj.strftime('%H:%M:%S') |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
329 |
elif isinstance(obj, datetime.timedelta): |
4000
4b16a7c01a25
fix jsonization of interval
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3902
diff
changeset
|
330 |
return (obj.days * 24 * 60 * 60) + obj.seconds |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
331 |
elif isinstance(obj, decimal.Decimal): |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
332 |
return float(obj) |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
333 |
try: |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
334 |
return JSONEncoder.default(self, obj) |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
335 |
except TypeError: |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
336 |
# we never ever want to fail because of an unknown type, |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
337 |
# just return None in those cases. |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
338 |
return None |
4652
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
339 |
|
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
340 |
from logilab.common import date |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
341 |
_THIS_MOD_NS = globals() |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
342 |
for funcname in ('date_range', 'todate', 'todatetime', 'datetime2ticks', |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
343 |
'days_in_month', 'days_in_year', 'previous_month', |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
344 |
'next_month', 'first_day', 'last_day', 'ustrftime', |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
345 |
'strptime'): |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
346 |
msg = '[3.6] %s has been moved to logilab.common.date' % funcname |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
347 |
_THIS_MOD_NS[funcname] = deprecated(msg)(getattr(date, funcname)) |