16 *context* and *request* and *view*. |
16 *context* and *request* and *view*. |
17 |
17 |
18 See :ref:`zca` to see how PyAMS can help components management. |
18 See :ref:`zca` to see how PyAMS can help components management. |
19 """ |
19 """ |
20 |
20 |
|
21 import logging |
|
22 |
|
23 import venusian |
|
24 from zope.annotation.interfaces import IAnnotations |
|
25 from zope.interface import alsoProvides, implementedBy |
|
26 from zope.lifecycleevent import ObjectCreatedEvent |
|
27 from zope.location import locate as zope_locate |
|
28 |
|
29 from pyams_utils.factory import get_object_factory, is_interface |
|
30 from pyams_utils.registry import get_current_registry |
|
31 |
|
32 |
21 __docformat__ = 'restructuredtext' |
33 __docformat__ = 'restructuredtext' |
22 |
34 |
23 |
|
24 # import standard library |
|
25 import logging |
|
26 logger = logging.getLogger('PyAMS (utils)') |
35 logger = logging.getLogger('PyAMS (utils)') |
27 |
|
28 import venusian |
|
29 |
|
30 # import interfaces |
|
31 from zope.annotation.interfaces import IAnnotations |
|
32 |
|
33 # import packages |
|
34 from pyams_utils.factory import get_object_factory, is_interface |
|
35 from pyams_utils.registry import get_current_registry |
|
36 from zope.interface import implementedBy, alsoProvides, Interface |
|
37 from zope.lifecycleevent import ObjectCreatedEvent |
|
38 from zope.location import locate as zope_locate |
|
39 |
36 |
40 |
37 |
41 class ContextAdapter(object): |
38 class ContextAdapter(object): |
42 """Context adapter""" |
39 """Context adapter""" |
43 |
40 |
137 """Get an adapter via object's annotations, creating it if not existent |
134 """Get an adapter via object's annotations, creating it if not existent |
138 |
135 |
139 :param object context: context object which should be adapted |
136 :param object context: context object which should be adapted |
140 :param str key: annotations key to look for |
137 :param str key: annotations key to look for |
141 :param factory: if annotations key is not found, this is the factory which will be used to |
138 :param factory: if annotations key is not found, this is the factory which will be used to |
142 create a new object; if factory is None and is requested object can't be found, None is returned |
139 create a new object; factory can be a class or callable object, or an interface for which |
|
140 a factory has been registered; if factory is None and is requested object can't be found, |
|
141 None is returned |
143 :param markers: if not None, list of marker interfaces which created adapter should provide |
142 :param markers: if not None, list of marker interfaces which created adapter should provide |
144 :param bool=True notify: if 'False', no notification event will be sent on object creation |
143 :param bool=True notify: if 'False', no notification event will be sent on object creation |
145 :param bool=True locate: if 'False', the new object is not attached to any parent |
144 :param bool=True locate: if 'False', the new object is not attached to any parent |
146 :param object=None parent: parent to which new object is attached |
145 :param object=None parent: parent to which new object is attached; if None, object is |
147 :param str=None name: if locate is not False, this is the name with which the new object is attached |
146 attached to context |
148 to it's parent. |
147 :param str=None name: if locate is not False, this is the name with which the new object is |
149 :param callback: if not None, callback function which will be called after |
148 attached to it's parent |
|
149 :param callback: if not None, callback function which will be called after object creation |
150 """ |
150 """ |
151 annotations = IAnnotations(context, None) |
151 annotations = IAnnotations(context, None) |
152 if annotations is None: |
152 if annotations is None: |
153 return None |
153 return None |
154 adapter = annotations.get(key) |
154 adapter = annotations.get(key) |