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