author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 29 Sep 2009 12:38:48 +0200 | |
branch | stable |
changeset 3511 | 581de819106f |
parent 2658 | 5535857eeaa5 |
child 2770 | 356e9d7c356d |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
1 |
"""Base class for dynamically loaded objects accessible through the vregistry. |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
2 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
3 |
You'll also find some convenience classes to build selectors. |
0 | 4 |
|
5 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1756
diff
changeset
|
6 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 7 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1756
diff
changeset
|
8 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 9 |
""" |
10 |
__docformat__ = "restructuredtext en" |
|
11 |
||
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
12 |
import types |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
13 |
from logging import getLogger |
2002
031414d88003
be stricter on catched exception, fix Time parsing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2001
diff
changeset
|
14 |
from datetime import datetime, timedelta, time |
0 | 15 |
|
699 | 16 |
from logilab.common.decorators import classproperty |
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
17 |
from logilab.common.deprecation import deprecated |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
18 |
from logilab.common.logging_ext import set_log_methods |
447 | 19 |
|
20 |
from rql.nodes import VariableRef, SubQuery |
|
0 | 21 |
from rql.stmts import Union, Select |
22 |
||
1174 | 23 |
from cubicweb import Unauthorized, NoSelectableObject |
2001 | 24 |
from cubicweb.utils import UStringIO, ustrftime, strptime, todate, todatetime |
0 | 25 |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
875
diff
changeset
|
26 |
ONESECOND = timedelta(0, 1, 0) |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
27 |
CACHE_REGISTRY = {} |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
28 |
|
0 | 29 |
|
1524 | 30 |
class Cache(dict): |
0 | 31 |
def __init__(self): |
32 |
super(Cache, self).__init__() |
|
2099
0cba78f4fa12
[CWCache] yet another bugfix in cwcache: proper initalization of Cache.creation_date
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2098
diff
changeset
|
33 |
_now = datetime.now() |
0cba78f4fa12
[CWCache] yet another bugfix in cwcache: proper initalization of Cache.creation_date
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2098
diff
changeset
|
34 |
self.cache_creation_date = _now |
0cba78f4fa12
[CWCache] yet another bugfix in cwcache: proper initalization of Cache.creation_date
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2098
diff
changeset
|
35 |
self.latest_cache_lookup = _now |
1524 | 36 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
37 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
38 |
# selector base classes and operations ######################################## |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
39 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
40 |
def objectify_selector(selector_func): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
41 |
"""convenience decorator for simple selectors where a class definition |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
42 |
would be overkill:: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
43 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
44 |
@objectify_selector |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
45 |
def yes(cls, *args, **kwargs): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
46 |
return 1 |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
47 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
48 |
""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
49 |
return type(selector_func.__name__, (Selector,), |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
50 |
{'__call__': lambda self, *args, **kwargs: selector_func(*args, **kwargs)}) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
51 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
52 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
53 |
def _instantiate_selector(selector): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
54 |
"""ensures `selector` is a `Selector` instance |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
55 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
56 |
NOTE: This should only be used locally in build___select__() |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
57 |
XXX: then, why not do it ?? |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
58 |
""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
59 |
if isinstance(selector, types.FunctionType): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
60 |
return objectify_selector(selector)() |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
61 |
if isinstance(selector, type) and issubclass(selector, Selector): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
62 |
return selector() |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
63 |
return selector |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
64 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
65 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
66 |
class Selector(object): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
67 |
"""base class for selector classes providing implementation |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
68 |
for operators ``&`` and ``|`` |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
69 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
70 |
This class is only here to give access to binary operators, the |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
71 |
selector logic itself should be implemented in the __call__ method |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
72 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
73 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
74 |
a selector is called to help choosing the correct object for a |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
75 |
particular context by returning a score (`int`) telling how well |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
76 |
the class given as first argument apply to the given context. |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
77 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
78 |
0 score means that the class doesn't apply. |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
79 |
""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
80 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
81 |
@property |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
82 |
def func_name(self): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
83 |
# backward compatibility |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
84 |
return self.__class__.__name__ |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
85 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
86 |
def search_selector(self, selector): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
87 |
"""search for the given selector or selector instance in the selectors |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
88 |
tree. Return it of None if not found |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
89 |
""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
90 |
if self is selector: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
91 |
return self |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
92 |
if isinstance(selector, type) and isinstance(self, selector): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
93 |
return self |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
94 |
return None |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
95 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
96 |
def __str__(self): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
97 |
return self.__class__.__name__ |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
98 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
99 |
def __and__(self, other): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
100 |
return AndSelector(self, other) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
101 |
def __rand__(self, other): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
102 |
return AndSelector(other, self) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
103 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
104 |
def __or__(self, other): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
105 |
return OrSelector(self, other) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
106 |
def __ror__(self, other): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
107 |
return OrSelector(other, self) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
108 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
109 |
def __invert__(self): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
110 |
return NotSelector(self) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
111 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
112 |
# XXX (function | function) or (function & function) not managed yet |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
113 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
114 |
def __call__(self, cls, *args, **kwargs): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
115 |
return NotImplementedError("selector %s must implement its logic " |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
116 |
"in its __call__ method" % self.__class__) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
117 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
118 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
119 |
class MultiSelector(Selector): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
120 |
"""base class for compound selector classes""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
121 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
122 |
def __init__(self, *selectors): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
123 |
self.selectors = self.merge_selectors(selectors) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
124 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
125 |
def __str__(self): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
126 |
return '%s(%s)' % (self.__class__.__name__, |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
127 |
','.join(str(s) for s in self.selectors)) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
128 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
129 |
@classmethod |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
130 |
def merge_selectors(cls, selectors): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
131 |
"""deal with selector instanciation when necessary and merge |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
132 |
multi-selectors if possible: |
0 | 133 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
134 |
AndSelector(AndSelector(sel1, sel2), AndSelector(sel3, sel4)) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
135 |
==> AndSelector(sel1, sel2, sel3, sel4) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
136 |
""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
137 |
merged_selectors = [] |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
138 |
for selector in selectors: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
139 |
try: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
140 |
selector = _instantiate_selector(selector) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
141 |
except: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
142 |
pass |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
143 |
#assert isinstance(selector, Selector), selector |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
144 |
if isinstance(selector, cls): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
145 |
merged_selectors += selector.selectors |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
146 |
else: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
147 |
merged_selectors.append(selector) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
148 |
return merged_selectors |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
149 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
150 |
def search_selector(self, selector): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
151 |
"""search for the given selector or selector instance in the selectors |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
152 |
tree. Return it of None if not found |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
153 |
""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
154 |
for childselector in self.selectors: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
155 |
if childselector is selector: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
156 |
return childselector |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
157 |
found = childselector.search_selector(selector) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
158 |
if found is not None: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
159 |
return found |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
160 |
return None |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
161 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
162 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
163 |
class AndSelector(MultiSelector): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
164 |
"""and-chained selectors (formerly known as chainall)""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
165 |
def __call__(self, cls, *args, **kwargs): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
166 |
score = 0 |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
167 |
for selector in self.selectors: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
168 |
partscore = selector(cls, *args, **kwargs) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
169 |
if not partscore: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
170 |
return 0 |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
171 |
score += partscore |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
172 |
return score |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
173 |
|
1524 | 174 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
175 |
class OrSelector(MultiSelector): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
176 |
"""or-chained selectors (formerly known as chainfirst)""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
177 |
def __call__(self, cls, *args, **kwargs): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
178 |
for selector in self.selectors: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
179 |
partscore = selector(cls, *args, **kwargs) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
180 |
if partscore: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
181 |
return partscore |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
182 |
return 0 |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
183 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
184 |
class NotSelector(Selector): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
185 |
"""negation selector""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
186 |
def __init__(self, selector): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
187 |
self.selector = selector |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
188 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
189 |
def __call__(self, cls, *args, **kwargs): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
190 |
score = self.selector(cls, *args, **kwargs) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
191 |
return int(not score) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
192 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
193 |
def __str__(self): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
194 |
return 'NOT(%s)' % super(NotSelector, self).__str__() |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
195 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
196 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
197 |
class yes(Selector): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
198 |
"""return arbitrary score |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
199 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
200 |
default score of 0.5 so any other selector take precedence |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
201 |
""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
202 |
def __init__(self, score=0.5): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
203 |
self.score = score |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
204 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
205 |
def __call__(self, *args, **kwargs): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
206 |
return self.score |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
207 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
208 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
209 |
# the base class for all appobjects ############################################ |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
210 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
211 |
class AppObject(object): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
212 |
"""This is the base class for CubicWeb application objects which are |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
213 |
selected according to a context (usually at least a request and a result |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
214 |
set). |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
215 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
216 |
Concrete application objects classes are designed to be loaded by the |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
217 |
vregistry and should be accessed through it, not by direct instantiation. |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
218 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
219 |
The following attributes should be set on concret appobject classes: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
220 |
:__registry__: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
221 |
name of the registry for this object (string like 'views', |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
222 |
'templates'...) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
223 |
:id: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
224 |
object's identifier in the registry (string like 'main', |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
225 |
'primary', 'folder_box') |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
226 |
:__select__: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
227 |
class'selector |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
228 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
229 |
Moreover, the `__abstract__` attribute may be set to True to indicate |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
230 |
that a appobject is abstract and should not be registered. |
1524 | 231 |
|
0 | 232 |
At registration time, the following attributes are set on the class: |
233 |
:vreg: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2144
diff
changeset
|
234 |
the instance's registry |
0 | 235 |
:schema: |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2144
diff
changeset
|
236 |
the instance's schema |
0 | 237 |
:config: |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2144
diff
changeset
|
238 |
the instance's configuration |
0 | 239 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
240 |
At selection time, the following attributes are set on the instance: |
0 | 241 |
:req: |
242 |
current request |
|
243 |
:rset: |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
244 |
context result set or None |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
245 |
:row: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
246 |
if a result set is set and the context is about a particular cell in the |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
247 |
result set, and not the result set as a whole, specify the row number we |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
248 |
are interested in, else None |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
249 |
:col: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
250 |
if a result set is set and the context is about a particular cell in the |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
251 |
result set, and not the result set as a whole, specify the col number we |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
252 |
are interested in, else None |
0 | 253 |
""" |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
254 |
__registry__ = None |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
255 |
id = None |
805
6e99feeba28b
set a default selector on base app objects class
sylvain.thenault@logilab.fr
parents:
802
diff
changeset
|
256 |
__select__ = yes() |
0 | 257 |
|
258 |
@classmethod |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
259 |
def classid(cls): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
260 |
"""returns a unique identifier for the appobject""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
261 |
return '%s.%s' % (cls.__module__, cls.__name__) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
262 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
263 |
# XXX bw compat code |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
264 |
@classmethod |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
265 |
def build___select__(cls): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
266 |
for klass in cls.mro(): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
267 |
if klass.__name__ == 'AppObject': |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
268 |
continue # the bw compat __selector__ is there |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
269 |
klassdict = klass.__dict__ |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
270 |
if ('__select__' in klassdict and '__selectors__' in klassdict |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
271 |
and '__selgenerated__' not in klassdict): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
272 |
raise TypeError("__select__ and __selectors__ can't be used together on class %s" % cls) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
273 |
if '__selectors__' in klassdict and '__selgenerated__' not in klassdict: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
274 |
cls.__selgenerated__ = True |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
275 |
# case where __selectors__ is defined locally (but __select__ |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
276 |
# is in a parent class) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
277 |
selectors = klassdict['__selectors__'] |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
278 |
if len(selectors) == 1: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
279 |
# micro optimization: don't bother with AndSelector if there's |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
280 |
# only one selector |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
281 |
select = _instantiate_selector(selectors[0]) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
282 |
else: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
283 |
select = AndSelector(*selectors) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
284 |
cls.__select__ = select |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
285 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
286 |
@classmethod |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
287 |
def registered(cls, registry): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
288 |
"""called by the registry when the appobject has been registered. |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
289 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
290 |
It must return the object that will be actually registered (this may be |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
291 |
the right hook to create an instance for example). By default the |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
292 |
appobject is returned without any transformation. |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
293 |
""" |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
294 |
cls.build___select__() |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
295 |
cls.vreg = registry.vreg |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
296 |
cls.schema = registry.schema |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
297 |
cls.config = registry.config |
0 | 298 |
cls.register_properties() |
299 |
return cls |
|
1524 | 300 |
|
1282
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1174
diff
changeset
|
301 |
@classmethod |
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1174
diff
changeset
|
302 |
def vreg_initialization_completed(cls): |
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1174
diff
changeset
|
303 |
pass |
1524 | 304 |
|
0 | 305 |
# Eproperties definition: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
306 |
# key: id of the property (the actual CWProperty key is build using |
0 | 307 |
# <registry name>.<obj id>.<property id> |
308 |
# value: tuple (property type, vocabfunc, default value, property description) |
|
309 |
# possible types are those used by `logilab.common.configuration` |
|
310 |
# |
|
311 |
# notice that when it exists multiple objects with the same id (adaptation, |
|
312 |
# overriding) only the first encountered definition is considered, so those |
|
313 |
# objects can't try to have different default values for instance. |
|
1524 | 314 |
|
0 | 315 |
property_defs = {} |
1524 | 316 |
|
0 | 317 |
@classmethod |
318 |
def register_properties(cls): |
|
319 |
for propid, pdef in cls.property_defs.items(): |
|
320 |
pdef = pdef.copy() # may be shared |
|
321 |
pdef['default'] = getattr(cls, propid, pdef['default']) |
|
322 |
pdef['sitewide'] = getattr(cls, 'site_wide', pdef.get('sitewide')) |
|
323 |
cls.vreg.register_property(cls.propkey(propid), **pdef) |
|
1524 | 324 |
|
0 | 325 |
@classmethod |
326 |
def propkey(cls, propid): |
|
327 |
return '%s.%s.%s' % (cls.__registry__, cls.id, propid) |
|
699 | 328 |
|
329 |
@classproperty |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
330 |
@deprecated('use __select__ and & or | operators') |
699 | 331 |
def __selectors__(cls): |
726
88a74f590986
improve __selectors__() so that it always return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
721
diff
changeset
|
332 |
selector = cls.__select__ |
88a74f590986
improve __selectors__() so that it always return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
721
diff
changeset
|
333 |
if isinstance(selector, AndSelector): |
732
45c3414ac002
__selectors__ must return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
726
diff
changeset
|
334 |
return tuple(selector.selectors) |
726
88a74f590986
improve __selectors__() so that it always return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
721
diff
changeset
|
335 |
if not isinstance(selector, tuple): |
88a74f590986
improve __selectors__() so that it always return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
721
diff
changeset
|
336 |
selector = (selector,) |
88a74f590986
improve __selectors__() so that it always return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
721
diff
changeset
|
337 |
return selector |
1524 | 338 |
|
1143
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1130
diff
changeset
|
339 |
def __init__(self, req=None, rset=None, row=None, col=None, **extra): |
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
340 |
super(AppObject, self).__init__() |
0 | 341 |
self.req = req |
342 |
self.rset = rset |
|
1143
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1130
diff
changeset
|
343 |
self.row = row |
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1130
diff
changeset
|
344 |
self.col = col |
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1130
diff
changeset
|
345 |
self.extra_kwargs = extra |
1524 | 346 |
|
0 | 347 |
def get_cache(self, cachename): |
348 |
""" |
|
349 |
NOTE: cachename should be dotted names as in : |
|
350 |
- cubicweb.mycache |
|
1524 | 351 |
- cubes.blog.mycache |
0 | 352 |
- etc. |
353 |
""" |
|
354 |
if cachename in CACHE_REGISTRY: |
|
355 |
cache = CACHE_REGISTRY[cachename] |
|
356 |
else: |
|
2098
13aab3775af7
[CWCache] fix CWCache validity test
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2002
diff
changeset
|
357 |
cache = CACHE_REGISTRY[cachename] = Cache() |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
875
diff
changeset
|
358 |
_now = datetime.now() |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
875
diff
changeset
|
359 |
if _now > cache.latest_cache_lookup + ONESECOND: |
1524 | 360 |
ecache = self.req.execute('Any C,T WHERE C is CWCache, C name %(name)s, C timestamp T', |
0 | 361 |
{'name':cachename}).get_entity(0,0) |
362 |
cache.latest_cache_lookup = _now |
|
363 |
if not ecache.valid(cache.cache_creation_date): |
|
1130
17ff4d4bfbd0
should call clear, not empth (thanks pylint)
sylvain.thenault@logilab.fr
parents:
1129
diff
changeset
|
364 |
cache.clear() |
0 | 365 |
cache.cache_creation_date = _now |
366 |
return cache |
|
367 |
||
368 |
def propval(self, propid): |
|
369 |
assert self.req |
|
370 |
return self.req.property_value(self.propkey(propid)) |
|
1524 | 371 |
|
0 | 372 |
def limited_rql(self): |
373 |
"""return a printable rql for the result set associated to the object, |
|
374 |
with limit/offset correctly set according to maximum page size and |
|
375 |
currently displayed page when necessary |
|
376 |
""" |
|
377 |
# try to get page boundaries from the navigation component |
|
378 |
# XXX we should probably not have a ref to this component here (eg in |
|
379 |
# cubicweb.common) |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
380 |
nav = self.vreg['components'].select_object('navigation', self.req, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
381 |
rset=self.rset) |
0 | 382 |
if nav: |
383 |
start, stop = nav.page_boundaries() |
|
384 |
rql = self._limit_offset_rql(stop - start, start) |
|
385 |
# result set may have be limited manually in which case navigation won't |
|
386 |
# apply |
|
387 |
elif self.rset.limited: |
|
388 |
rql = self._limit_offset_rql(*self.rset.limited) |
|
389 |
# navigation component doesn't apply and rset has not been limited, no |
|
390 |
# need to limit query |
|
391 |
else: |
|
392 |
rql = self.rset.printable_rql() |
|
393 |
return rql |
|
1524 | 394 |
|
0 | 395 |
def _limit_offset_rql(self, limit, offset): |
396 |
rqlst = self.rset.syntax_tree() |
|
397 |
if len(rqlst.children) == 1: |
|
398 |
select = rqlst.children[0] |
|
399 |
olimit, ooffset = select.limit, select.offset |
|
400 |
select.limit, select.offset = limit, offset |
|
401 |
rql = rqlst.as_string(kwargs=self.rset.args) |
|
402 |
# restore original limit/offset |
|
403 |
select.limit, select.offset = olimit, ooffset |
|
404 |
else: |
|
405 |
newselect = Select() |
|
406 |
newselect.limit = limit |
|
407 |
newselect.offset = offset |
|
408 |
aliases = [VariableRef(newselect.get_variable(vref.name, i)) |
|
409 |
for i, vref in enumerate(rqlst.selection)] |
|
410 |
newselect.set_with([SubQuery(aliases, rqlst)], check=False) |
|
411 |
newunion = Union() |
|
412 |
newunion.append(newselect) |
|
413 |
rql = rqlst.as_string(kwargs=self.rset.args) |
|
414 |
rqlst.parent = None |
|
415 |
return rql |
|
1524 | 416 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
417 |
def view(self, __vid, rset=None, __fallback_oid=None, __registry='views', |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
418 |
**kwargs): |
1985
9c1db4e06095
move view method's logic on the registry, so it's easier to call it from outside an appobject. Also make rset argument optional
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1984
diff
changeset
|
419 |
"""shortcut to self.vreg.view method avoiding to pass self.req""" |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
420 |
return self.vreg[__registry].render(__vid, self.req, __fallback_oid, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
421 |
rset=rset, **kwargs) |
1524 | 422 |
|
1756
42d87dedd631
move initialize_varmaker impl to base app object
sylvain.thenault@logilab.fr
parents:
1723
diff
changeset
|
423 |
def initialize_varmaker(self): |
42d87dedd631
move initialize_varmaker impl to base app object
sylvain.thenault@logilab.fr
parents:
1723
diff
changeset
|
424 |
varmaker = self.req.get_page_data('rql_varmaker') |
42d87dedd631
move initialize_varmaker impl to base app object
sylvain.thenault@logilab.fr
parents:
1723
diff
changeset
|
425 |
if varmaker is None: |
42d87dedd631
move initialize_varmaker impl to base app object
sylvain.thenault@logilab.fr
parents:
1723
diff
changeset
|
426 |
varmaker = self.req.varmaker |
42d87dedd631
move initialize_varmaker impl to base app object
sylvain.thenault@logilab.fr
parents:
1723
diff
changeset
|
427 |
self.req.set_page_data('rql_varmaker', varmaker) |
42d87dedd631
move initialize_varmaker impl to base app object
sylvain.thenault@logilab.fr
parents:
1723
diff
changeset
|
428 |
self.varmaker = varmaker |
42d87dedd631
move initialize_varmaker impl to base app object
sylvain.thenault@logilab.fr
parents:
1723
diff
changeset
|
429 |
|
0 | 430 |
# url generation methods ################################################## |
1524 | 431 |
|
0 | 432 |
controller = 'view' |
1524 | 433 |
|
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
434 |
def build_url(self, *args, **kwargs): |
0 | 435 |
"""return an absolute URL using params dictionary key/values as URL |
436 |
parameters. Values are automatically URL quoted, and the |
|
437 |
publishing method to use may be specified or will be guessed. |
|
438 |
""" |
|
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
439 |
# use *args since we don't want first argument to be "anonymous" to |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
440 |
# avoid potential clash with kwargs |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
441 |
if args: |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
442 |
assert len(args) == 1, 'only 0 or 1 non-named-argument expected' |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
443 |
method = args[0] |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
444 |
else: |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
445 |
method = None |
0 | 446 |
# XXX I (adim) think that if method is passed explicitly, we should |
447 |
# not try to process it and directly call req.build_url() |
|
448 |
if method is None: |
|
449 |
method = self.controller |
|
450 |
if method == 'view' and self.req.from_controller() == 'view' and \ |
|
451 |
not '_restpath' in kwargs: |
|
452 |
method = self.req.relative_path(includeparams=False) or 'view' |
|
453 |
return self.req.build_url(method, **kwargs) |
|
454 |
||
455 |
# various resources accessors ############################################# |
|
1524 | 456 |
|
0 | 457 |
def entity(self, row, col=0): |
458 |
"""short cut to get an entity instance for a particular row/column |
|
459 |
(col default to 0) |
|
460 |
""" |
|
461 |
return self.rset.get_entity(row, col) |
|
1524 | 462 |
|
0 | 463 |
def complete_entity(self, row, col=0, skip_bytes=True): |
464 |
"""short cut to get an completed entity instance for a particular |
|
465 |
row (all instance's attributes have been fetched) |
|
466 |
""" |
|
467 |
entity = self.entity(row, col) |
|
468 |
entity.complete(skip_bytes=skip_bytes) |
|
469 |
return entity |
|
470 |
||
471 |
def user_rql_callback(self, args, msg=None): |
|
472 |
"""register a user callback to execute some rql query and return an url |
|
473 |
to call it ready to be inserted in html |
|
474 |
""" |
|
475 |
def rqlexec(req, rql, args=None, key=None): |
|
476 |
req.execute(rql, args, key) |
|
477 |
return self.user_callback(rqlexec, args, msg) |
|
1524 | 478 |
|
0 | 479 |
def user_callback(self, cb, args, msg=None, nonify=False): |
480 |
"""register the given user callback and return an url to call it ready to be |
|
481 |
inserted in html |
|
482 |
""" |
|
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1524
diff
changeset
|
483 |
from simplejson import dumps |
0 | 484 |
self.req.add_js('cubicweb.ajax.js') |
485 |
cbname = self.req.register_onetime_callback(cb, *args) |
|
1524 | 486 |
msg = dumps(msg or '') |
0 | 487 |
return "javascript:userCallbackThenReloadPage('%s', %s)" % ( |
488 |
cbname, msg) |
|
489 |
||
490 |
# formating methods ####################################################### |
|
491 |
||
492 |
def tal_render(self, template, variables): |
|
493 |
"""render a precompiled page template with variables in the given |
|
494 |
dictionary as context |
|
495 |
""" |
|
1637 | 496 |
from cubicweb.ext.tal import CubicWebContext |
0 | 497 |
context = CubicWebContext() |
498 |
context.update({'self': self, 'rset': self.rset, '_' : self.req._, |
|
499 |
'req': self.req, 'user': self.req.user}) |
|
500 |
context.update(variables) |
|
501 |
output = UStringIO() |
|
502 |
template.expand(context, output) |
|
503 |
return output.getvalue() |
|
504 |
||
505 |
def format_date(self, date, date_format=None, time=False): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2144
diff
changeset
|
506 |
"""return a string for a date time according to instance's |
0 | 507 |
configuration |
508 |
""" |
|
509 |
if date: |
|
510 |
if date_format is None: |
|
511 |
if time: |
|
512 |
date_format = self.req.property_value('ui.datetime-format') |
|
513 |
else: |
|
514 |
date_format = self.req.property_value('ui.date-format') |
|
515 |
return ustrftime(date, date_format) |
|
516 |
return u'' |
|
517 |
||
518 |
def format_time(self, time): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2144
diff
changeset
|
519 |
"""return a string for a time according to instance's |
0 | 520 |
configuration |
521 |
""" |
|
522 |
if time: |
|
523 |
return ustrftime(time, self.req.property_value('ui.time-format')) |
|
524 |
return u'' |
|
525 |
||
526 |
def format_float(self, num): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2144
diff
changeset
|
527 |
"""return a string for floating point number according to instance's |
0 | 528 |
configuration |
529 |
""" |
|
530 |
if num: |
|
531 |
return self.req.property_value('ui.float-format') % num |
|
532 |
return u'' |
|
1524 | 533 |
|
1984
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
534 |
def parse_datetime(self, value, etype='Datetime'): |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
535 |
"""get a datetime or time from a string (according to etype) |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
536 |
Datetime formatted as Date are accepted |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
537 |
""" |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
538 |
assert etype in ('Datetime', 'Date', 'Time'), etype |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
539 |
# XXX raise proper validation error |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
540 |
if etype == 'Datetime': |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
541 |
format = self.req.property_value('ui.datetime-format') |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
542 |
try: |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
543 |
return todatetime(strptime(value, format)) |
2002
031414d88003
be stricter on catched exception, fix Time parsing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2001
diff
changeset
|
544 |
except ValueError: |
1984
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
545 |
pass |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
546 |
elif etype == 'Time': |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
547 |
format = self.req.property_value('ui.time-format') |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
548 |
try: |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
549 |
# (adim) I can't find a way to parse a Time with a custom format |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
550 |
date = strptime(value, format) # this returns a DateTime |
2002
031414d88003
be stricter on catched exception, fix Time parsing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2001
diff
changeset
|
551 |
return time(date.hour, date.minute, date.second) |
031414d88003
be stricter on catched exception, fix Time parsing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2001
diff
changeset
|
552 |
except ValueError: |
1984
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
553 |
raise ValueError('can\'t parse %r (expected %s)' % (value, format)) |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
554 |
try: |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
555 |
format = self.req.property_value('ui.date-format') |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
556 |
dt = strptime(value, format) |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
557 |
if etype == 'Datetime': |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
558 |
return todatetime(dt) |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
559 |
return todate(dt) |
2002
031414d88003
be stricter on catched exception, fix Time parsing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2001
diff
changeset
|
560 |
except ValueError: |
1984
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
561 |
raise ValueError('can\'t parse %r (expected %s)' % (value, format)) |
8d571df6012a
move parse_datetime on appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
562 |
|
0 | 563 |
# security related methods ################################################ |
1524 | 564 |
|
0 | 565 |
def ensure_ro_rql(self, rql): |
566 |
"""raise an exception if the given rql is not a select query""" |
|
567 |
first = rql.split(' ', 1)[0].lower() |
|
568 |
if first in ('insert', 'set', 'delete'): |
|
569 |
raise Unauthorized(self.req._('only select queries are authorized')) |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
570 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
571 |
set_log_methods(AppObject, getLogger('cubicweb.appobject')) |