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