author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Tue, 15 Sep 2009 16:00:03 +0200 | |
branch | 3.5 |
changeset 3231 | 3ee43e2f8560 |
parent 3101 | e75ab3635f07 |
child 3293 | 69c0ba095536 |
child 3316 | c4c07aab1c39 |
permissions | -rw-r--r-- |
0 | 1 |
"""Some utilities for CubicWeb server/clients. |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1751
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:
1751
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
10 |
from logilab.mtconverter import xml_escape |
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
11 |
|
709 | 12 |
import locale |
0 | 13 |
from md5 import md5 |
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
|
14 |
import datetime as pydatetime |
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
|
15 |
from datetime import datetime, timedelta, date |
2009
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
16 |
from time import time, mktime |
0 | 17 |
from random import randint, seed |
1625
744f0cc8758f
restore monthrange import
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1616
diff
changeset
|
18 |
from calendar import monthrange |
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
|
19 |
import decimal |
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
|
20 |
|
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
|
21 |
import simplejson |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
22 |
|
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
|
23 |
# initialize random seed from current time |
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
|
24 |
seed() |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
25 |
try: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
26 |
strptime = datetime.strptime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
27 |
except AttributeError: # py < 2.5 |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
28 |
from time import strptime as time_strptime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
29 |
def strptime(value, format): |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
30 |
return datetime(*time_strptime(value, format)[:6]) |
0 | 31 |
|
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
|
32 |
def todate(somedate): |
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
|
33 |
"""return a date from a date (leaving unchanged) or a datetime""" |
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
|
34 |
if isinstance(somedate, datetime): |
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
|
35 |
return date(somedate.year, somedate.month, somedate.day) |
1603 | 36 |
assert isinstance(somedate, date), repr(somedate) |
1621
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
37 |
return somedate |
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
38 |
|
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
39 |
def todatetime(somedate): |
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
40 |
"""return a date from a date (leaving unchanged) or a datetime""" |
1751 | 41 |
# take care, datetime is a subclass of date |
42 |
if isinstance(somedate, datetime): |
|
43 |
return somedate |
|
44 |
assert isinstance(somedate, date), repr(somedate) |
|
45 |
return datetime(somedate.year, somedate.month, somedate.day) |
|
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
|
46 |
|
2009
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
47 |
def datetime2ticks(date): |
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
48 |
return mktime(date.timetuple()) * 1000 |
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
49 |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
50 |
ONEDAY = timedelta(days=1) |
1697
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
51 |
ONEWEEK = timedelta(days=7) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
52 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
53 |
def days_in_month(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
54 |
return monthrange(date_.year, date_.month)[1] |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
55 |
|
1697
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
56 |
def previous_month(date_, nbmonth=1): |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
57 |
while nbmonth: |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
58 |
date_ = first_day(date_) - ONEDAY |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
59 |
nbmonth -= 1 |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
60 |
return date_ |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
61 |
|
1697
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
62 |
def next_month(date_, nbmonth=1): |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
63 |
while nbmonth: |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
64 |
date_ = last_day(date_) + ONEDAY |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
65 |
nbmonth -= 1 |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
66 |
return date_ |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
67 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
68 |
def first_day(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
69 |
return date(date_.year, date_.month, 1) |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
70 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
71 |
def last_day(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
72 |
return date(date_.year, date_.month, days_in_month(date_)) |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
73 |
|
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
74 |
def date_range(begin, end, incday=None, incmonth=None): |
0 | 75 |
"""yields each date between begin and end |
76 |
:param begin: the start date |
|
77 |
:param end: the end date |
|
78 |
:param incr: the step to use to iterate over dates. Default is |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
79 |
one day. |
0 | 80 |
:param include: None (means no exclusion) or a function taking a |
81 |
date as parameter, and returning True if the date |
|
82 |
should be included. |
|
83 |
""" |
|
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
84 |
assert not (incday and incmonth) |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
85 |
begin = todate(begin) |
1711
182536159750
Make sure end is datetime.date.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1705
diff
changeset
|
86 |
end = todate(end) |
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
87 |
if incmonth: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
88 |
while begin < end: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
89 |
begin = next_month(begin, incmonth) |
1132 | 90 |
yield begin |
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
91 |
else: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
92 |
if not incday: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
93 |
incr = ONEDAY |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
94 |
else: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
95 |
incr = timedelta(incday) |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
96 |
while begin <= end: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
97 |
yield begin |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
98 |
begin += incr |
0 | 99 |
|
709 | 100 |
def ustrftime(date, fmt='%Y-%m-%d'): |
101 |
"""like strftime, but returns a unicode string instead of an encoded |
|
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
102 |
string which' may be problematic with localized date. |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
103 |
|
709 | 104 |
encoding is guessed by locale.getpreferredencoding() |
105 |
""" |
|
106 |
# date format may depend on the locale |
|
107 |
encoding = locale.getpreferredencoding(do_setlocale=False) or 'UTF-8' |
|
1397
6cbc7bc8ea6d
rollback florent's not-proven-to-be-useful fix
sylvain.thenault@logilab.fr
parents:
1387
diff
changeset
|
108 |
return unicode(date.strftime(str(fmt)), encoding) |
709 | 109 |
|
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
|
110 |
def make_uid(key): |
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
|
111 |
"""forge a unique identifier""" |
2009
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
112 |
msg = str(key) + "%.10f" % time() + str(randint(0, 1000000)) |
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
|
113 |
return md5(msg).hexdigest() |
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
|
114 |
|
0 | 115 |
|
116 |
def dump_class(cls, clsname): |
|
117 |
"""create copy of a class by creating an empty class inheriting |
|
118 |
from the given cls. |
|
119 |
||
120 |
Those class will be used as place holder for attribute and relation |
|
121 |
description |
|
122 |
""" |
|
123 |
# type doesn't accept unicode name |
|
124 |
# return type.__new__(type, str(clsname), (cls,), {}) |
|
125 |
# __autogenerated__ attribute is just a marker |
|
126 |
return type(str(clsname), (cls,), {'__autogenerated__': True}) |
|
127 |
||
128 |
||
129 |
def merge_dicts(dict1, dict2): |
|
130 |
"""update a copy of `dict1` with `dict2`""" |
|
131 |
dict1 = dict(dict1) |
|
132 |
dict1.update(dict2) |
|
133 |
return dict1 |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
134 |
|
0 | 135 |
|
136 |
class SizeConstrainedList(list): |
|
137 |
"""simple list that makes sure the list does not get bigger |
|
138 |
than a given size. |
|
139 |
||
140 |
when the list is full and a new element is added, the first |
|
141 |
element of the list is removed before appending the new one |
|
142 |
||
143 |
>>> l = SizeConstrainedList(2) |
|
144 |
>>> l.append(1) |
|
145 |
>>> l.append(2) |
|
146 |
>>> l |
|
147 |
[1, 2] |
|
148 |
>>> l.append(3) |
|
149 |
[2, 3] |
|
150 |
""" |
|
151 |
def __init__(self, maxsize): |
|
152 |
self.maxsize = maxsize |
|
153 |
||
154 |
def append(self, element): |
|
155 |
if len(self) == self.maxsize: |
|
156 |
del self[0] |
|
157 |
super(SizeConstrainedList, self).append(element) |
|
158 |
||
159 |
def extend(self, sequence): |
|
160 |
super(SizeConstrainedList, self).extend(sequence) |
|
161 |
keepafter = len(self) - self.maxsize |
|
162 |
if keepafter > 0: |
|
163 |
del self[:keepafter] |
|
164 |
||
165 |
__iadd__ = extend |
|
166 |
||
167 |
||
168 |
class UStringIO(list): |
|
169 |
"""a file wrapper which automatically encode unicode string to an encoding |
|
170 |
specifed in the constructor |
|
171 |
""" |
|
172 |
||
173 |
def __nonzero__(self): |
|
174 |
return True |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
175 |
|
0 | 176 |
def write(self, value): |
177 |
assert isinstance(value, unicode), u"unicode required not %s : %s"\ |
|
178 |
% (type(value).__name__, repr(value)) |
|
179 |
self.append(value) |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
180 |
|
0 | 181 |
def getvalue(self): |
182 |
return u''.join(self) |
|
183 |
||
184 |
def __repr__(self): |
|
185 |
return '<%s at %#x>' % (self.__class__.__name__, id(self)) |
|
186 |
||
187 |
||
188 |
class HTMLHead(UStringIO): |
|
189 |
"""wraps HTML header's stream |
|
190 |
||
191 |
Request objects use a HTMLHead instance to ease adding of |
|
192 |
javascripts and stylesheets |
|
193 |
""" |
|
194 |
js_unload_code = u'jQuery(window).unload(unloadPageData);' |
|
195 |
||
196 |
def __init__(self): |
|
197 |
super(HTMLHead, self).__init__() |
|
198 |
self.jsvars = [] |
|
199 |
self.jsfiles = [] |
|
200 |
self.cssfiles = [] |
|
201 |
self.ie_cssfiles = [] |
|
202 |
self.post_inlined_scripts = [] |
|
203 |
self.pagedata_unload = False |
|
204 |
||
205 |
||
206 |
def add_raw(self, rawheader): |
|
207 |
self.write(rawheader) |
|
208 |
||
209 |
def define_var(self, var, value): |
|
210 |
self.jsvars.append( (var, value) ) |
|
211 |
||
212 |
def add_post_inline_script(self, content): |
|
213 |
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
|
214 |
|
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
|
215 |
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
|
216 |
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
|
217 |
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
|
218 |
%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
|
219 |
});""" % 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
|
220 |
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
|
221 |
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
|
222 |
%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
|
223 |
});""" % jscode) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
224 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
225 |
|
0 | 226 |
def add_js(self, jsfile): |
227 |
"""adds `jsfile` to the list of javascripts used in the webpage |
|
228 |
||
229 |
This function checks if the file has already been added |
|
230 |
:param jsfile: the script's URL |
|
231 |
""" |
|
232 |
if jsfile not in self.jsfiles: |
|
233 |
self.jsfiles.append(jsfile) |
|
234 |
||
235 |
def add_css(self, cssfile, media): |
|
236 |
"""adds `cssfile` to the list of javascripts used in the webpage |
|
237 |
||
238 |
This function checks if the file has already been added |
|
239 |
:param cssfile: the stylesheet's URL |
|
240 |
""" |
|
241 |
if (cssfile, media) not in self.cssfiles: |
|
242 |
self.cssfiles.append( (cssfile, media) ) |
|
243 |
||
244 |
def add_ie_css(self, cssfile, media='all'): |
|
245 |
"""registers some IE specific CSS""" |
|
246 |
if (cssfile, media) not in self.ie_cssfiles: |
|
247 |
self.ie_cssfiles.append( (cssfile, media) ) |
|
248 |
||
249 |
def add_unload_pagedata(self): |
|
250 |
"""registers onunload callback to clean page data on server""" |
|
251 |
if not self.pagedata_unload: |
|
252 |
self.post_inlined_scripts.append(self.js_unload_code) |
|
253 |
self.pagedata_unload = True |
|
254 |
||
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
|
255 |
def getvalue(self, skiphead=False): |
0 | 256 |
"""reimplement getvalue to provide a consistent (and somewhat browser |
257 |
optimzed cf. http://stevesouders.com/cuzillion) order in external |
|
258 |
resources declaration |
|
259 |
""" |
|
260 |
w = self.write |
|
261 |
# 1/ variable declaration if any |
|
262 |
if self.jsvars: |
|
263 |
from simplejson import dumps |
|
2554
3b6a6d2f9d7e
make jsvars declaration xhtml and html compatible
Fabrice <fabrice@secondweb.fr>
parents:
2361
diff
changeset
|
264 |
w(u'<script type="text/javascript"><!--//--><![CDATA[//><!--\n') |
0 | 265 |
for var, value in self.jsvars: |
266 |
w(u'%s = %s;\n' % (var, dumps(value))) |
|
2554
3b6a6d2f9d7e
make jsvars declaration xhtml and html compatible
Fabrice <fabrice@secondweb.fr>
parents:
2361
diff
changeset
|
267 |
w(u'//--><!]]></script>\n') |
0 | 268 |
# 2/ css files |
269 |
for cssfile, media in self.cssfiles: |
|
270 |
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
|
271 |
(media, xml_escape(cssfile))) |
0 | 272 |
# 3/ ie css if necessary |
273 |
if self.ie_cssfiles: |
|
274 |
w(u'<!--[if lt IE 8]>\n') |
|
275 |
for cssfile, media in self.ie_cssfiles: |
|
276 |
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
|
277 |
(media, xml_escape(cssfile))) |
0 | 278 |
w(u'<![endif]--> \n') |
279 |
# 4/ js files |
|
280 |
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
|
281 |
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
|
282 |
xml_escape(jsfile)) |
0 | 283 |
# 5/ post inlined scripts (i.e. scripts depending on other JS files) |
284 |
if self.post_inlined_scripts: |
|
285 |
w(u'<script type="text/javascript">\n') |
|
286 |
w(u'\n\n'.join(self.post_inlined_scripts)) |
|
287 |
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
|
288 |
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
|
289 |
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
|
290 |
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
|
291 |
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
|
292 |
|
0 | 293 |
|
294 |
class HTMLStream(object): |
|
295 |
"""represents a HTML page. |
|
296 |
||
297 |
This is used my main templates so that HTML headers can be added |
|
298 |
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
|
299 |
|
0 | 300 |
HTMLStream uses the (U)StringIO interface to be compliant with |
301 |
existing code. |
|
302 |
""" |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
303 |
|
0 | 304 |
def __init__(self, req): |
305 |
# stream for <head> |
|
306 |
self.head = req.html_headers |
|
307 |
# main stream |
|
308 |
self.body = UStringIO() |
|
309 |
self.doctype = u'' |
|
310 |
# xmldecl and html opening tag |
|
311 |
self.xmldecl = u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding |
|
312 |
self.htmltag = u'<html xmlns="http://www.w3.org/1999/xhtml" ' \ |
|
313 |
'xmlns:cubicweb="http://www.logilab.org/2008/cubicweb" ' \ |
|
314 |
'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
|
315 |
# 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
|
316 |
req.main_stream = self |
0 | 317 |
|
318 |
def write(self, data): |
|
319 |
"""StringIO interface: this method will be assigned to self.w |
|
320 |
""" |
|
321 |
self.body.write(data) |
|
322 |
||
323 |
def getvalue(self): |
|
324 |
"""writes HTML headers, closes </head> tag and writes HTML body""" |
|
325 |
return u'%s\n%s\n%s\n%s\n%s\n</html>' % (self.xmldecl, self.doctype, |
|
326 |
self.htmltag, |
|
327 |
self.head.getvalue(), |
|
328 |
self.body.getvalue()) |
|
329 |
||
330 |
||
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
331 |
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
|
332 |
"""pdf conversion depends on |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
333 |
* pyxmltrf (python package) |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
334 |
* fop 0.9x |
0 | 335 |
""" |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
336 |
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
|
337 |
return __answer[0] |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
338 |
try: |
2936
f35b64718d02
follow name change, fix pdf template registration, add debian package tracking
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2935
diff
changeset
|
339 |
import pysixt |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
340 |
except ImportError: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
341 |
__answer[0] = False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
342 |
return False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
343 |
from subprocess import Popen, STDOUT |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
344 |
import os |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
345 |
try: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
346 |
Popen(['/usr/bin/fop', '-q'], |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
347 |
stdout=open(os.devnull, 'w'), |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
348 |
stderr=STDOUT) |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
349 |
except OSError, e: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
350 |
print e |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
351 |
__answer[0] = False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
352 |
return False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
353 |
__answer[0] = True |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
354 |
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
|
355 |
|
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
|
356 |
|
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
|
357 |
class CubicWebJsonEncoder(simplejson.JSONEncoder): |
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
|
358 |
"""define a simplejson encoder to be able to encode yams std types""" |
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
|
359 |
def default(self, obj): |
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
|
360 |
if isinstance(obj, pydatetime.datetime): |
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
|
361 |
return obj.strftime('%Y/%m/%d %H:%M:%S') |
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
|
362 |
elif isinstance(obj, pydatetime.date): |
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
|
363 |
return obj.strftime('%Y/%m/%d') |
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
|
364 |
elif isinstance(obj, pydatetime.time): |
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
|
365 |
return obj.strftime('%H:%M:%S') |
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
|
366 |
elif isinstance(obj, decimal.Decimal): |
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
|
367 |
return float(obj) |
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
|
368 |
try: |
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
|
369 |
return simplejson.JSONEncoder.default(self, obj) |
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
|
370 |
except TypeError: |
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
|
371 |
# we never ever want to fail because of an unknown type, |
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
|
372 |
# just return None in those cases. |
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
|
373 |
return None |