1 """This file contains some basic selectors required by application objects. |
1 """This file contains some basic selectors required by application objects. |
2 |
2 |
3 A selector is responsible to score how well an object may be used with a |
3 A selector is responsible to score how well an object may be used with a |
4 given result set (publishing time selection) |
4 given result set (publishing time selection) |
|
5 |
|
6 If you have trouble with selectors, especially if the objet (typically |
|
7 a view or a component) you want to use is not selected and you want to |
|
8 know which one(s) of its selectors fail (e.g. returns 0), you can use |
|
9 `traced_selection` or even direclty `TRACED_OIDS`. |
|
10 |
|
11 `TRACED_OIDS` is a tuple of traced object ids. The special value |
|
12 'all' may be used to log selectors for all objects. |
|
13 |
|
14 For instance, say that the following code yields a `NoSelectableObject` |
|
15 exception:: |
|
16 |
|
17 self.view('calendar', myrset) |
|
18 |
|
19 You can log the selectors involved for *calendar* by replacing the line |
|
20 above by:: |
|
21 |
|
22 # in Python2.5 |
|
23 from cubicweb.selectors import traced_selection |
|
24 with traced_selection(): |
|
25 self.view('calendar', myrset) |
|
26 |
|
27 # in Python2.4 |
|
28 from cubicweb import selectors |
|
29 selectors.TRACED_OIDS = ('calendar',) |
|
30 self.view('calendar', myrset) |
|
31 selectors.TRACED_OIDS = () |
|
32 |
|
33 |
5 |
34 |
6 :organization: Logilab |
35 :organization: Logilab |
7 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
36 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
37 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
9 """ |
38 """ |
10 |
39 |
11 __docformat__ = "restructuredtext en" |
40 __docformat__ = "restructuredtext en" |
|
41 |
|
42 import logging |
12 |
43 |
13 from logilab.common.compat import all |
44 from logilab.common.compat import all |
14 from logilab.common.deprecation import deprecated_function |
45 from logilab.common.deprecation import deprecated_function |
15 |
46 |
16 from cubicweb import Unauthorized, NoSelectableObject, role |
47 from cubicweb import Unauthorized, NoSelectableObject, role |
17 from cubicweb.cwvreg import DummyCursorError |
48 from cubicweb.cwvreg import DummyCursorError |
18 from cubicweb.vregistry import chainall, chainfirst, NoSelectableObject |
49 from cubicweb.vregistry import chainall, chainfirst, NoSelectableObject |
19 from cubicweb.cwconfig import CubicWebConfiguration |
50 from cubicweb.cwconfig import CubicWebConfiguration |
20 from cubicweb.schema import split_expression |
51 from cubicweb.schema import split_expression |
21 |
52 |
|
53 |
|
54 # helpers for debugging selectors |
|
55 SELECTOR_LOGGER = logging.getLogger('cubicweb.selectors') |
|
56 TRACED_OIDS = () |
22 |
57 |
23 def lltrace(selector): |
58 def lltrace(selector): |
24 # don't wrap selectors if not in development mode |
59 # don't wrap selectors if not in development mode |
25 if CubicWebConfiguration.mode == 'installed': |
60 if CubicWebConfiguration.mode == 'installed': |
26 return selector |
61 return selector |
27 def traced(cls, *args, **kwargs): |
62 def traced(cls, *args, **kwargs): |
28 ret = selector(cls, *args, **kwargs) |
63 ret = selector(cls, *args, **kwargs) |
29 cls.lldebug('selector %s returned %s for %s', selector.__name__, ret, cls) |
64 if TRACED_OIDS == 'all' or cls.id in TRACED_OIDS: |
|
65 SELECTOR_LOGGER.warning('selector %s returned %s for %s', selector.__name__, ret, cls) |
30 return ret |
66 return ret |
31 return traced |
67 return traced |
|
68 |
|
69 class traced_selection(object): |
|
70 """selector debugging helper. |
|
71 |
|
72 Typical usage is : |
|
73 |
|
74 >>> with traced_selection(): |
|
75 ... # some code in which you want to debug selectors |
|
76 ... # for all objects |
|
77 |
|
78 or |
|
79 |
|
80 >>> with traced_selection( ('oid1', 'oid2') ): |
|
81 ... # some code in which you want to debug selectors |
|
82 ... # for objects with id 'oid1' and 'oid2' |
32 |
83 |
|
84 """ |
|
85 def __init__(self, traced='all'): |
|
86 self.traced = traced |
|
87 |
|
88 def __enter__(self): |
|
89 global TRACED_OIDS |
|
90 TRACED_OIDS = self.traced |
|
91 |
|
92 def __exit__(self, exctype, exc, traceback): |
|
93 global TRACED_OIDS |
|
94 TRACED_OIDS = () |
|
95 return traceback is None |
|
96 |
33 # very basic selectors ######################################################## |
97 # very basic selectors ######################################################## |
34 |
98 |
35 def yes(cls, *args, **kwargs): |
99 def yes(cls, *args, **kwargs): |
36 """accept everything""" |
100 """accept everything""" |
37 return 1 |
101 return 1 |