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