author | Julien Cristau <julien.cristau@logilab.fr> |
Wed, 29 Jan 2014 16:03:42 +0100 | |
changeset 9501 | 2904e2ba81a0 |
parent 9279 | 0814b9dd9bf3 |
child 9523 | cd5738fc440f |
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.""" |
7956
db49658b2812
add missing imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7954
diff
changeset
|
19 |
|
db49658b2812
add missing imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7954
diff
changeset
|
20 |
from __future__ import division, with_statement |
db49658b2812
add missing imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7954
diff
changeset
|
21 |
|
0 | 22 |
__docformat__ = "restructuredtext en" |
23 |
||
3316 | 24 |
import sys |
25 |
import decimal |
|
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
26 |
import datetime |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
27 |
import random |
7998
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
28 |
import re |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
29 |
|
7954
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
30 |
from operator import itemgetter |
6067
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
31 |
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
|
32 |
from itertools import repeat |
4833
41a78fb4107c
3.7 depends on python >= 2.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4693
diff
changeset
|
33 |
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
|
34 |
from warnings import warn |
7954
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
35 |
from threading import Lock |
8682
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
36 |
from urlparse import urlparse |
7954
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
37 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
38 |
from logging import getLogger |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
39 |
|
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
40 |
from logilab.mtconverter import xml_escape |
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
41 |
from logilab.common.deprecation import deprecated |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
42 |
|
4830
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
43 |
_MARKER = object() |
10e8bc190695
[javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4693
diff
changeset
|
44 |
|
1024
bb96289257bf
remove unused working_hours function, new todate function since we can't compare date and datetime objects
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
45 |
# initialize random seed from current time |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
46 |
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
|
47 |
|
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
|
48 |
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
|
49 |
"""Return a unique identifier string. |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
50 |
|
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
|
51 |
if specified, `key` is used to prefix the generated uid so it can be used |
9232
430a7dc5e2cf
[utils] fix typos in make_uid docstring
Julien Cristau <julien.cristau@logilab.fr>
parents:
8941
diff
changeset
|
52 |
for instance as a DOM id or as sql table name. |
4653
ca11228a5268
import cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4652
diff
changeset
|
53 |
|
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
|
54 |
See uuid.uuid4 documentation for the shape of the generated identifier, but |
9232
430a7dc5e2cf
[utils] fix typos in make_uid docstring
Julien Cristau <julien.cristau@logilab.fr>
parents:
8941
diff
changeset
|
55 |
this is basically a 32 bits hexadecimal string. |
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
|
56 |
""" |
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
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
|
0 | 61 |
|
6067
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
62 |
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
|
63 |
"""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
|
64 |
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
|
65 |
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
|
66 |
argspec = getargspec(callable) |
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
67 |
if argspec[2]: |
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
68 |
return True |
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
69 |
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
|
70 |
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
|
71 |
return False |
efca814587e2
[primary] refactor primary view handling of relation's label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6044
diff
changeset
|
72 |
return True |
0 | 73 |
|
6141
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
74 |
|
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
75 |
class wrap_on_write(object): |
6580
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
76 |
""" Sometimes it is convenient to NOT write some container element |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
77 |
if it happens that there is nothing to be written within, |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
78 |
but this cannot be known beforehand. |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
79 |
Hence one can do this: |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
80 |
|
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
81 |
.. sourcecode:: python |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
82 |
|
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
83 |
with wrap_on_write(w, '<div class="foo">', '</div>') as wow: |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
84 |
component.render_stuff(wow) |
798200432f50
some docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6492
diff
changeset
|
85 |
""" |
6141
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
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
|
91 |
|
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
92 |
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
|
93 |
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
|
94 |
|
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
|
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
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
|
104 |
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
|
105 |
else: |
b8287e54b528
[web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6067
diff
changeset
|
106 |
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
|
107 |
|
0 | 108 |
|
3882
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
109 |
# use networkX instead ? |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
110 |
# http://networkx.lanl.gov/reference/algorithms.traversal.html#module-networkx.algorithms.traversal.astar |
6854 | 111 |
def transitive_closure_of(entity, rtype, _seen=None): |
4317
b2093f8f0493
some docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
112 |
"""return transitive closure *for the subgraph starting from the given |
b2093f8f0493
some docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
113 |
entity* (eg 'parent' entities are not included in the results) |
b2093f8f0493
some docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
114 |
""" |
3882
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
115 |
if _seen is None: |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
116 |
_seen = set() |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
117 |
_seen.add(entity.eid) |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
118 |
yield entity |
6854 | 119 |
for child in getattr(entity, rtype): |
3882
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
120 |
if child.eid in _seen: |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
121 |
continue |
6854 | 122 |
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
|
123 |
yield subchild |
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
124 |
|
addc715f4fcd
backported from confman
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3851
diff
changeset
|
125 |
|
0 | 126 |
class SizeConstrainedList(list): |
7568
c5ee33fb6a3b
[test] run and fix utils and uilib doctests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7268
diff
changeset
|
127 |
"""simple list that makes sure the list does not get bigger than a given |
c5ee33fb6a3b
[test] run and fix utils and uilib doctests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7268
diff
changeset
|
128 |
size. |
0 | 129 |
|
7568
c5ee33fb6a3b
[test] run and fix utils and uilib doctests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7268
diff
changeset
|
130 |
when the list is full and a new element is added, the first element of the |
c5ee33fb6a3b
[test] run and fix utils and uilib doctests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7268
diff
changeset
|
131 |
list is removed before appending the new one |
0 | 132 |
|
133 |
>>> l = SizeConstrainedList(2) |
|
134 |
>>> l.append(1) |
|
135 |
>>> l.append(2) |
|
136 |
>>> l |
|
137 |
[1, 2] |
|
138 |
>>> l.append(3) |
|
7568
c5ee33fb6a3b
[test] run and fix utils and uilib doctests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7268
diff
changeset
|
139 |
>>> l |
0 | 140 |
[2, 3] |
141 |
""" |
|
142 |
def __init__(self, maxsize): |
|
143 |
self.maxsize = maxsize |
|
144 |
||
145 |
def append(self, element): |
|
146 |
if len(self) == self.maxsize: |
|
147 |
del self[0] |
|
148 |
super(SizeConstrainedList, self).append(element) |
|
149 |
||
150 |
def extend(self, sequence): |
|
151 |
super(SizeConstrainedList, self).extend(sequence) |
|
152 |
keepafter = len(self) - self.maxsize |
|
153 |
if keepafter > 0: |
|
154 |
del self[:keepafter] |
|
155 |
||
156 |
__iadd__ = extend |
|
157 |
||
158 |
||
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 |
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
|
160 |
"""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
|
161 |
__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
|
162 |
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
|
163 |
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
|
164 |
self._item = item |
5883
7a5f370c5be1
[utils] improve repr() of RepeatList
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5726
diff
changeset
|
165 |
def __repr__(self): |
7a5f370c5be1
[utils] improve repr() of RepeatList
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5726
diff
changeset
|
166 |
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
|
167 |
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
|
168 |
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
|
169 |
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
|
170 |
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
|
171 |
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
|
172 |
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
|
173 |
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
|
174 |
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
|
175 |
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
|
176 |
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
|
177 |
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
|
178 |
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
|
179 |
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
|
180 |
# 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
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
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
|
187 |
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
|
188 |
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
|
189 |
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
|
190 |
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
|
191 |
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
|
192 |
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
|
193 |
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
|
194 |
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
|
195 |
if isinstance(other, RepeatList): |
6492
47a284c0d012
fix some pylint detected errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6448
diff
changeset
|
196 |
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
|
197 |
return self[:] == other |
9279
0814b9dd9bf3
[py3k] note about future warning on __eq__ vs __hash__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9232
diff
changeset
|
198 |
# py3k future warning "Overriding __eq__ blocks inheritance of __hash__ in 3.x" |
0814b9dd9bf3
[py3k] note about future warning on __eq__ vs __hash__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9232
diff
changeset
|
199 |
# is annoying but won't go away because we don't want to hash() the repeatlist |
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
|
200 |
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
|
201 |
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
|
202 |
|
6684
b8bd0ecced2e
minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6580
diff
changeset
|
203 |
|
0 | 204 |
class UStringIO(list): |
205 |
"""a file wrapper which automatically encode unicode string to an encoding |
|
206 |
specifed in the constructor |
|
207 |
""" |
|
208 |
||
209 |
def __nonzero__(self): |
|
210 |
return True |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
211 |
|
0 | 212 |
def write(self, value): |
213 |
assert isinstance(value, unicode), u"unicode required not %s : %s"\ |
|
214 |
% (type(value).__name__, repr(value)) |
|
215 |
self.append(value) |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
216 |
|
0 | 217 |
def getvalue(self): |
218 |
return u''.join(self) |
|
219 |
||
220 |
def __repr__(self): |
|
221 |
return '<%s at %#x>' % (self.__class__.__name__, id(self)) |
|
222 |
||
223 |
||
224 |
class HTMLHead(UStringIO): |
|
225 |
"""wraps HTML header's stream |
|
226 |
||
227 |
Request objects use a HTMLHead instance to ease adding of |
|
228 |
javascripts and stylesheets |
|
229 |
""" |
|
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
|
230 |
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
|
231 |
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
|
232 |
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
|
233 |
}''' |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
234 |
script_opening = u'<script type="text/javascript">\n' |
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
235 |
script_closing = u'\n</script>' |
0 | 236 |
|
7762
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
237 |
def __init__(self, req): |
0 | 238 |
super(HTMLHead, self).__init__() |
239 |
self.jsvars = [] |
|
240 |
self.jsfiles = [] |
|
241 |
self.cssfiles = [] |
|
242 |
self.ie_cssfiles = [] |
|
243 |
self.post_inlined_scripts = [] |
|
244 |
self.pagedata_unload = False |
|
7762
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
245 |
self._cw = req |
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
246 |
self.datadir_url = req.datadir_url |
0 | 247 |
|
248 |
def add_raw(self, rawheader): |
|
249 |
self.write(rawheader) |
|
250 |
||
3838
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
251 |
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
|
252 |
"""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
|
253 |
|
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
254 |
: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
|
255 |
: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
|
256 |
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
|
257 |
: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
|
258 |
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
|
259 |
""" |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
260 |
self.jsvars.append( (var, value, override) ) |
0 | 261 |
|
262 |
def add_post_inline_script(self, content): |
|
263 |
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
|
264 |
|
8795
772cd62e1295
[utils] drop pre-3.7 compat in util
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8682
diff
changeset
|
265 |
def add_onload(self, jscode): |
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
|
266 |
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
|
267 |
%s});""" % jscode) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
268 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
269 |
|
0 | 270 |
def add_js(self, jsfile): |
271 |
"""adds `jsfile` to the list of javascripts used in the webpage |
|
272 |
||
273 |
This function checks if the file has already been added |
|
274 |
:param jsfile: the script's URL |
|
275 |
""" |
|
276 |
if jsfile not in self.jsfiles: |
|
277 |
self.jsfiles.append(jsfile) |
|
278 |
||
7277
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
279 |
def add_css(self, cssfile, media='all'): |
0 | 280 |
"""adds `cssfile` to the list of javascripts used in the webpage |
281 |
||
282 |
This function checks if the file has already been added |
|
283 |
:param cssfile: the stylesheet's URL |
|
284 |
""" |
|
285 |
if (cssfile, media) not in self.cssfiles: |
|
286 |
self.cssfiles.append( (cssfile, media) ) |
|
287 |
||
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4859
diff
changeset
|
288 |
def add_ie_css(self, cssfile, media='all', iespec=u'[if lt IE 8]'): |
0 | 289 |
"""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
|
290 |
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
|
291 |
self.ie_cssfiles.append( (cssfile, media, iespec) ) |
0 | 292 |
|
293 |
def add_unload_pagedata(self): |
|
294 |
"""registers onunload callback to clean page data on server""" |
|
295 |
if not self.pagedata_unload: |
|
296 |
self.post_inlined_scripts.append(self.js_unload_code) |
|
297 |
self.pagedata_unload = True |
|
298 |
||
7277
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
299 |
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
|
300 |
"""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
|
301 |
|
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
302 |
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
|
303 |
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
|
304 |
: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
|
305 |
""" |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
306 |
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
|
307 |
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
|
308 |
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
|
309 |
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
|
310 |
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
|
311 |
|
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
312 |
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
|
313 |
"""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
|
314 |
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
|
315 |
|
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
316 |
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
|
317 |
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
|
318 |
: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
|
319 |
""" |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
320 |
concatable = [] |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
321 |
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
|
322 |
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
|
323 |
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
|
324 |
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
|
325 |
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
|
326 |
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
|
327 |
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
|
328 |
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
|
329 |
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
|
330 |
else: |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
331 |
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
|
332 |
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
|
333 |
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
|
334 |
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
|
335 |
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
|
336 |
|
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7187
diff
changeset
|
337 |
|
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
|
338 |
def getvalue(self, skiphead=False): |
0 | 339 |
"""reimplement getvalue to provide a consistent (and somewhat browser |
340 |
optimzed cf. http://stevesouders.com/cuzillion) order in external |
|
341 |
resources declaration |
|
342 |
""" |
|
343 |
w = self.write |
|
344 |
# 1/ variable declaration if any |
|
345 |
if self.jsvars: |
|
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
346 |
w(self.script_opening) |
3838
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
347 |
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
|
348 |
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
|
349 |
if not override: |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
350 |
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
|
351 |
(var, vardecl)) |
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3789
diff
changeset
|
352 |
w(vardecl + u'\n') |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
353 |
w(self.script_closing) |
0 | 354 |
# 2/ css files |
7762
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
355 |
ie_cssfiles = ((x, (y, z)) for x, y, z in self.ie_cssfiles) |
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
356 |
if self.datadir_url and self._cw.vreg.config['concat-resources']: |
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
357 |
cssfiles = self.group_urls(self.cssfiles) |
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
358 |
ie_cssfiles = self.group_urls(ie_cssfiles) |
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
359 |
jsfiles = (x for x, _ in self.group_urls((x, None) for x in self.jsfiles)) |
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
360 |
else: |
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
361 |
cssfiles = self.cssfiles |
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
362 |
jsfiles = self.jsfiles |
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
363 |
for cssfile, media in cssfiles: |
0 | 364 |
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
|
365 |
(media, xml_escape(cssfile))) |
0 | 366 |
# 3/ ie css if necessary |
7762
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
367 |
if self.ie_cssfiles: # use self.ie_cssfiles because `ie_cssfiles` is a genexp |
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
368 |
for cssfile, (media, iespec) in ie_cssfiles: |
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4859
diff
changeset
|
369 |
w(u'<!--%s>\n' % iespec) |
0 | 370 |
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
|
371 |
(media, xml_escape(cssfile))) |
0 | 372 |
w(u'<![endif]--> \n') |
373 |
# 4/ js files |
|
7762
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
374 |
for jsfile in jsfiles: |
7649
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
375 |
if skiphead: |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
376 |
# Don't insert <script> tags directly as they would be |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
377 |
# interpreted directly by some browsers (e.g. IE). |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
378 |
# Use <pre class="script"> tags instead and let |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
379 |
# `loadAjaxHtmlHead` handle the script insertion / execution. |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
380 |
w(u'<pre class="script" src="%s"></pre>\n' % |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
381 |
xml_escape(jsfile)) |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
382 |
# FIXME: a probably better implementation might be to add |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
383 |
# JS or CSS urls in a JS list that loadAjaxHtmlHead |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
384 |
# would iterate on and postprocess: |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
385 |
# cw._ajax_js_scripts.push('myscript.js') |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
386 |
# Then, in loadAjaxHtmlHead, do something like: |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
387 |
# jQuery.each(cw._ajax_js_script, jQuery.getScript) |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
388 |
else: |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
389 |
w(u'<script type="text/javascript" src="%s"></script>\n' % |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
390 |
xml_escape(jsfile)) |
0 | 391 |
# 5/ post inlined scripts (i.e. scripts depending on other JS files) |
392 |
if self.post_inlined_scripts: |
|
7649
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
393 |
if skiphead: |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
394 |
for script in self.post_inlined_scripts: |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
395 |
w(u'<pre class="script">') |
7660
a1506b5306cc
closes #1826543: error on some ajax_replace_url call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7649
diff
changeset
|
396 |
w(xml_escape(script)) |
7649
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
397 |
w(u'</pre>') |
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
398 |
else: |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
399 |
w(self.script_opening) |
7649
ede740bd7077
[ajax, IE] rewrite some code to avoid duplicated evaluation with IE
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7575
diff
changeset
|
400 |
w(u'\n\n'.join(self.post_inlined_scripts)) |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
401 |
w(self.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
|
402 |
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
|
403 |
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
|
404 |
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
|
405 |
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
|
406 |
|
0 | 407 |
|
408 |
class HTMLStream(object): |
|
409 |
"""represents a HTML page. |
|
410 |
||
411 |
This is used my main templates so that HTML headers can be added |
|
412 |
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
|
413 |
|
0 | 414 |
HTMLStream uses the (U)StringIO interface to be compliant with |
415 |
existing code. |
|
416 |
""" |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
417 |
|
0 | 418 |
def __init__(self, req): |
419 |
# stream for <head> |
|
420 |
self.head = req.html_headers |
|
421 |
# main stream |
|
422 |
self.body = UStringIO() |
|
423 |
self.doctype = u'' |
|
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
424 |
self._htmlattrs = [('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
|
425 |
# 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
|
426 |
req.main_stream = self |
0 | 427 |
|
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
428 |
@deprecated('[3.17] there are no namespaces in html, xhtml is not served any longer') |
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
429 |
def add_namespace(self, prefix, uri): |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
430 |
pass |
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
431 |
|
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
432 |
@deprecated('[3.17] there are no namespaces in html, xhtml is not served any longer') |
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
433 |
def set_namespaces(self, namespaces): |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
434 |
pass |
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
435 |
|
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
436 |
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
|
437 |
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
|
438 |
|
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
439 |
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
|
440 |
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
|
441 |
|
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
442 |
def set_doctype(self, doctype, reset_xmldecl=None): |
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
443 |
self.doctype = doctype |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
444 |
if reset_xmldecl is not None: |
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
445 |
warn('[3.17] xhtml is no more supported', |
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
446 |
DeprecationWarning, stacklevel=2) |
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
447 |
|
0 | 448 |
def write(self, data): |
449 |
"""StringIO interface: this method will be assigned to self.w |
|
450 |
""" |
|
451 |
self.body.write(data) |
|
452 |
||
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
453 |
@property |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
454 |
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
|
455 |
attrs = ' '.join('%s="%s"' % (attr, xml_escape(value)) |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
456 |
for attr, value in self._htmlattrs) |
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7140
diff
changeset
|
457 |
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
|
458 |
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
|
459 |
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
|
460 |
|
0 | 461 |
def getvalue(self): |
462 |
"""writes HTML headers, closes </head> tag and writes HTML body""" |
|
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
463 |
return u'%s\n%s\n%s\n%s\n</html>' % (self.doctype, |
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
464 |
self.htmltag, |
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
465 |
self.head.getvalue(), |
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8795
diff
changeset
|
466 |
self.body.getvalue()) |
0 | 467 |
|
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
468 |
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
|
469 |
# 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
|
470 |
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
|
471 |
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
|
472 |
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
|
473 |
import json |
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
474 |
except ImportError: |
7575
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
475 |
json_dumps = JSString = None |
5940
0e3ae19b181a
[uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5883
diff
changeset
|
476 |
|
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
477 |
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
|
478 |
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
|
479 |
|
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
|
480 |
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
|
481 |
"""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
|
482 |
|
3371
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
483 |
def default(self, obj): |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7762
diff
changeset
|
484 |
if hasattr(obj, '__json_encode__'): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7762
diff
changeset
|
485 |
return obj.__json_encode__() |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4335
diff
changeset
|
486 |
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
|
487 |
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
|
488 |
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
|
489 |
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
|
490 |
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
|
491 |
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
|
492 |
elif isinstance(obj, datetime.timedelta): |
4000
4b16a7c01a25
fix jsonization of interval
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3902
diff
changeset
|
493 |
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
|
494 |
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
|
495 |
return float(obj) |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
496 |
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
|
497 |
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
|
498 |
except TypeError: |
762a7bc3b73d
this has probably be killed by a bad merge...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3364
diff
changeset
|
499 |
# 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
|
500 |
# 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
|
501 |
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
|
502 |
|
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7762
diff
changeset
|
503 |
def json_dumps(value, **kwargs): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7762
diff
changeset
|
504 |
return json.dumps(value, cls=CubicWebJsonEncoder, **kwargs) |
5940
0e3ae19b181a
[uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5883
diff
changeset
|
505 |
|
4879
56c16efedc51
deprecates merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4844
diff
changeset
|
506 |
|
7575
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
507 |
class JSString(str): |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
508 |
"""use this string sub class in values given to :func:`js_dumps` to |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
509 |
insert raw javascript chain in some JSON string |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
510 |
""" |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
511 |
|
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
512 |
def _dict2js(d, predictable=False): |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
513 |
res = [key + ': ' + js_dumps(val, predictable) |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
514 |
for key, val in d.iteritems()] |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
515 |
return '{%s}' % ', '.join(res) |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
516 |
|
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
517 |
def _list2js(l, predictable=False): |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
518 |
return '[%s]' % ', '.join([js_dumps(val, predictable) for val in l]) |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
519 |
|
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
520 |
def js_dumps(something, predictable=False): |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
521 |
"""similar as :func:`json_dumps`, except values which are instances of |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
522 |
:class:`JSString` are expected to be valid javascript and will be output |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
523 |
as is |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
524 |
|
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
525 |
>>> js_dumps({'hop': JSString('$.hop'), 'bar': None}, predictable=True) |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
526 |
'{bar: null, hop: $.hop}' |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
527 |
>>> js_dumps({'hop': '$.hop'}) |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
528 |
'{hop: "$.hop"}' |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
529 |
>>> js_dumps({'hip': {'hop': JSString('momo')}}) |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
530 |
'{hip: {hop: momo}}' |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
531 |
""" |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
532 |
if isinstance(something, dict): |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
533 |
return _dict2js(something, predictable) |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
534 |
if isinstance(something, list): |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
535 |
return _list2js(something, predictable) |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
536 |
if isinstance(something, JSString): |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
537 |
return something |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
538 |
return json_dumps(something) |
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
539 |
|
7998
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
540 |
PERCENT_IN_URLQUOTE_RE = re.compile(r'%(?=[0-9a-fA-F]{2})') |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
541 |
def js_href(javascript_code): |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
542 |
"""Generate a "javascript: ..." string for an href attribute. |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
543 |
|
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
544 |
Some % which may be interpreted in a href context will be escaped. |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
545 |
|
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
546 |
In an href attribute, url-quotes-looking fragments are interpreted before |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
547 |
being given to the javascript engine. Valid url quotes are in the form |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
548 |
``%xx`` with xx being a byte in hexadecimal form. This means that ``%toto`` |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
549 |
will be unaltered but ``%babar`` will be mangled because ``ba`` is the |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
550 |
hexadecimal representation of 186. |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
551 |
|
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
552 |
>>> js_href('alert("babar");') |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
553 |
'javascript: alert("babar");' |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
554 |
>>> js_href('alert("%babar");') |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
555 |
'javascript: alert("%25babar");' |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
556 |
>>> js_href('alert("%toto %babar");') |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
557 |
'javascript: alert("%toto %25babar");' |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
558 |
>>> js_href('alert("%1337%");') |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
559 |
'javascript: alert("%251337%");' |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
560 |
""" |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
561 |
return 'javascript: ' + PERCENT_IN_URLQUOTE_RE.sub(r'%25', javascript_code) |
9ef285eb20f4
[utils] add a ``js_href`` function to generated proper javascript href
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7990
diff
changeset
|
562 |
|
7575
335f14e8e5a7
[uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7569
diff
changeset
|
563 |
|
8682
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
564 |
def parse_repo_uri(uri): |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
565 |
""" transform a command line uri into a (protocol, hostport, appid), e.g: |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
566 |
<myapp> -> 'inmemory', None, '<myapp>' |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
567 |
inmemory://<myapp> -> 'inmemory', None, '<myapp>' |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
568 |
pyro://[host][:port] -> 'pyro', 'host:port', None |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
569 |
zmqpickle://[host][:port] -> 'zmqpickle', 'host:port', None |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
570 |
""" |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
571 |
parseduri = urlparse(uri) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
572 |
scheme = parseduri.scheme |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
573 |
if scheme == '': |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
574 |
return ('inmemory', None, parseduri.path) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
575 |
if scheme == 'inmemory': |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
576 |
return (scheme, None, parseduri.netloc) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
577 |
if scheme in ('pyro', 'pyroloc') or scheme.startswith('zmqpickle-'): |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
578 |
return (scheme, parseduri.netloc, parseduri.path) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
579 |
raise NotImplementedError('URI protocol not implemented for `%s`' % uri) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
580 |
|
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
581 |
|
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7998
diff
changeset
|
582 |
|
7954
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
583 |
logger = getLogger('cubicweb.utils') |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
584 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
585 |
class QueryCache(object): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
586 |
""" a minimalist dict-like object to be used by the querier |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
587 |
and native source (replaces lgc.cache for this very usage) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
588 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
589 |
To be efficient it must be properly used. The usage patterns are |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
590 |
quite specific to its current clients. |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
591 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
592 |
The ceiling value should be sufficiently high, else it will be |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
593 |
ruthlessly inefficient (there will be warnings when this happens). |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
594 |
A good (high enough) value can only be set on a per-application |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
595 |
value. A default, reasonnably high value is provided but tuning |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
596 |
e.g `rql-cache-size` can certainly help. |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
597 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
598 |
There are two kinds of elements to put in this cache: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
599 |
* frequently used elements |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
600 |
* occasional elements |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
601 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
602 |
The former should finish in the _permanent structure after some |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
603 |
warmup. |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
604 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
605 |
Occasional elements can be buggy requests (server-side) or |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
606 |
end-user (web-ui provided) requests. These have to be cleaned up |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
607 |
when they fill the cache, without evicting the usefull, frequently |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
608 |
used entries. |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
609 |
""" |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
610 |
# quite arbitrary, but we want to never |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
611 |
# immortalize some use-a-little query |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
612 |
_maxlevel = 15 |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
613 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
614 |
def __init__(self, ceiling=3000): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
615 |
self._max = ceiling |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
616 |
# keys belonging forever to this cache |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
617 |
self._permanent = set() |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
618 |
# mapping of key (that can get wiped) to getitem count |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
619 |
self._transient = {} |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
620 |
self._data = {} |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
621 |
self._lock = Lock() |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
622 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
623 |
def __len__(self): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
624 |
with self._lock: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
625 |
return len(self._data) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
626 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
627 |
def __getitem__(self, k): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
628 |
with self._lock: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
629 |
if k in self._permanent: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
630 |
return self._data[k] |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
631 |
v = self._transient.get(k, _MARKER) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
632 |
if v is _MARKER: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
633 |
self._transient[k] = 1 |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
634 |
return self._data[k] |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
635 |
if v > self._maxlevel: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
636 |
self._permanent.add(k) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
637 |
self._transient.pop(k, None) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
638 |
else: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
639 |
self._transient[k] += 1 |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
640 |
return self._data[k] |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
641 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
642 |
def __setitem__(self, k, v): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
643 |
with self._lock: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
644 |
if len(self._data) >= self._max: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
645 |
self._try_to_make_room() |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
646 |
self._data[k] = v |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
647 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
648 |
def pop(self, key, default=_MARKER): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
649 |
with self._lock: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
650 |
try: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
651 |
if default is _MARKER: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
652 |
return self._data.pop(key) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
653 |
return self._data.pop(key, default) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
654 |
finally: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
655 |
if key in self._permanent: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
656 |
self._permanent.remove(key) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
657 |
else: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
658 |
self._transient.pop(key, None) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
659 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
660 |
def clear(self): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
661 |
with self._lock: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
662 |
self._clear() |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
663 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
664 |
def _clear(self): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
665 |
self._permanent = set() |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
666 |
self._transient = {} |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
667 |
self._data = {} |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
668 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
669 |
def _try_to_make_room(self): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
670 |
current_size = len(self._data) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
671 |
items = sorted(self._transient.items(), key=itemgetter(1)) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
672 |
level = 0 |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
673 |
for k, v in items: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
674 |
self._data.pop(k, None) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
675 |
self._transient.pop(k, None) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
676 |
if v > level: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
677 |
datalen = len(self._data) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
678 |
if datalen == 0: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
679 |
return |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
680 |
if (current_size - datalen) / datalen > .1: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
681 |
break |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
682 |
level = v |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
683 |
else: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
684 |
# we removed cruft but everything is permanent |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
685 |
if len(self._data) >= self._max: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
686 |
logger.warning('Cache %s is full.' % id(self)) |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
687 |
self._clear() |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
688 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
689 |
def _usage_report(self): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
690 |
with self._lock: |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
691 |
return {'itemcount': len(self._data), |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
692 |
'transientcount': len(self._transient), |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
693 |
'permanentcount': len(self._permanent)} |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
694 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
695 |
def popitem(self): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
696 |
raise NotImplementedError() |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
697 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
698 |
def setdefault(self, key, default=None): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
699 |
raise NotImplementedError() |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
700 |
|
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
701 |
def update(self, other): |
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7876
diff
changeset
|
702 |
raise NotImplementedError() |