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