|
1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """ |
|
19 |
|
20 The `AppObject` class |
|
21 --------------------- |
|
22 |
|
23 The AppObject class is the base class for all dynamically loaded objects |
|
24 (application objects) accessible through the vregistry. |
|
25 |
|
26 We can find a certain number of attributes and methods defined in this class and |
|
27 common to all the application objects. |
|
28 |
|
29 """ |
|
30 __docformat__ = "restructuredtext en" |
|
31 |
|
32 from logging import getLogger |
|
33 |
|
34 from logilab.common.deprecation import deprecated, class_renamed |
|
35 from logilab.common.logging_ext import set_log_methods |
|
36 |
|
37 # first line imports for bw compat |
|
38 from logilab.common.registry import (objectify_predicate, traced_selection, Predicate, |
|
39 RegistrableObject, yes) |
|
40 |
|
41 |
|
42 objectify_selector = deprecated('[3.15] objectify_selector has been ' |
|
43 'renamed to objectify_predicates in ' |
|
44 'logilab.common.registry')(objectify_predicate) |
|
45 traced_selection = deprecated('[3.15] traced_selection has been ' |
|
46 'moved to logilab.common.registry')(traced_selection) |
|
47 Selector = class_renamed('Selector', Predicate, |
|
48 '[3.15] Selector has been renamed to Predicate ' |
|
49 'in logilab.common.registry') |
|
50 |
|
51 @deprecated('[3.15] lltrace decorator can now be removed') |
|
52 def lltrace(func): |
|
53 return func |
|
54 |
|
55 # the base class for all appobjects ############################################ |
|
56 |
|
57 class AppObject(RegistrableObject): |
|
58 """This is the base class for CubicWeb application objects which are |
|
59 selected in a request context. |
|
60 |
|
61 The following attributes should be set on concrete appobject classes: |
|
62 |
|
63 At selection time, the following attributes are set on the instance: |
|
64 |
|
65 :attr:`_cw` |
|
66 current request |
|
67 :attr:`cw_extra_kwargs` |
|
68 other received arguments |
|
69 |
|
70 And also the following, only if `rset` is found in arguments (in which case |
|
71 rset/row/col will be removed from `cwextra_kwargs`): |
|
72 |
|
73 :attr:`cw_rset` |
|
74 context result set or None |
|
75 |
|
76 :attr:`cw_row` |
|
77 if a result set is set and the context is about a particular cell in the |
|
78 result set, and not the result set as a whole, specify the row number we |
|
79 are interested in, else None |
|
80 |
|
81 :attr:`cw_col` |
|
82 if a result set is set and the context is about a particular cell in the |
|
83 result set, and not the result set as a whole, specify the col number we |
|
84 are interested in, else None |
|
85 |
|
86 |
|
87 .. Note:: |
|
88 |
|
89 * do not inherit directly from this class but from a more specific class |
|
90 such as `AnyEntity`, `EntityView`, `AnyRsetView`, `Action`... |
|
91 |
|
92 """ |
|
93 __select__ = yes() |
|
94 |
|
95 @classmethod |
|
96 def __registered__(cls, registry): |
|
97 """called by the registry when the appobject has been registered. |
|
98 |
|
99 It must return the object that will be actually registered (this may be |
|
100 the right hook to create an instance for example). By default the |
|
101 appobject is returned without any transformation. |
|
102 """ |
|
103 pdefs = getattr(cls, 'cw_property_defs', {}) |
|
104 for propid, pdef in pdefs.items(): |
|
105 pdef = pdef.copy() # may be shared |
|
106 pdef['default'] = getattr(cls, propid, pdef['default']) |
|
107 pdef['sitewide'] = getattr(cls, 'site_wide', pdef.get('sitewide')) |
|
108 registry.vreg.register_property(cls._cwpropkey(propid), **pdef) |
|
109 assert callable(cls.__select__), cls |
|
110 return cls |
|
111 |
|
112 def __init__(self, req, **extra): |
|
113 super(AppObject, self).__init__() |
|
114 self._cw = req |
|
115 try: |
|
116 self.cw_rset = extra.pop('rset') |
|
117 self.cw_row = extra.pop('row', None) |
|
118 self.cw_col = extra.pop('col', None) |
|
119 except KeyError: |
|
120 pass |
|
121 self.cw_extra_kwargs = extra |
|
122 |
|
123 # persistent class properties ############################################## |
|
124 # |
|
125 # optional `cw_property_defs` dict on a class defines available persistent |
|
126 # properties for this class: |
|
127 # |
|
128 # * key: id of the property (the actual CWProperty key is build using |
|
129 # <registry name>.<obj id>.<property id> |
|
130 # * value: tuple (property type, vocabfunc, default value, property description) |
|
131 # possible types are those used by `logilab.common.configuration` |
|
132 # |
|
133 # notice that when it exists multiple objects with the same id (adaptation, |
|
134 # overriding) only the first encountered definition is considered, so those |
|
135 # objects can't try to have different default values for instance. |
|
136 # |
|
137 # you can then access to a property value using self.cw_propval, where self |
|
138 # is an instance of class |
|
139 |
|
140 @classmethod |
|
141 def _cwpropkey(cls, propid): |
|
142 """return cw property key for the property of the given id for this |
|
143 class |
|
144 """ |
|
145 return '%s.%s.%s' % (cls.__registry__, cls.__regid__, propid) |
|
146 |
|
147 def cw_propval(self, propid): |
|
148 """return cw property value associated to key |
|
149 |
|
150 <cls.__registry__>.<cls.id>.<propid> |
|
151 """ |
|
152 return self._cw.property_value(self._cwpropkey(propid)) |
|
153 |
|
154 # these are overridden by set_log_methods below |
|
155 # only defining here to prevent pylint from complaining |
|
156 info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None |
|
157 |
|
158 set_log_methods(AppObject, getLogger('cubicweb.appobject')) |
|
159 |
|
160 # defined here to avoid warning on usage on the AppObject class |
|
161 yes = deprecated('[3.15] yes has been moved to logilab.common.registry')(yes) |