author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 10 Jun 2011 17:41:41 +0200 | |
changeset 7496 | 0c75ed374b06 |
parent 7423 | 598a4f051259 |
child 7569 | 02c338197322 |
permissions | -rw-r--r-- |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
6044
9e48ebd5400c
[view] use the new magic js object to generate javascript func call for auto reload user callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5940
diff
changeset
|
18 |
"""Some utilities for CubicWeb server/clients.""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
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
|
22 |
import os |
3316 | 23 |
import sys |
24 |
import decimal |
|
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
25 |
import datetime |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
26 |
import random |
6067
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
27 |
from inspect import getargspec |
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
|
28 |
from itertools import repeat |
4833
41a78fb4107c
3.7 depends on python >= 2.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4693
diff
changeset
|
29 |
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
|
30 |
from warnings import warn |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
31 |
|
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
32 |
from logilab.mtconverter import xml_escape |
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
33 |
from logilab.common.deprecation import deprecated |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
34 |
|
4830
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
35 |
_MARKER = object() |
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
36 |
|
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
|
37 |
# initialize random seed from current time |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
38 |
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
|
39 |
|
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
|
40 |
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
|
41 |
"""Return a unique identifier string. |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
42 |
|
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
|
43 |
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
|
44 |
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
|
45 |
|
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
|
46 |
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
|
47 |
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
|
48 |
""" |
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
|
49 |
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
|
50 |
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
|
51 |
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
|
52 |
|
0 | 53 |
|
6067
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
54 |
def support_args(callable, *argnames): |
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
55 |
"""return true if the callable support given argument names""" |
6913
ab85124dc0eb
[utils] support_args now support classes, searching if __init__ supports the arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6684
diff
changeset
|
56 |
if isinstance(callable, type): |
ab85124dc0eb
[utils] support_args now support classes, searching if __init__ supports the arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6684
diff
changeset
|
57 |
callable = callable.__init__ |
6067
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
58 |
argspec = getargspec(callable) |
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
59 |
if argspec[2]: |
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
60 |
return True |
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
61 |
for argname in argnames: |
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
62 |
if argname not in argspec[0]: |
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
63 |
return False |
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
64 |
return True |
0 | 65 |
|
6141
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
66 |
|
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
67 |
class wrap_on_write(object): |
6580
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
68 |
""" Sometimes it is convenient to NOT write some container element |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
69 |
if it happens that there is nothing to be written within, |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
70 |
but this cannot be known beforehand. |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
71 |
Hence one can do this: |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
72 |
|
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
73 |
.. sourcecode:: python |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
74 |
|
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
75 |
with wrap_on_write(w, '<div class="foo">', '</div>') as wow: |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
76 |
component.render_stuff(wow) |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
77 |
""" |
6141
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
78 |
def __init__(self, w, tag, closetag=None): |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
79 |
self.written = False |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
80 |
self.tag = unicode(tag) |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
81 |
self.closetag = closetag |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
82 |
self.w = w |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
83 |
|
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
84 |
def __enter__(self): |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
85 |
return self |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
86 |
|
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
87 |
def __call__(self, data): |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
88 |
if self.written is False: |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
89 |
self.w(self.tag) |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
90 |
self.written = True |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
91 |
self.w(data) |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
92 |
|
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
93 |
def __exit__(self, exctype, value, traceback): |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
94 |
if self.written is True: |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
95 |
if self.closetag: |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
96 |
self.w(unicode(self.closetag)) |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
97 |
else: |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
98 |
self.w(self.tag.replace('<', '</', 1)) |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
99 |
|
0 | 100 |
|
3882
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
101 |
# use networkX instead ? |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
102 |
# http://networkx.lanl.gov/reference/algorithms.traversal.html#module-networkx.algorithms.traversal.astar |
6854 | 103 |
def transitive_closure_of(entity, rtype, _seen=None): |
4317
b2093f8f0493
some docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
104 |
"""return transitive closure *for the subgraph starting from the given |
b2093f8f0493
some docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
105 |
entity* (eg 'parent' entities are not included in the results) |
b2093f8f0493
some docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
106 |
""" |
3882
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
107 |
if _seen is None: |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
108 |
_seen = set() |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
109 |
_seen.add(entity.eid) |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
110 |
yield entity |
6854 | 111 |
for child in getattr(entity, rtype): |
3882
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
112 |
if child.eid in _seen: |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
113 |
continue |
6854 | 114 |
for subchild in transitive_closure_of(child, rtype, _seen): |
3882
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
115 |
yield subchild |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
116 |
|
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
117 |
|
0 | 118 |
class SizeConstrainedList(list): |
119 |
"""simple list that makes sure the list does not get bigger |
|
120 |
than a given size. |
|
121 |
||
122 |
when the list is full and a new element is added, the first |
|
123 |
element of the list is removed before appending the new one |
|
124 |
||
125 |
>>> l = SizeConstrainedList(2) |
|
126 |
>>> l.append(1) |
|
127 |
>>> l.append(2) |
|
128 |
>>> l |
|
129 |
[1, 2] |
|
130 |
>>> l.append(3) |
|
131 |
[2, 3] |
|
132 |
""" |
|
133 |
def __init__(self, maxsize): |
|
134 |
self.maxsize = maxsize |
|
135 |
||
136 |
def append(self, element): |
|
137 |
if len(self) == self.maxsize: |
|
138 |
del self[0] |
|
139 |
super(SizeConstrainedList, self).append(element) |
|
140 |
||
141 |
def extend(self, sequence): |
|
142 |
super(SizeConstrainedList, self).extend(sequence) |
|
143 |
keepafter = len(self) - self.maxsize |
|
144 |
if keepafter > 0: |
|
145 |
del self[:keepafter] |
|
146 |
||
147 |
__iadd__ = extend |
|
148 |
||
149 |
||
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
|
150 |
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
|
151 |
"""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
|
152 |
__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
|
153 |
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
|
154 |
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
|
155 |
self._item = item |
5883
7a5f370c5be1
[utils] improve repr() of RepeatList
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5726
diff
changeset
|
156 |
def __repr__(self): |
7a5f370c5be1
[utils] improve repr() of RepeatList
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5726
diff
changeset
|
157 |
return '<cubicweb.utils.RepeatList at %s item=%s size=%s>' % ( |
7a5f370c5be1
[utils] improve repr() of RepeatList
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5726
diff
changeset
|
158 |
id(self), self._item, self._size) |
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
|
159 |
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
|
160 |
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
|
161 |
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
|
162 |
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
|
163 |
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
|
164 |
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
|
165 |
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
|
166 |
return self._item |
6374
5adf53edfe12
[rset] support for delitem on repeat list (may be necessary in pyro source)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6176
diff
changeset
|
167 |
def __delitem__(self, idc): |
5adf53edfe12
[rset] support for delitem on repeat list (may be necessary in pyro source)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6176
diff
changeset
|
168 |
assert self._size > 0 |
5adf53edfe12
[rset] support for delitem on repeat list (may be necessary in pyro source)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6176
diff
changeset
|
169 |
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
|
170 |
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
|
171 |
# 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
|
172 |
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
|
173 |
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
|
174 |
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
|
175 |
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
|
176 |
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
|
177 |
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
|
178 |
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
|
179 |
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
|
180 |
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
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
if isinstance(other, RepeatList): |
6492
47a284c0d012
fix some pylint detected errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6448
diff
changeset
|
187 |
return other._size == self._size and other._item == self._item |
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
|
188 |
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
|
189 |
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
|
190 |
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
|
191 |
|
6684
b8bd0ecced2e
minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6580
diff
changeset
|
192 |
|
0 | 193 |
class UStringIO(list): |
194 |
"""a file wrapper which automatically encode unicode string to an encoding |
|
195 |
specifed in the constructor |
|
196 |
""" |
|
197 |
||
198 |
def __nonzero__(self): |
|
199 |
return True |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
200 |
|
0 | 201 |
def write(self, value): |
202 |
assert isinstance(value, unicode), u"unicode required not %s : %s"\ |
|
203 |
% (type(value).__name__, repr(value)) |
|
204 |
self.append(value) |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
205 |
|
0 | 206 |
def getvalue(self): |
207 |
return u''.join(self) |
|
208 |
||
209 |
def __repr__(self): |
|
210 |
return '<%s at %#x>' % (self.__class__.__name__, id(self)) |
|
211 |
||
212 |
||
213 |
class HTMLHead(UStringIO): |
|
214 |
"""wraps HTML header's stream |
|
215 |
||
216 |
Request objects use a HTMLHead instance to ease adding of |
|
217 |
javascripts and stylesheets |
|
218 |
""" |
|
7267
2fe2ed459e4d
[page data] don't call unload_page_data too many time (closes #1518398)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7140
diff
changeset
|
219 |
js_unload_code = u'''if (typeof(pageDataUnloaded) == 'undefined') { |
2fe2ed459e4d
[page data] don't call unload_page_data too many time (closes #1518398)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7140
diff
changeset
|
220 |
jQuery(window).unload(unloadPageData); |
2fe2ed459e4d
[page data] don't call unload_page_data too many time (closes #1518398)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7140
diff
changeset
|
221 |
pageDataUnloaded = true; |
2fe2ed459e4d
[page data] don't call unload_page_data too many time (closes #1518398)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7140
diff
changeset
|
222 |
}''' |
6406
39663630ca3c
xml-escape <script> tags generated by HTMLHead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6374
diff
changeset
|
223 |
# Making <script> tag content work properly with all possible |
39663630ca3c
xml-escape <script> tags generated by HTMLHead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6374
diff
changeset
|
224 |
# content-types (xml/html) and all possible browsers is very |
39663630ca3c
xml-escape <script> tags generated by HTMLHead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6374
diff
changeset
|
225 |
# tricky, see http://www.hixie.ch/advocacy/xhtml for an in-depth discussion |
39663630ca3c
xml-escape <script> tags generated by HTMLHead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6374
diff
changeset
|
226 |
xhtml_safe_script_opening = u'<script type="text/javascript"><!--//--><![CDATA[//><!--\n' |
39663630ca3c
xml-escape <script> tags generated by HTMLHead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6374
diff
changeset
|
227 |
xhtml_safe_script_closing = u'\n//--><!]]></script>' |
0 | 228 |
|
7277
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
229 |
def __init__(self, datadir_url=None): |
0 | 230 |
super(HTMLHead, self).__init__() |
231 |
self.jsvars = [] |
|
232 |
self.jsfiles = [] |
|
233 |
self.cssfiles = [] |
|
234 |
self.ie_cssfiles = [] |
|
235 |
self.post_inlined_scripts = [] |
|
236 |
self.pagedata_unload = False |
|
7277
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
237 |
self.datadir_url = datadir_url |
0 | 238 |
|
239 |
||
240 |
def add_raw(self, rawheader): |
|
241 |
self.write(rawheader) |
|
242 |
||
3838
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
243 |
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
|
244 |
"""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
|
245 |
|
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
246 |
: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
|
247 |
: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
|
248 |
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
|
249 |
: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
|
250 |
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
|
251 |
""" |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
252 |
self.jsvars.append( (var, value, override) ) |
0 | 253 |
|
254 |
def add_post_inline_script(self, content): |
|
255 |
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
|
256 |
|
4830
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
257 |
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
|
258 |
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
|
259 |
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
|
260 |
DeprecationWarning, stacklevel=2) |
6448
8590d82e9b1b
[javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6409
diff
changeset
|
261 |
self.add_post_inline_script(u"""$(cw).one('server-response', function(event) { |
4903
627fcd90e08f
fix bad conflict resolution
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4899
diff
changeset
|
262 |
%s});""" % jscode) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
263 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
264 |
|
0 | 265 |
def add_js(self, jsfile): |
266 |
"""adds `jsfile` to the list of javascripts used in the webpage |
|
267 |
||
268 |
This function checks if the file has already been added |
|
269 |
:param jsfile: the script's URL |
|
270 |
""" |
|
271 |
if jsfile not in self.jsfiles: |
|
272 |
self.jsfiles.append(jsfile) |
|
273 |
||
7277
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
274 |
def add_css(self, cssfile, media='all'): |
0 | 275 |
"""adds `cssfile` to the list of javascripts used in the webpage |
276 |
||
277 |
This function checks if the file has already been added |
|
278 |
:param cssfile: the stylesheet's URL |
|
279 |
""" |
|
280 |
if (cssfile, media) not in self.cssfiles: |
|
281 |
self.cssfiles.append( (cssfile, media) ) |
|
282 |
||
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4859
diff
changeset
|
283 |
def add_ie_css(self, cssfile, media='all', iespec=u'[if lt IE 8]'): |
0 | 284 |
"""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
|
285 |
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
|
286 |
self.ie_cssfiles.append( (cssfile, media, iespec) ) |
0 | 287 |
|
288 |
def add_unload_pagedata(self): |
|
289 |
"""registers onunload callback to clean page data on server""" |
|
290 |
if not self.pagedata_unload: |
|
291 |
self.post_inlined_scripts.append(self.js_unload_code) |
|
292 |
self.pagedata_unload = True |
|
293 |
||
7277
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
294 |
def concat_urls(self, urls): |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
295 |
"""concatenates urls into one url usable by Apache mod_concat |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
296 |
|
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
297 |
This method returns the url without modifying it if there is only |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
298 |
one element in the list |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
299 |
:param urls: list of local urls/filenames to concatenate |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
300 |
""" |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
301 |
if len(urls) == 1: |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
302 |
return urls[0] |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
303 |
len_prefix = len(self.datadir_url) |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
304 |
concated = u','.join(url[len_prefix:] for url in urls) |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
305 |
return (u'%s??%s' % (self.datadir_url, concated)) |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
306 |
|
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
307 |
def group_urls(self, urls_spec): |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
308 |
"""parses urls_spec in order to generate concatenated urls |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
309 |
for js and css includes |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
310 |
|
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
311 |
This method checks if the file is local and if it shares options |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
312 |
with direct neighbors |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
313 |
:param urls_spec: entire list of urls/filenames to inspect |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
314 |
""" |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
315 |
concatable = [] |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
316 |
prev_islocal = False |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
317 |
prev_key = None |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
318 |
for url, key in urls_spec: |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
319 |
islocal = url.startswith(self.datadir_url) |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
320 |
if concatable and (islocal != prev_islocal or key != prev_key): |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
321 |
yield (self.concat_urls(concatable), prev_key) |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
322 |
del concatable[:] |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
323 |
if not islocal: |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
324 |
yield (url, key) |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
325 |
else: |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
326 |
concatable.append(url) |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
327 |
prev_islocal = islocal |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
328 |
prev_key = key |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
329 |
if concatable: |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
330 |
yield (self.concat_urls(concatable), prev_key) |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
331 |
|
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
332 |
|
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
|
333 |
def getvalue(self, skiphead=False): |
0 | 334 |
"""reimplement getvalue to provide a consistent (and somewhat browser |
335 |
optimzed cf. http://stevesouders.com/cuzillion) order in external |
|
336 |
resources declaration |
|
337 |
""" |
|
338 |
w = self.write |
|
339 |
# 1/ variable declaration if any |
|
340 |
if self.jsvars: |
|
6406
39663630ca3c
xml-escape <script> tags generated by HTMLHead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6374
diff
changeset
|
341 |
w(self.xhtml_safe_script_opening) |
3838
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
342 |
for var, value, override in self.jsvars: |
5389
809d3b5b3d31
[python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5377
diff
changeset
|
343 |
vardecl = u'%s = %s;' % (var, json.dumps(value)) |
3838
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
344 |
if not override: |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
345 |
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
|
346 |
(var, vardecl)) |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
347 |
w(vardecl + u'\n') |
6406
39663630ca3c
xml-escape <script> tags generated by HTMLHead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6374
diff
changeset
|
348 |
w(self.xhtml_safe_script_closing) |
0 | 349 |
# 2/ css files |
7277
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
350 |
for cssfile, media in (self.group_urls(self.cssfiles) if self.datadir_url else self.cssfiles): |
0 | 351 |
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
|
352 |
(media, xml_escape(cssfile))) |
0 | 353 |
# 3/ ie css if necessary |
354 |
if self.ie_cssfiles: |
|
7277
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
355 |
ie_cssfiles = ((x, (y, z)) for x, y, z in self.ie_cssfiles) |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
356 |
for cssfile, (media, iespec) in (self.group_urls(ie_cssfiles) if self.datadir_url else ie_cssfiles): |
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4859
diff
changeset
|
357 |
w(u'<!--%s>\n' % iespec) |
0 | 358 |
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
|
359 |
(media, xml_escape(cssfile))) |
0 | 360 |
w(u'<![endif]--> \n') |
361 |
# 4/ js files |
|
7277
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
362 |
jsfiles = ((x, None) for x in self.jsfiles) |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
363 |
for jsfile, media in self.group_urls(jsfiles) if self.datadir_url else jsfiles: |
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
364 |
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
|
365 |
xml_escape(jsfile)) |
0 | 366 |
# 5/ post inlined scripts (i.e. scripts depending on other JS files) |
367 |
if self.post_inlined_scripts: |
|
6406
39663630ca3c
xml-escape <script> tags generated by HTMLHead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6374
diff
changeset
|
368 |
w(self.xhtml_safe_script_opening) |
0 | 369 |
w(u'\n\n'.join(self.post_inlined_scripts)) |
6406
39663630ca3c
xml-escape <script> tags generated by HTMLHead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6374
diff
changeset
|
370 |
w(self.xhtml_safe_script_closing) |
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
|
371 |
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
|
372 |
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
|
373 |
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
|
374 |
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
|
375 |
|
0 | 376 |
|
377 |
class HTMLStream(object): |
|
378 |
"""represents a HTML page. |
|
379 |
||
380 |
This is used my main templates so that HTML headers can be added |
|
381 |
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
|
382 |
|
0 | 383 |
HTMLStream uses the (U)StringIO interface to be compliant with |
384 |
existing code. |
|
385 |
""" |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
386 |
|
0 | 387 |
def __init__(self, req): |
388 |
# stream for <head> |
|
389 |
self.head = req.html_headers |
|
390 |
# main stream |
|
391 |
self.body = UStringIO() |
|
392 |
self.doctype = u'' |
|
393 |
# xmldecl and html opening tag |
|
394 |
self.xmldecl = u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding |
|
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
395 |
self._namespaces = [('xmlns', 'http://www.w3.org/1999/xhtml'), |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
396 |
('xmlns:cubicweb','http://www.logilab.org/2008/cubicweb')] |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
397 |
self._htmlattrs = [('xml:lang', req.lang), |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
398 |
('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
|
399 |
# 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
|
400 |
req.main_stream = self |
0 | 401 |
|
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
402 |
def add_namespace(self, prefix, uri): |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
403 |
self._namespaces.append( (prefix, uri) ) |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
404 |
|
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
405 |
def set_namespaces(self, namespaces): |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
406 |
self._namespaces = namespaces |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
407 |
|
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
408 |
def add_htmlattr(self, attrname, attrvalue): |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
409 |
self._htmlattrs.append( (attrname, attrvalue) ) |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
410 |
|
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
411 |
def set_htmlattrs(self, attrs): |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
412 |
self._htmlattrs = attrs |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
413 |
|
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
414 |
def set_doctype(self, doctype, reset_xmldecl=True): |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
415 |
self.doctype = doctype |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
416 |
if reset_xmldecl: |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
417 |
self.xmldecl = u'' |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
418 |
|
0 | 419 |
def write(self, data): |
420 |
"""StringIO interface: this method will be assigned to self.w |
|
421 |
""" |
|
422 |
self.body.write(data) |
|
423 |
||
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
424 |
@property |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
425 |
def htmltag(self): |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
426 |
attrs = ' '.join('%s="%s"' % (attr, xml_escape(value)) |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
427 |
for attr, value in (self._namespaces + self._htmlattrs)) |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
428 |
if attrs: |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
429 |
return '<html %s>' % attrs |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
430 |
return '<html>' |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
431 |
|
0 | 432 |
def getvalue(self): |
433 |
"""writes HTML headers, closes </head> tag and writes HTML body""" |
|
434 |
return u'%s\n%s\n%s\n%s\n%s\n</html>' % (self.xmldecl, self.doctype, |
|
435 |
self.htmltag, |
|
436 |
self.head.getvalue(), |
|
437 |
self.body.getvalue()) |
|
438 |
||
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
439 |
try: |
5389
809d3b5b3d31
[python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5377
diff
changeset
|
440 |
# may not be there if cubicweb-web not installed |
5940
0e3ae19b181a
[uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5883
diff
changeset
|
441 |
if sys.version_info < (2, 6): |
5389
809d3b5b3d31
[python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5377
diff
changeset
|
442 |
import simplejson as json |
809d3b5b3d31
[python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5377
diff
changeset
|
443 |
else: |
809d3b5b3d31
[python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5377
diff
changeset
|
444 |
import json |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
445 |
except ImportError: |
5940
0e3ae19b181a
[uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5883
diff
changeset
|
446 |
json_dumps = None |
0e3ae19b181a
[uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5883
diff
changeset
|
447 |
|
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
448 |
else: |
6134
272cfcc10a28
use logilab.common.date.ustrftime in json encoder to work around datetime.strftime limitation on dates < 1900
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6044
diff
changeset
|
449 |
from logilab.common.date import ustrftime |
6176
33ba2c1dbf10
[cw.utils] avoid spurious warning, ustrftime may be globally imported
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6134
diff
changeset
|
450 |
|
5389
809d3b5b3d31
[python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5377
diff
changeset
|
451 |
class CubicWebJsonEncoder(json.JSONEncoder): |
5377
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5320
diff
changeset
|
452 |
"""define a json 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
|
453 |
|
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
454 |
def default(self, obj): |
5726
c3b99606644d
[json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
455 |
if hasattr(obj, 'eid'): |
c3b99606644d
[json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
456 |
d = obj.cw_attr_cache.copy() |
c3b99606644d
[json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
457 |
d['eid'] = obj.eid |
c3b99606644d
[json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
458 |
return d |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
459 |
if isinstance(obj, datetime.datetime): |
6134
272cfcc10a28
use logilab.common.date.ustrftime in json encoder to work around datetime.strftime limitation on dates < 1900
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6044
diff
changeset
|
460 |
return ustrftime(obj, '%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
|
461 |
elif isinstance(obj, datetime.date): |
6134
272cfcc10a28
use logilab.common.date.ustrftime in json encoder to work around datetime.strftime limitation on dates < 1900
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6044
diff
changeset
|
462 |
return ustrftime(obj, '%Y/%m/%d') |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
463 |
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
|
464 |
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
|
465 |
elif isinstance(obj, datetime.timedelta): |
4000
4b16a7c01a25
fix jsonization of interval
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3902
diff
changeset
|
466 |
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
|
467 |
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
|
468 |
return float(obj) |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
469 |
try: |
5389
809d3b5b3d31
[python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5377
diff
changeset
|
470 |
return json.JSONEncoder.default(self, obj) |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
471 |
except TypeError: |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
472 |
# 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
|
473 |
# 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
|
474 |
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
|
475 |
|
5940
0e3ae19b181a
[uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5883
diff
changeset
|
476 |
def json_dumps(value): |
0e3ae19b181a
[uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5883
diff
changeset
|
477 |
return json.dumps(value, cls=CubicWebJsonEncoder) |
0e3ae19b181a
[uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5883
diff
changeset
|
478 |
|
4879
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
479 |
|
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
480 |
@deprecated('[3.7] merge_dicts is deprecated') |
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
481 |
def merge_dicts(dict1, dict2): |
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
482 |
"""update a copy of `dict1` with `dict2`""" |
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
483 |
dict1 = dict(dict1) |
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
484 |
dict1.update(dict2) |
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
485 |
return dict1 |
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
486 |
|
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
|
487 |
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
|
488 |
_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
|
489 |
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
|
490 |
'days_in_month', 'days_in_year', 'previous_month', |
6176
33ba2c1dbf10
[cw.utils] avoid spurious warning, ustrftime may be globally imported
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6134
diff
changeset
|
491 |
'next_month', 'first_day', 'last_day', |
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
|
492 |
'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
|
493 |
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
|
494 |
_THIS_MOD_NS[funcname] = deprecated(msg)(getattr(date, funcname)) |