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