author | Katia Saurfelt <katia.saurfelt@logilab.fr> |
Thu, 30 Apr 2009 17:44:35 +0200 | |
branch | tls-sprint |
changeset 1625 | 744f0cc8758f |
parent 1616 | b081fe3d5945 |
child 1626 | 399ac5294471 |
permissions | -rw-r--r-- |
0 | 1 |
"""Some utilities for CubicWeb server/clients. |
2 |
||
3 |
:organization: Logilab |
|
708
b4728112625f
move utils from cw.common to cw
sylvain.thenault@logilab.fr
parents:
643
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
709 | 9 |
import locale |
0 | 10 |
from md5 import md5 |
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
|
11 |
from datetime import datetime, timedelta, date |
1397
6cbc7bc8ea6d
rollback florent's not-proven-to-be-useful fix
sylvain.thenault@logilab.fr
parents:
1387
diff
changeset
|
12 |
from time import time |
0 | 13 |
from random import randint, seed |
1625
744f0cc8758f
restore monthrange import
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1616
diff
changeset
|
14 |
from calendar import monthrange |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
15 |
|
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
|
16 |
# 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
|
17 |
seed() |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
18 |
try: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
19 |
strptime = datetime.strptime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
20 |
except AttributeError: # py < 2.5 |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
21 |
from time import strptime as time_strptime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
22 |
def strptime(value, format): |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
23 |
return datetime(*time_strptime(value, format)[:6]) |
0 | 24 |
|
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
|
25 |
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
|
26 |
"""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
|
27 |
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
|
28 |
return date(somedate.year, somedate.month, somedate.day) |
1603 | 29 |
assert isinstance(somedate, date), repr(somedate) |
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
|
30 |
return date |
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
|
31 |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
32 |
ONEDAY = timedelta(days=1) |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
33 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
34 |
def days_in_month(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
35 |
return monthrange(date_.year, date_.month)[1] |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
36 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
37 |
def previous_month(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
38 |
return first_day(date_) - ONEDAY |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
39 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
40 |
def next_month(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
41 |
return last_day(date_) + ONEDAY |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
42 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
43 |
def first_day(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
44 |
return date(date_.year, date_.month, 1) |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
45 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
46 |
def last_day(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
47 |
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
|
48 |
|
0 | 49 |
def date_range(begin, end, incr=1, include=None): |
50 |
"""yields each date between begin and end |
|
51 |
:param begin: the start date |
|
52 |
:param end: the end date |
|
53 |
: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
|
54 |
one day. |
0 | 55 |
:param include: None (means no exclusion) or a function taking a |
56 |
date as parameter, and returning True if the date |
|
57 |
should be included. |
|
58 |
""" |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
59 |
incr = timedelta(incr, 0, 0) |
1132 | 60 |
while begin <= end: |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
61 |
if include is None or include(begin): |
1132 | 62 |
yield begin |
63 |
begin += incr |
|
0 | 64 |
|
709 | 65 |
def ustrftime(date, fmt='%Y-%m-%d'): |
66 |
"""like strftime, but returns a unicode string instead of an encoded |
|
67 |
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
|
68 |
|
709 | 69 |
encoding is guessed by locale.getpreferredencoding() |
70 |
""" |
|
71 |
# date format may depend on the locale |
|
72 |
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
|
73 |
return unicode(date.strftime(str(fmt)), encoding) |
709 | 74 |
|
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
|
75 |
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
|
76 |
"""forge a unique identifier""" |
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
|
77 |
msg = str(key) + "%.10f"%time() + str(randint(0, 1000000)) |
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
|
78 |
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
|
79 |
|
0 | 80 |
|
81 |
def dump_class(cls, clsname): |
|
82 |
"""create copy of a class by creating an empty class inheriting |
|
83 |
from the given cls. |
|
84 |
||
85 |
Those class will be used as place holder for attribute and relation |
|
86 |
description |
|
87 |
""" |
|
88 |
# type doesn't accept unicode name |
|
89 |
# return type.__new__(type, str(clsname), (cls,), {}) |
|
90 |
# __autogenerated__ attribute is just a marker |
|
91 |
return type(str(clsname), (cls,), {'__autogenerated__': True}) |
|
92 |
||
93 |
||
94 |
def merge_dicts(dict1, dict2): |
|
95 |
"""update a copy of `dict1` with `dict2`""" |
|
96 |
dict1 = dict(dict1) |
|
97 |
dict1.update(dict2) |
|
98 |
return dict1 |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
99 |
|
0 | 100 |
|
101 |
class SizeConstrainedList(list): |
|
102 |
"""simple list that makes sure the list does not get bigger |
|
103 |
than a given size. |
|
104 |
||
105 |
when the list is full and a new element is added, the first |
|
106 |
element of the list is removed before appending the new one |
|
107 |
||
108 |
>>> l = SizeConstrainedList(2) |
|
109 |
>>> l.append(1) |
|
110 |
>>> l.append(2) |
|
111 |
>>> l |
|
112 |
[1, 2] |
|
113 |
>>> l.append(3) |
|
114 |
[2, 3] |
|
115 |
""" |
|
116 |
def __init__(self, maxsize): |
|
117 |
self.maxsize = maxsize |
|
118 |
||
119 |
def append(self, element): |
|
120 |
if len(self) == self.maxsize: |
|
121 |
del self[0] |
|
122 |
super(SizeConstrainedList, self).append(element) |
|
123 |
||
124 |
def extend(self, sequence): |
|
125 |
super(SizeConstrainedList, self).extend(sequence) |
|
126 |
keepafter = len(self) - self.maxsize |
|
127 |
if keepafter > 0: |
|
128 |
del self[:keepafter] |
|
129 |
||
130 |
__iadd__ = extend |
|
131 |
||
132 |
||
133 |
class UStringIO(list): |
|
134 |
"""a file wrapper which automatically encode unicode string to an encoding |
|
135 |
specifed in the constructor |
|
136 |
""" |
|
137 |
||
138 |
def __nonzero__(self): |
|
139 |
return True |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
140 |
|
0 | 141 |
def write(self, value): |
142 |
assert isinstance(value, unicode), u"unicode required not %s : %s"\ |
|
143 |
% (type(value).__name__, repr(value)) |
|
144 |
self.append(value) |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
145 |
|
0 | 146 |
def getvalue(self): |
147 |
return u''.join(self) |
|
148 |
||
149 |
def __repr__(self): |
|
150 |
return '<%s at %#x>' % (self.__class__.__name__, id(self)) |
|
151 |
||
152 |
||
153 |
class HTMLHead(UStringIO): |
|
154 |
"""wraps HTML header's stream |
|
155 |
||
156 |
Request objects use a HTMLHead instance to ease adding of |
|
157 |
javascripts and stylesheets |
|
158 |
""" |
|
159 |
js_unload_code = u'jQuery(window).unload(unloadPageData);' |
|
160 |
||
161 |
def __init__(self): |
|
162 |
super(HTMLHead, self).__init__() |
|
163 |
self.jsvars = [] |
|
164 |
self.jsfiles = [] |
|
165 |
self.cssfiles = [] |
|
166 |
self.ie_cssfiles = [] |
|
167 |
self.post_inlined_scripts = [] |
|
168 |
self.pagedata_unload = False |
|
169 |
||
170 |
||
171 |
def add_raw(self, rawheader): |
|
172 |
self.write(rawheader) |
|
173 |
||
174 |
def define_var(self, var, value): |
|
175 |
self.jsvars.append( (var, value) ) |
|
176 |
||
177 |
def add_post_inline_script(self, content): |
|
178 |
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
|
179 |
|
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
|
180 |
def add_onload(self, jscode): |
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
|
181 |
self.add_post_inline_script(u"""jQuery(document).ready(function () { |
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
|
182 |
%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
|
183 |
});""" % jscode) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
184 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
185 |
|
0 | 186 |
def add_js(self, jsfile): |
187 |
"""adds `jsfile` to the list of javascripts used in the webpage |
|
188 |
||
189 |
This function checks if the file has already been added |
|
190 |
:param jsfile: the script's URL |
|
191 |
""" |
|
192 |
if jsfile not in self.jsfiles: |
|
193 |
self.jsfiles.append(jsfile) |
|
194 |
||
195 |
def add_css(self, cssfile, media): |
|
196 |
"""adds `cssfile` to the list of javascripts used in the webpage |
|
197 |
||
198 |
This function checks if the file has already been added |
|
199 |
:param cssfile: the stylesheet's URL |
|
200 |
""" |
|
201 |
if (cssfile, media) not in self.cssfiles: |
|
202 |
self.cssfiles.append( (cssfile, media) ) |
|
203 |
||
204 |
def add_ie_css(self, cssfile, media='all'): |
|
205 |
"""registers some IE specific CSS""" |
|
206 |
if (cssfile, media) not in self.ie_cssfiles: |
|
207 |
self.ie_cssfiles.append( (cssfile, media) ) |
|
208 |
||
209 |
def add_unload_pagedata(self): |
|
210 |
"""registers onunload callback to clean page data on server""" |
|
211 |
if not self.pagedata_unload: |
|
212 |
self.post_inlined_scripts.append(self.js_unload_code) |
|
213 |
self.pagedata_unload = True |
|
214 |
||
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
|
215 |
def getvalue(self, skiphead=False): |
0 | 216 |
"""reimplement getvalue to provide a consistent (and somewhat browser |
217 |
optimzed cf. http://stevesouders.com/cuzillion) order in external |
|
218 |
resources declaration |
|
219 |
""" |
|
220 |
w = self.write |
|
221 |
# 1/ variable declaration if any |
|
222 |
if self.jsvars: |
|
223 |
from simplejson import dumps |
|
224 |
w(u'<script type="text/javascript">\n') |
|
225 |
for var, value in self.jsvars: |
|
226 |
w(u'%s = %s;\n' % (var, dumps(value))) |
|
227 |
w(u'</script>\n') |
|
228 |
# 2/ css files |
|
229 |
for cssfile, media in self.cssfiles: |
|
230 |
w(u'<link rel="stylesheet" type="text/css" media="%s" href="%s"/>\n' % |
|
231 |
(media, cssfile)) |
|
232 |
# 3/ ie css if necessary |
|
233 |
if self.ie_cssfiles: |
|
234 |
w(u'<!--[if lt IE 8]>\n') |
|
235 |
for cssfile, media in self.ie_cssfiles: |
|
236 |
w(u'<link rel="stylesheet" type="text/css" media="%s" href="%s"/>\n' % |
|
237 |
(media, cssfile)) |
|
238 |
w(u'<![endif]--> \n') |
|
239 |
# 4/ js files |
|
240 |
for jsfile in self.jsfiles: |
|
241 |
w(u'<script type="text/javascript" src="%s"></script>\n' % jsfile) |
|
242 |
# 5/ post inlined scripts (i.e. scripts depending on other JS files) |
|
243 |
if self.post_inlined_scripts: |
|
244 |
w(u'<script type="text/javascript">\n') |
|
245 |
w(u'\n\n'.join(self.post_inlined_scripts)) |
|
246 |
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
|
247 |
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
|
248 |
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
|
249 |
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
|
250 |
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
|
251 |
|
0 | 252 |
|
253 |
class HTMLStream(object): |
|
254 |
"""represents a HTML page. |
|
255 |
||
256 |
This is used my main templates so that HTML headers can be added |
|
257 |
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
|
258 |
|
0 | 259 |
HTMLStream uses the (U)StringIO interface to be compliant with |
260 |
existing code. |
|
261 |
""" |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
262 |
|
0 | 263 |
def __init__(self, req): |
264 |
# stream for <head> |
|
265 |
self.head = req.html_headers |
|
266 |
# main stream |
|
267 |
self.body = UStringIO() |
|
268 |
self.doctype = u'' |
|
269 |
# xmldecl and html opening tag |
|
270 |
self.xmldecl = u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding |
|
271 |
self.htmltag = u'<html xmlns="http://www.w3.org/1999/xhtml" ' \ |
|
272 |
'xmlns:cubicweb="http://www.logilab.org/2008/cubicweb" ' \ |
|
273 |
'xml:lang="%s" lang="%s">' % (req.lang, req.lang) |
|
274 |
||
275 |
||
276 |
def write(self, data): |
|
277 |
"""StringIO interface: this method will be assigned to self.w |
|
278 |
""" |
|
279 |
self.body.write(data) |
|
280 |
||
281 |
def getvalue(self): |
|
282 |
"""writes HTML headers, closes </head> tag and writes HTML body""" |
|
283 |
return u'%s\n%s\n%s\n%s\n%s\n</html>' % (self.xmldecl, self.doctype, |
|
284 |
self.htmltag, |
|
285 |
self.head.getvalue(), |
|
286 |
self.body.getvalue()) |
|
287 |
||
288 |
||
289 |
class AcceptMixIn(object): |
|
290 |
"""Mixin class for vobjects defining the 'accepts' attribute describing |
|
291 |
a set of supported entity type (Any by default). |
|
292 |
""" |
|
293 |
# XXX deprecated, no more necessary |
|
294 |
||
295 |