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