author | Sandrine Ribeau <sandrine.ribeau@logilab.fr> |
Mon, 07 Dec 2009 06:51:33 -0800 | |
changeset 4006 | c89be0bdf943 |
parent 3998 | 94cc7cad3d2d |
child 4012 | f6c65e04704c |
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 |
||
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
10 |
from logilab.mtconverter import xml_escape |
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
11 |
|
709 | 12 |
import locale |
3316 | 13 |
import sys |
14 |
import decimal |
|
15 |
import datetime as pydatetime |
|
0 | 16 |
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
|
17 |
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
|
18 |
from time import time, mktime |
0 | 19 |
from random import randint, seed |
1625
744f0cc8758f
restore monthrange import
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1616
diff
changeset
|
20 |
from calendar import monthrange |
3231
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
21 |
import decimal |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
22 |
|
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
|
23 |
# 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
|
24 |
seed() |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
25 |
try: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
26 |
strptime = datetime.strptime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
27 |
except AttributeError: # py < 2.5 |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
28 |
from time import strptime as time_strptime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
29 |
def strptime(value, format): |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
30 |
return datetime(*time_strptime(value, format)[:6]) |
0 | 31 |
|
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
|
32 |
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
|
33 |
"""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
|
34 |
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
|
35 |
return date(somedate.year, somedate.month, somedate.day) |
1603 | 36 |
assert isinstance(somedate, date), repr(somedate) |
1621
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
37 |
return somedate |
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
38 |
|
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
39 |
def todatetime(somedate): |
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
40 |
"""return a date from a date (leaving unchanged) or a datetime""" |
1751 | 41 |
# take care, datetime is a subclass of date |
42 |
if isinstance(somedate, datetime): |
|
43 |
return somedate |
|
44 |
assert isinstance(somedate, date), repr(somedate) |
|
45 |
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
|
46 |
|
2009
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
47 |
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
|
48 |
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
|
49 |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
50 |
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
|
51 |
ONEWEEK = timedelta(days=7) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
52 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
53 |
def days_in_month(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
54 |
return monthrange(date_.year, date_.month)[1] |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
55 |
|
3851
3a18a0a24411
added days_in_year function
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3838
diff
changeset
|
56 |
def days_in_year(date_): |
3a18a0a24411
added days_in_year function
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3838
diff
changeset
|
57 |
feb = pydatetime.date(date_.year, 2, 1) |
3a18a0a24411
added days_in_year function
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3838
diff
changeset
|
58 |
if days_in_month(feb) == 29: |
3a18a0a24411
added days_in_year function
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3838
diff
changeset
|
59 |
return 366 |
3a18a0a24411
added days_in_year function
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3838
diff
changeset
|
60 |
else: |
3a18a0a24411
added days_in_year function
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3838
diff
changeset
|
61 |
return 365 |
3a18a0a24411
added days_in_year function
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3838
diff
changeset
|
62 |
|
1697
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
return date_ |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
68 |
|
1697
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
69 |
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
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
return date_ |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
74 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
75 |
def first_day(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
76 |
return date(date_.year, date_.month, 1) |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
77 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
78 |
def last_day(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
79 |
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
|
80 |
|
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
81 |
def date_range(begin, end, incday=None, incmonth=None): |
0 | 82 |
"""yields each date between begin and end |
83 |
:param begin: the start date |
|
84 |
:param end: the end date |
|
85 |
: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
|
86 |
one day. |
0 | 87 |
:param include: None (means no exclusion) or a function taking a |
88 |
date as parameter, and returning True if the date |
|
89 |
should be included. |
|
90 |
""" |
|
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
91 |
assert not (incday and incmonth) |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
92 |
begin = todate(begin) |
1711
182536159750
Make sure end is datetime.date.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1705
diff
changeset
|
93 |
end = todate(end) |
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
94 |
if incmonth: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
95 |
while begin < end: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
96 |
begin = next_month(begin, incmonth) |
1132 | 97 |
yield begin |
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
98 |
else: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
99 |
if not incday: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
100 |
incr = ONEDAY |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
101 |
else: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
102 |
incr = timedelta(incday) |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
103 |
while begin <= end: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
104 |
yield begin |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
105 |
begin += incr |
0 | 106 |
|
709 | 107 |
def ustrftime(date, fmt='%Y-%m-%d'): |
108 |
"""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
|
109 |
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
|
110 |
|
709 | 111 |
encoding is guessed by locale.getpreferredencoding() |
112 |
""" |
|
113 |
# date format may depend on the locale |
|
114 |
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
|
115 |
return unicode(date.strftime(str(fmt)), encoding) |
709 | 116 |
|
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
|
117 |
|
3146
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
118 |
if sys.version_info[:2] < (2, 5): |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
119 |
def make_uid(key): |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
120 |
"""forge a unique identifier |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
121 |
not that unique on win32""" |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
122 |
msg = str(key) + "%.10f" % time() + str(randint(0, 1000000)) |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
123 |
return md5(msg).hexdigest() |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
124 |
else: |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
125 |
from uuid import uuid4 |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
126 |
def make_uid(key): |
3364
039d1694f36d
fix uid generation for py >= 2.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
127 |
# remove dash, generated uid are used as identifier sometimes (sql table |
039d1694f36d
fix uid generation for py >= 2.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
128 |
# names at least) |
039d1694f36d
fix uid generation for py >= 2.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
129 |
return str(key) + str(uuid4()).replace('-', '') |
039d1694f36d
fix uid generation for py >= 2.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
130 |
|
0 | 131 |
|
132 |
def dump_class(cls, clsname): |
|
133 |
"""create copy of a class by creating an empty class inheriting |
|
134 |
from the given cls. |
|
135 |
||
136 |
Those class will be used as place holder for attribute and relation |
|
137 |
description |
|
138 |
""" |
|
139 |
# type doesn't accept unicode name |
|
140 |
# return type.__new__(type, str(clsname), (cls,), {}) |
|
141 |
# __autogenerated__ attribute is just a marker |
|
142 |
return type(str(clsname), (cls,), {'__autogenerated__': True}) |
|
143 |
||
144 |
||
145 |
def merge_dicts(dict1, dict2): |
|
146 |
"""update a copy of `dict1` with `dict2`""" |
|
147 |
dict1 = dict(dict1) |
|
148 |
dict1.update(dict2) |
|
149 |
return dict1 |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
150 |
|
0 | 151 |
|
3882
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
152 |
# use networkX instead ? |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
153 |
# http://networkx.lanl.gov/reference/algorithms.traversal.html#module-networkx.algorithms.traversal.astar |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
154 |
def transitive_closure_of(entity, relname, _seen=None): |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
155 |
if _seen is None: |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
156 |
_seen = set() |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
157 |
_seen.add(entity.eid) |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
158 |
yield entity |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
159 |
for child in getattr(entity, relname): |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
160 |
if child.eid in _seen: |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
161 |
continue |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
162 |
for subchild in transitive_closure_of(child, relname, _seen): |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
163 |
yield subchild |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
164 |
|
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
165 |
|
0 | 166 |
class SizeConstrainedList(list): |
167 |
"""simple list that makes sure the list does not get bigger |
|
168 |
than a given size. |
|
169 |
||
170 |
when the list is full and a new element is added, the first |
|
171 |
element of the list is removed before appending the new one |
|
172 |
||
173 |
>>> l = SizeConstrainedList(2) |
|
174 |
>>> l.append(1) |
|
175 |
>>> l.append(2) |
|
176 |
>>> l |
|
177 |
[1, 2] |
|
178 |
>>> l.append(3) |
|
179 |
[2, 3] |
|
180 |
""" |
|
181 |
def __init__(self, maxsize): |
|
182 |
self.maxsize = maxsize |
|
183 |
||
184 |
def append(self, element): |
|
185 |
if len(self) == self.maxsize: |
|
186 |
del self[0] |
|
187 |
super(SizeConstrainedList, self).append(element) |
|
188 |
||
189 |
def extend(self, sequence): |
|
190 |
super(SizeConstrainedList, self).extend(sequence) |
|
191 |
keepafter = len(self) - self.maxsize |
|
192 |
if keepafter > 0: |
|
193 |
del self[:keepafter] |
|
194 |
||
195 |
__iadd__ = extend |
|
196 |
||
197 |
||
198 |
class UStringIO(list): |
|
199 |
"""a file wrapper which automatically encode unicode string to an encoding |
|
200 |
specifed in the constructor |
|
201 |
""" |
|
202 |
||
203 |
def __nonzero__(self): |
|
204 |
return True |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
205 |
|
0 | 206 |
def write(self, value): |
207 |
assert isinstance(value, unicode), u"unicode required not %s : %s"\ |
|
208 |
% (type(value).__name__, repr(value)) |
|
209 |
self.append(value) |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
210 |
|
0 | 211 |
def getvalue(self): |
212 |
return u''.join(self) |
|
213 |
||
214 |
def __repr__(self): |
|
215 |
return '<%s at %#x>' % (self.__class__.__name__, id(self)) |
|
216 |
||
217 |
||
218 |
class HTMLHead(UStringIO): |
|
219 |
"""wraps HTML header's stream |
|
220 |
||
221 |
Request objects use a HTMLHead instance to ease adding of |
|
222 |
javascripts and stylesheets |
|
223 |
""" |
|
224 |
js_unload_code = u'jQuery(window).unload(unloadPageData);' |
|
225 |
||
226 |
def __init__(self): |
|
227 |
super(HTMLHead, self).__init__() |
|
228 |
self.jsvars = [] |
|
229 |
self.jsfiles = [] |
|
230 |
self.cssfiles = [] |
|
231 |
self.ie_cssfiles = [] |
|
232 |
self.post_inlined_scripts = [] |
|
233 |
self.pagedata_unload = False |
|
234 |
||
235 |
||
236 |
def add_raw(self, rawheader): |
|
237 |
self.write(rawheader) |
|
238 |
||
3838
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
239 |
def define_var(self, var, value, override=True): |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
240 |
"""adds a javascript var declaration / assginment in the header |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
241 |
|
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
242 |
:param var: the variable name |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
243 |
:param value: the variable value (as a raw python value, |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
244 |
it will be jsonized later) |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
245 |
:param override: if False, don't set the variable value if the variable |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
246 |
is already defined. Default is True. |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
247 |
""" |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
248 |
self.jsvars.append( (var, value, override) ) |
0 | 249 |
|
250 |
def add_post_inline_script(self, content): |
|
251 |
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
|
252 |
|
2258
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
253 |
def add_onload(self, jscode, jsoncall=False): |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
254 |
if jsoncall: |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
255 |
self.add_post_inline_script(u"""jQuery(CubicWeb).bind('ajax-loaded', function(event) { |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
256 |
%s |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
257 |
});""" % jscode) |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
258 |
else: |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
259 |
self.add_post_inline_script(u"""jQuery(document).ready(function () { |
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
|
260 |
%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
|
261 |
});""" % jscode) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
262 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
263 |
|
0 | 264 |
def add_js(self, jsfile): |
265 |
"""adds `jsfile` to the list of javascripts used in the webpage |
|
266 |
||
267 |
This function checks if the file has already been added |
|
268 |
:param jsfile: the script's URL |
|
269 |
""" |
|
270 |
if jsfile not in self.jsfiles: |
|
271 |
self.jsfiles.append(jsfile) |
|
272 |
||
273 |
def add_css(self, cssfile, media): |
|
274 |
"""adds `cssfile` to the list of javascripts used in the webpage |
|
275 |
||
276 |
This function checks if the file has already been added |
|
277 |
:param cssfile: the stylesheet's URL |
|
278 |
""" |
|
279 |
if (cssfile, media) not in self.cssfiles: |
|
280 |
self.cssfiles.append( (cssfile, media) ) |
|
281 |
||
282 |
def add_ie_css(self, cssfile, media='all'): |
|
283 |
"""registers some IE specific CSS""" |
|
284 |
if (cssfile, media) not in self.ie_cssfiles: |
|
285 |
self.ie_cssfiles.append( (cssfile, media) ) |
|
286 |
||
287 |
def add_unload_pagedata(self): |
|
288 |
"""registers onunload callback to clean page data on server""" |
|
289 |
if not self.pagedata_unload: |
|
290 |
self.post_inlined_scripts.append(self.js_unload_code) |
|
291 |
self.pagedata_unload = True |
|
292 |
||
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
|
293 |
def getvalue(self, skiphead=False): |
0 | 294 |
"""reimplement getvalue to provide a consistent (and somewhat browser |
295 |
optimzed cf. http://stevesouders.com/cuzillion) order in external |
|
296 |
resources declaration |
|
297 |
""" |
|
298 |
w = self.write |
|
299 |
# 1/ variable declaration if any |
|
300 |
if self.jsvars: |
|
2554
3b6a6d2f9d7e
make jsvars declaration xhtml and html compatible
Fabrice <fabrice@secondweb.fr>
parents:
2361
diff
changeset
|
301 |
w(u'<script type="text/javascript"><!--//--><![CDATA[//><!--\n') |
3838
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
302 |
for var, value, override in self.jsvars: |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
303 |
vardecl = u'%s = %s;' % (var, dumps(value)) |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
304 |
if not override: |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
305 |
vardecl = (u'if (typeof %s == "undefined") {%s}' % |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
306 |
(var, vardecl)) |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
307 |
w(vardecl + u'\n') |
2554
3b6a6d2f9d7e
make jsvars declaration xhtml and html compatible
Fabrice <fabrice@secondweb.fr>
parents:
2361
diff
changeset
|
308 |
w(u'//--><!]]></script>\n') |
0 | 309 |
# 2/ css files |
310 |
for cssfile, media in self.cssfiles: |
|
311 |
w(u'<link rel="stylesheet" type="text/css" media="%s" href="%s"/>\n' % |
|
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
312 |
(media, xml_escape(cssfile))) |
0 | 313 |
# 3/ ie css if necessary |
314 |
if self.ie_cssfiles: |
|
315 |
w(u'<!--[if lt IE 8]>\n') |
|
316 |
for cssfile, media in self.ie_cssfiles: |
|
317 |
w(u'<link rel="stylesheet" type="text/css" media="%s" href="%s"/>\n' % |
|
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
318 |
(media, xml_escape(cssfile))) |
0 | 319 |
w(u'<![endif]--> \n') |
320 |
# 4/ js files |
|
321 |
for jsfile in self.jsfiles: |
|
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
322 |
w(u'<script type="text/javascript" src="%s"></script>\n' % |
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
323 |
xml_escape(jsfile)) |
0 | 324 |
# 5/ post inlined scripts (i.e. scripts depending on other JS files) |
325 |
if self.post_inlined_scripts: |
|
326 |
w(u'<script type="text/javascript">\n') |
|
327 |
w(u'\n\n'.join(self.post_inlined_scripts)) |
|
328 |
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
|
329 |
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
|
330 |
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
|
331 |
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
|
332 |
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
|
333 |
|
0 | 334 |
|
335 |
class HTMLStream(object): |
|
336 |
"""represents a HTML page. |
|
337 |
||
338 |
This is used my main templates so that HTML headers can be added |
|
339 |
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
|
340 |
|
0 | 341 |
HTMLStream uses the (U)StringIO interface to be compliant with |
342 |
existing code. |
|
343 |
""" |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
344 |
|
0 | 345 |
def __init__(self, req): |
346 |
# stream for <head> |
|
347 |
self.head = req.html_headers |
|
348 |
# main stream |
|
349 |
self.body = UStringIO() |
|
350 |
self.doctype = u'' |
|
351 |
# xmldecl and html opening tag |
|
352 |
self.xmldecl = u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding |
|
353 |
self.htmltag = u'<html xmlns="http://www.w3.org/1999/xhtml" ' \ |
|
354 |
'xmlns:cubicweb="http://www.logilab.org/2008/cubicweb" ' \ |
|
355 |
'xml:lang="%s" lang="%s">' % (req.lang, req.lang) |
|
3094
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3091
diff
changeset
|
356 |
# keep main_stream's reference on req for easier text/html demoting |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3091
diff
changeset
|
357 |
req.main_stream = self |
0 | 358 |
|
359 |
def write(self, data): |
|
360 |
"""StringIO interface: this method will be assigned to self.w |
|
361 |
""" |
|
362 |
self.body.write(data) |
|
363 |
||
364 |
def getvalue(self): |
|
365 |
"""writes HTML headers, closes </head> tag and writes HTML body""" |
|
366 |
return u'%s\n%s\n%s\n%s\n%s\n</html>' % (self.xmldecl, self.doctype, |
|
367 |
self.htmltag, |
|
368 |
self.head.getvalue(), |
|
369 |
self.body.getvalue()) |
|
370 |
||
371 |
||
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
372 |
def can_do_pdf_conversion(__answer=[None]): |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
373 |
"""pdf conversion depends on |
3789
fb22b55f80f8
update docstring, xml escape exception description
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3372
diff
changeset
|
374 |
* pysixt (python package) |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
375 |
* fop 0.9x |
0 | 376 |
""" |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
377 |
if __answer[0] is not None: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
378 |
return __answer[0] |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
379 |
try: |
2936
f35b64718d02
follow name change, fix pdf template registration, add debian package tracking
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2935
diff
changeset
|
380 |
import pysixt |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
381 |
except ImportError: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
382 |
__answer[0] = False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
383 |
return False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
384 |
from subprocess import Popen, STDOUT |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
385 |
import os |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
386 |
try: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
387 |
Popen(['/usr/bin/fop', '-q'], |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
388 |
stdout=open(os.devnull, 'w'), |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
389 |
stderr=STDOUT) |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
390 |
except OSError, e: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
391 |
print e |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
392 |
__answer[0] = False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
393 |
return False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
394 |
__answer[0] = True |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
395 |
return True |
3231
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
396 |
|
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
397 |
try: |
3372 | 398 |
# may not be there if cubicweb-web not installed |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
399 |
from simplejson import dumps, JSONEncoder |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
400 |
except ImportError: |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
401 |
pass |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
402 |
else: |
3231
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
403 |
|
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
404 |
class CubicWebJsonEncoder(JSONEncoder): |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
405 |
"""define a simplejson encoder to be able to encode yams std types""" |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
406 |
def default(self, obj): |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
407 |
if isinstance(obj, pydatetime.datetime): |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
408 |
return obj.strftime('%Y/%m/%d %H:%M:%S') |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
409 |
elif isinstance(obj, pydatetime.date): |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
410 |
return obj.strftime('%Y/%m/%d') |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
411 |
elif isinstance(obj, pydatetime.time): |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
412 |
return obj.strftime('%H:%M:%S') |
3902
a0efb0326021
fix #499846 by handling timedelta type in json serialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3882
diff
changeset
|
413 |
elif isinstance(obj, pydatetime.timedelta): |
a0efb0326021
fix #499846 by handling timedelta type in json serialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3882
diff
changeset
|
414 |
return '%10d.%s' % (obj.days, obj.seconds) |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
415 |
elif isinstance(obj, decimal.Decimal): |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
416 |
return float(obj) |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
417 |
try: |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
418 |
return JSONEncoder.default(self, obj) |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
419 |
except TypeError: |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
420 |
# we never ever want to fail because of an unknown type, |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
421 |
# just return None in those cases. |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
422 |
return None |