author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Fri, 16 Apr 2010 19:45:11 +0200 | |
branch | stable |
changeset 5320 | 848e92bb81cc |
parent 5077 | dc448c9ad9dd |
child 5328 | c51e8f62652a |
child 5377 | 84d14ddfae13 |
permissions | -rw-r--r-- |
4511 | 1 |
"""Some utilities for CubicWeb server/clients. |
0 | 2 |
|
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4000
diff
changeset
|
4 |
:copyright: 2001-2010 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 |
||
4952
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
10 |
import os |
3316 | 11 |
import sys |
12 |
import decimal |
|
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
13 |
import datetime |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
14 |
import random |
5069
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
15 |
from itertools import repeat |
4833
41a78fb4107c
3.7 depends on python >= 2.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4693
diff
changeset
|
16 |
from uuid import uuid4 |
4830
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
17 |
from warnings import warn |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
18 |
|
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
19 |
from logilab.mtconverter import xml_escape |
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
20 |
from logilab.common.deprecation import deprecated |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
21 |
|
4830
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
22 |
_MARKER = object() |
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
23 |
|
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
|
24 |
# initialize random seed from current time |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
25 |
random.seed() |
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 |
|
4880
fa6671e83639
[utils] make_uid imrovment: using .hex already remove '-', make key argument optional, document it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4879
diff
changeset
|
27 |
def make_uid(key=None): |
fa6671e83639
[utils] make_uid imrovment: using .hex already remove '-', make key argument optional, document it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4879
diff
changeset
|
28 |
"""Return a unique identifier string. |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
29 |
|
4880
fa6671e83639
[utils] make_uid imrovment: using .hex already remove '-', make key argument optional, document it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4879
diff
changeset
|
30 |
if specified, `key` is used to prefix the generated uid so it can be used |
fa6671e83639
[utils] make_uid imrovment: using .hex already remove '-', make key argument optional, document it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4879
diff
changeset
|
31 |
for instance as a DOM id or as sql table names. |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
32 |
|
4880
fa6671e83639
[utils] make_uid imrovment: using .hex already remove '-', make key argument optional, document it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4879
diff
changeset
|
33 |
See uuid.uuid4 documentation for the shape of the generated identifier, but |
fa6671e83639
[utils] make_uid imrovment: using .hex already remove '-', make key argument optional, document it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4879
diff
changeset
|
34 |
this is basicallly a 32 bits hexadecimal string. |
fa6671e83639
[utils] make_uid imrovment: using .hex already remove '-', make key argument optional, document it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4879
diff
changeset
|
35 |
""" |
fa6671e83639
[utils] make_uid imrovment: using .hex already remove '-', make key argument optional, document it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4879
diff
changeset
|
36 |
if key is None: |
fa6671e83639
[utils] make_uid imrovment: using .hex already remove '-', make key argument optional, document it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4879
diff
changeset
|
37 |
return uuid4().hex |
fa6671e83639
[utils] make_uid imrovment: using .hex already remove '-', make key argument optional, document it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4879
diff
changeset
|
38 |
return str(key) + uuid4().hex |
3364
039d1694f36d
fix uid generation for py >= 2.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
39 |
|
0 | 40 |
|
41 |
def dump_class(cls, clsname): |
|
42 |
"""create copy of a class by creating an empty class inheriting |
|
43 |
from the given cls. |
|
44 |
||
45 |
Those class will be used as place holder for attribute and relation |
|
46 |
description |
|
47 |
""" |
|
48 |
# type doesn't accept unicode name |
|
49 |
# return type.__new__(type, str(clsname), (cls,), {}) |
|
50 |
# __autogenerated__ attribute is just a marker |
|
4881
39fc30001a20
[utils] improve dump_class by copying back original class doc and module name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4880
diff
changeset
|
51 |
return type(str(clsname), (cls,), {'__autogenerated__': True, |
39fc30001a20
[utils] improve dump_class by copying back original class doc and module name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4880
diff
changeset
|
52 |
'__doc__': cls.__doc__, |
39fc30001a20
[utils] improve dump_class by copying back original class doc and module name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4880
diff
changeset
|
53 |
'__module__': cls.__module__}) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
54 |
|
0 | 55 |
|
3882
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
56 |
# use networkX instead ? |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
57 |
# 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
|
58 |
def transitive_closure_of(entity, relname, _seen=None): |
4317
b2093f8f0493
some docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
59 |
"""return transitive closure *for the subgraph starting from the given |
b2093f8f0493
some docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
60 |
entity* (eg 'parent' entities are not included in the results) |
b2093f8f0493
some docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
61 |
""" |
3882
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
62 |
if _seen is None: |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
63 |
_seen = set() |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
64 |
_seen.add(entity.eid) |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
65 |
yield entity |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
66 |
for child in getattr(entity, relname): |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
67 |
if child.eid in _seen: |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
68 |
continue |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
69 |
for subchild in transitive_closure_of(child, relname, _seen): |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
70 |
yield subchild |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
71 |
|
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
72 |
|
0 | 73 |
class SizeConstrainedList(list): |
74 |
"""simple list that makes sure the list does not get bigger |
|
75 |
than a given size. |
|
76 |
||
77 |
when the list is full and a new element is added, the first |
|
78 |
element of the list is removed before appending the new one |
|
79 |
||
80 |
>>> l = SizeConstrainedList(2) |
|
81 |
>>> l.append(1) |
|
82 |
>>> l.append(2) |
|
83 |
>>> l |
|
84 |
[1, 2] |
|
85 |
>>> l.append(3) |
|
86 |
[2, 3] |
|
87 |
""" |
|
88 |
def __init__(self, maxsize): |
|
89 |
self.maxsize = maxsize |
|
90 |
||
91 |
def append(self, element): |
|
92 |
if len(self) == self.maxsize: |
|
93 |
del self[0] |
|
94 |
super(SizeConstrainedList, self).append(element) |
|
95 |
||
96 |
def extend(self, sequence): |
|
97 |
super(SizeConstrainedList, self).extend(sequence) |
|
98 |
keepafter = len(self) - self.maxsize |
|
99 |
if keepafter > 0: |
|
100 |
del self[:keepafter] |
|
101 |
||
102 |
__iadd__ = extend |
|
103 |
||
104 |
||
5069
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
105 |
class RepeatList(object): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
106 |
"""fake a list with the same element in each row""" |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
107 |
__slots__ = ('_size', '_item') |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
108 |
def __init__(self, size, item): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
109 |
self._size = size |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
110 |
self._item = item |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
111 |
def __len__(self): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
112 |
return self._size |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
113 |
def __nonzero__(self): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
114 |
return self._size |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
115 |
def __iter__(self): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
116 |
return repeat(self._item, self._size) |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
117 |
def __getitem__(self, index): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
118 |
return self._item |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
119 |
def __getslice__(self, i, j): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
120 |
# XXX could be more efficient, but do we bother? |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
121 |
return ([self._item] * self._size)[i:j] |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
122 |
def __add__(self, other): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
123 |
if isinstance(other, RepeatList): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
124 |
if other._item == self._item: |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
125 |
return RepeatList(self._size + other._size, self._item) |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
126 |
return ([self._item] * self._size) + other[:] |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
127 |
return ([self._item] * self._size) + other |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
128 |
def __radd__(self, other): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
129 |
if isinstance(other, RepeatList): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
130 |
if other._item == self._item: |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
131 |
return RepeatList(self._size + other._size, self._item) |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
132 |
return other[:] + ([self._item] * self._size) |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
133 |
return other[:] + ([self._item] * self._size) |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
134 |
def __eq__(self, other): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
135 |
if isinstance(other, RepeatList): |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
136 |
return other._size == self.size and other._item == self.item |
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
137 |
return self[:] == other |
5077
dc448c9ad9dd
[rset] cubes such as iliane hi-jack rset description by poping some lines from it. Handle this on RepeatList
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5069
diff
changeset
|
138 |
def pop(self, i): |
dc448c9ad9dd
[rset] cubes such as iliane hi-jack rset description by poping some lines from it. Handle this on RepeatList
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5069
diff
changeset
|
139 |
self._size -= 1 |
5069
135c5d7b89d0
[querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4961
diff
changeset
|
140 |
|
0 | 141 |
class UStringIO(list): |
142 |
"""a file wrapper which automatically encode unicode string to an encoding |
|
143 |
specifed in the constructor |
|
144 |
""" |
|
145 |
||
146 |
def __nonzero__(self): |
|
147 |
return True |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
148 |
|
0 | 149 |
def write(self, value): |
150 |
assert isinstance(value, unicode), u"unicode required not %s : %s"\ |
|
151 |
% (type(value).__name__, repr(value)) |
|
152 |
self.append(value) |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
153 |
|
0 | 154 |
def getvalue(self): |
155 |
return u''.join(self) |
|
156 |
||
157 |
def __repr__(self): |
|
158 |
return '<%s at %#x>' % (self.__class__.__name__, id(self)) |
|
159 |
||
160 |
||
161 |
class HTMLHead(UStringIO): |
|
162 |
"""wraps HTML header's stream |
|
163 |
||
164 |
Request objects use a HTMLHead instance to ease adding of |
|
165 |
javascripts and stylesheets |
|
166 |
""" |
|
167 |
js_unload_code = u'jQuery(window).unload(unloadPageData);' |
|
168 |
||
169 |
def __init__(self): |
|
170 |
super(HTMLHead, self).__init__() |
|
171 |
self.jsvars = [] |
|
172 |
self.jsfiles = [] |
|
173 |
self.cssfiles = [] |
|
174 |
self.ie_cssfiles = [] |
|
175 |
self.post_inlined_scripts = [] |
|
176 |
self.pagedata_unload = False |
|
177 |
||
178 |
||
179 |
def add_raw(self, rawheader): |
|
180 |
self.write(rawheader) |
|
181 |
||
3838
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
182 |
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
|
183 |
"""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
|
184 |
|
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
185 |
: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
|
186 |
: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
|
187 |
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
|
188 |
: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
|
189 |
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
|
190 |
""" |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
191 |
self.jsvars.append( (var, value, override) ) |
0 | 192 |
|
193 |
def add_post_inline_script(self, content): |
|
194 |
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
|
195 |
|
4830
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
196 |
def add_onload(self, jscode, jsoncall=_MARKER): |
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
197 |
if jsoncall is not _MARKER: |
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
198 |
warn('[3.7] specifying jsoncall is not needed anymore', |
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
199 |
DeprecationWarning, stacklevel=2) |
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
200 |
self.add_post_inline_script(u"""jQuery(CubicWeb).one('server-response', function(event) { |
4903
627fcd90e08f
fix bad conflict resolution
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4899
diff
changeset
|
201 |
%s});""" % jscode) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
202 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
203 |
|
0 | 204 |
def add_js(self, jsfile): |
205 |
"""adds `jsfile` to the list of javascripts used in the webpage |
|
206 |
||
207 |
This function checks if the file has already been added |
|
208 |
:param jsfile: the script's URL |
|
209 |
""" |
|
210 |
if jsfile not in self.jsfiles: |
|
211 |
self.jsfiles.append(jsfile) |
|
212 |
||
213 |
def add_css(self, cssfile, media): |
|
214 |
"""adds `cssfile` to the list of javascripts used in the webpage |
|
215 |
||
216 |
This function checks if the file has already been added |
|
217 |
:param cssfile: the stylesheet's URL |
|
218 |
""" |
|
219 |
if (cssfile, media) not in self.cssfiles: |
|
220 |
self.cssfiles.append( (cssfile, media) ) |
|
221 |
||
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4859
diff
changeset
|
222 |
def add_ie_css(self, cssfile, media='all', iespec=u'[if lt IE 8]'): |
0 | 223 |
"""registers some IE specific CSS""" |
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4859
diff
changeset
|
224 |
if (cssfile, media, iespec) not in self.ie_cssfiles: |
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4859
diff
changeset
|
225 |
self.ie_cssfiles.append( (cssfile, media, iespec) ) |
0 | 226 |
|
227 |
def add_unload_pagedata(self): |
|
228 |
"""registers onunload callback to clean page data on server""" |
|
229 |
if not self.pagedata_unload: |
|
230 |
self.post_inlined_scripts.append(self.js_unload_code) |
|
231 |
self.pagedata_unload = True |
|
232 |
||
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
|
233 |
def getvalue(self, skiphead=False): |
0 | 234 |
"""reimplement getvalue to provide a consistent (and somewhat browser |
235 |
optimzed cf. http://stevesouders.com/cuzillion) order in external |
|
236 |
resources declaration |
|
237 |
""" |
|
238 |
w = self.write |
|
239 |
# 1/ variable declaration if any |
|
240 |
if self.jsvars: |
|
2554
3b6a6d2f9d7e
make jsvars declaration xhtml and html compatible
Fabrice <fabrice@secondweb.fr>
parents:
2361
diff
changeset
|
241 |
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
|
242 |
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
|
243 |
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
|
244 |
if not override: |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
245 |
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
|
246 |
(var, vardecl)) |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
247 |
w(vardecl + u'\n') |
2554
3b6a6d2f9d7e
make jsvars declaration xhtml and html compatible
Fabrice <fabrice@secondweb.fr>
parents:
2361
diff
changeset
|
248 |
w(u'//--><!]]></script>\n') |
0 | 249 |
# 2/ css files |
250 |
for cssfile, media in self.cssfiles: |
|
251 |
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
|
252 |
(media, xml_escape(cssfile))) |
0 | 253 |
# 3/ ie css if necessary |
254 |
if self.ie_cssfiles: |
|
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4859
diff
changeset
|
255 |
for cssfile, media, iespec in self.ie_cssfiles: |
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4859
diff
changeset
|
256 |
w(u'<!--%s>\n' % iespec) |
0 | 257 |
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
|
258 |
(media, xml_escape(cssfile))) |
0 | 259 |
w(u'<![endif]--> \n') |
260 |
# 4/ js files |
|
261 |
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
|
262 |
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
|
263 |
xml_escape(jsfile)) |
0 | 264 |
# 5/ post inlined scripts (i.e. scripts depending on other JS files) |
265 |
if self.post_inlined_scripts: |
|
266 |
w(u'<script type="text/javascript">\n') |
|
267 |
w(u'\n\n'.join(self.post_inlined_scripts)) |
|
268 |
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
|
269 |
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
|
270 |
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
|
271 |
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
|
272 |
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
|
273 |
|
0 | 274 |
|
275 |
class HTMLStream(object): |
|
276 |
"""represents a HTML page. |
|
277 |
||
278 |
This is used my main templates so that HTML headers can be added |
|
279 |
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
|
280 |
|
0 | 281 |
HTMLStream uses the (U)StringIO interface to be compliant with |
282 |
existing code. |
|
283 |
""" |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
284 |
|
0 | 285 |
def __init__(self, req): |
286 |
# stream for <head> |
|
287 |
self.head = req.html_headers |
|
288 |
# main stream |
|
289 |
self.body = UStringIO() |
|
290 |
self.doctype = u'' |
|
291 |
# xmldecl and html opening tag |
|
292 |
self.xmldecl = u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding |
|
293 |
self.htmltag = u'<html xmlns="http://www.w3.org/1999/xhtml" ' \ |
|
294 |
'xmlns:cubicweb="http://www.logilab.org/2008/cubicweb" ' \ |
|
295 |
'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
|
296 |
# 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
|
297 |
req.main_stream = self |
0 | 298 |
|
299 |
def write(self, data): |
|
300 |
"""StringIO interface: this method will be assigned to self.w |
|
301 |
""" |
|
302 |
self.body.write(data) |
|
303 |
||
304 |
def getvalue(self): |
|
305 |
"""writes HTML headers, closes </head> tag and writes HTML body""" |
|
306 |
return u'%s\n%s\n%s\n%s\n%s\n</html>' % (self.xmldecl, self.doctype, |
|
307 |
self.htmltag, |
|
308 |
self.head.getvalue(), |
|
309 |
self.body.getvalue()) |
|
310 |
||
311 |
||
4952
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
312 |
def _pdf_conversion_availability(): |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
313 |
try: |
2936
f35b64718d02
follow name change, fix pdf template registration, add debian package tracking
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2935
diff
changeset
|
314 |
import pysixt |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
315 |
except ImportError: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
316 |
return False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
317 |
from subprocess import Popen, STDOUT |
4952
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
318 |
if not os.path.isfile('/usr/bin/fop'): |
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
319 |
return False |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
320 |
try: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
321 |
Popen(['/usr/bin/fop', '-q'], |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
322 |
stdout=open(os.devnull, 'w'), |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
323 |
stderr=STDOUT) |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
324 |
except OSError, e: |
4952
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
325 |
getLogger('cubicweb').info('fop not usable (%s)', e) |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
326 |
return False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
327 |
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
|
328 |
|
4952
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
329 |
def can_do_pdf_conversion(__answer_cache=[]): |
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
330 |
"""pdf conversion depends on |
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
331 |
* pysixt (python package) |
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
332 |
* fop 0.9x |
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
333 |
|
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
334 |
NOTE: actual check is done by _pdf_conversion_availability and |
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
335 |
result is cached |
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
336 |
""" |
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
337 |
if not __answer_cache: # first time, not in cache |
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
338 |
__answer_cache.append(_pdf_conversion_availability()) |
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
339 |
return __answer_cache[0] |
f32dcf3925d4
[web] get rid of spurious '[Errno 2] No such file or directory' on load
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4860
diff
changeset
|
340 |
|
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
341 |
try: |
3372 | 342 |
# 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
|
343 |
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
|
344 |
except ImportError: |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
345 |
pass |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
346 |
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
|
347 |
|
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
348 |
class CubicWebJsonEncoder(JSONEncoder): |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
349 |
"""define a simplejson encoder to be able to encode yams std types""" |
5320
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
350 |
|
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
351 |
# _iterencode is the only entry point I've found to use a custom encode |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
352 |
# hook early enough: .default() is called if nothing else matched before, |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
353 |
# .iterencode() is called once on the main structure to encode and then |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
354 |
# never gets called again. |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
355 |
# For the record, our main use case is in FormValidateController with: |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
356 |
# dumps((status, args, entity), cls=CubicWebJsonEncoder) |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
357 |
# where we want all the entity attributes, including eid, to be part |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
358 |
# of the json object dumped. |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
359 |
# This would have once more been easier if Entity didn't extend dict. |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
360 |
def _iterencode(self, obj, markers=None): |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
361 |
if hasattr(obj, '__json_encode__'): |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
362 |
obj = obj.__json_encode__() |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
363 |
return JSONEncoder._iterencode(self, obj, markers) |
848e92bb81cc
[json] provide a custom json encoder for entities that includes eid in the json object
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5077
diff
changeset
|
364 |
|
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
365 |
def default(self, obj): |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
366 |
if isinstance(obj, datetime.datetime): |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
367 |
return obj.strftime('%Y/%m/%d %H:%M:%S') |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
368 |
elif isinstance(obj, datetime.date): |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
369 |
return obj.strftime('%Y/%m/%d') |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
370 |
elif isinstance(obj, datetime.time): |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
371 |
return obj.strftime('%H:%M:%S') |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
372 |
elif isinstance(obj, datetime.timedelta): |
4000
4b16a7c01a25
fix jsonization of interval
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3902
diff
changeset
|
373 |
return (obj.days * 24 * 60 * 60) + obj.seconds |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
374 |
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
|
375 |
return float(obj) |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
376 |
try: |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
377 |
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
|
378 |
except TypeError: |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
379 |
# 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
|
380 |
# 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
|
381 |
return None |
4652
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
382 |
|
4879
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
383 |
|
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
384 |
@deprecated('[3.7] merge_dicts is deprecated') |
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
385 |
def merge_dicts(dict1, dict2): |
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
386 |
"""update a copy of `dict1` with `dict2`""" |
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
387 |
dict1 = dict(dict1) |
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
388 |
dict1.update(dict2) |
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
389 |
return dict1 |
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
390 |
|
4652
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
391 |
from logilab.common import date |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
392 |
_THIS_MOD_NS = globals() |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
393 |
for funcname in ('date_range', 'todate', 'todatetime', 'datetime2ticks', |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
394 |
'days_in_month', 'days_in_year', 'previous_month', |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
395 |
'next_month', 'first_day', 'last_day', 'ustrftime', |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
396 |
'strptime'): |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
397 |
msg = '[3.6] %s has been moved to logilab.common.date' % funcname |
7c41eea7fc30
restore 3.5 compat for date functions moved to lgc.date in 3.6 (closes #716055)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4511
diff
changeset
|
398 |
_THIS_MOD_NS[funcname] = deprecated(msg)(getattr(date, funcname)) |