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