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