/usr/share/pyshared/zope/annotation/README.txt is in python-zope.annotation 3.6.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | ==================
Object Annotations
==================
This package provides a mechanism to store additional information about
objects without need to modify object class.
Annotation factories
--------------------
There is more to document about annotations, but we'll just sketch out
a scenario on how to use the annotation factory for now. This is one
of the easiest ways to use annotations -- basically you can see them
as persistent, writable adapters.
First, let's make a persistent object we can create annotations for:
>>> from zope import interface
>>> class IFoo(interface.Interface):
... pass
>>> from zope.annotation.interfaces import IAttributeAnnotatable
>>> from persistent import Persistent
>>> class Foo(Persistent):
... interface.implements(IFoo, IAttributeAnnotatable)
We directly say that Foo implements IAttributeAnnotatable here. In
practice this is often done in ZCML, using the `implements`
subdirective of the `content` or `class` directive.
Now let's create an annotation for this:
>>> class IBar(interface.Interface):
... a = interface.Attribute('A')
... b = interface.Attribute('B')
>>> from zope import component
>>> class Bar(Persistent):
... interface.implements(IBar)
... component.adapts(IFoo)
... def __init__(self):
... self.a = 1
... self.b = 2
Note that the annotation implementation does not expect any arguments
to its `__init__`. Otherwise it's basically an adapter.
Now, we'll register the annotation as an adapter. To do this we use
the `factory` function provided by `zope.annotation`:
>>> from zope.annotation import factory
>>> component.provideAdapter(factory(Bar))
Note that we do not need to specify what the adapter provides or what
it adapts - we already do this on the annotation class itself.
Now let's make an instance of `Foo`, and make an annotation for it.
>>> foo = Foo()
>>> bar = IBar(foo)
>>> bar.a
1
>>> bar.b
2
We'll change `a` and get the annotation again. Our change is still
there:
>>> bar.a = 3
>>> IBar(foo).a
3
Of course it's still different for another instance of `Foo`:
>>> foo2 = Foo()
>>> IBar(foo2).a
1
What if our annotation does not provide what it adapts with
`component.adapts`? It will complain:
>>> class IQux(interface.Interface):
... pass
>>> class Qux(Persistent):
... interface.implements(IQux)
>>> component.provideAdapter(factory(Qux)) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: Missing 'zope.component.adapts' on annotation
It's possible to provide an annotation with an explicit key. (If the
key is not supplied, the key is deduced from the annotation's dotted
name, provided it is a class.)
>>> class IHoi(interface.Interface):
... pass
>>> class Hoi(Persistent):
... interface.implements(IHoi)
... component.adapts(IFoo)
>>> component.provideAdapter(factory(Hoi, 'my.unique.key'))
>>> isinstance(IHoi(foo), Hoi)
True
Location
--------
Annotation factories are put into the location hierarchy with their parent
pointing to the annotated object and the name to the dotted name of the
annotation's class (or the name the adapter was registered under):
>>> foo3 = Foo()
>>> new_hoi = IHoi(foo3)
>>> new_hoi.__parent__
<Foo object at 0x...>
>>> new_hoi.__name__
'my.unique.key'
>>> import zope.location.interfaces
>>> zope.location.interfaces.ILocation.providedBy(new_hoi)
True
Please notice, that our Hoi object does not implement ILocation, so a
location proxy will be used. This has to be re-established every time we
retrieve the object
(Guard against former bug: proxy wasn't established when the annotation
existed already.)
>>> old_hoi = IHoi(foo3)
>>> old_hoi.__parent__
<Foo object at 0x...>
>>> old_hoi.__name__
'my.unique.key'
>>> zope.location.interfaces.ILocation.providedBy(old_hoi)
True
LocationProxies
---------------
Suppose your annotation proxy provides ILocation.
>>> class IPolloi(interface.Interface):
... pass
>>> class Polloi(Persistent):
... interface.implements(IPolloi, zope.location.interfaces.ILocation)
... component.adapts(IFoo)
... __name__ = __parent__ = 0
>>> component.provideAdapter(factory(Polloi, 'my.other.key'))
Sometimes you're adapting an object wrapped in a LocationProxy.
>>> foo4 = Foo()
>>> import zope.location.location
>>> wrapped_foo4 = zope.location.location.LocationProxy(foo4, None, 'foo4')
>>> located_polloi = IPolloi(wrapped_foo4)
At first glance it looks as if located_polloi is located under wrapped_foo4.
>>> located_polloi.__parent__ is wrapped_foo4
True
>>> located_polloi.__name__
'my.other.key'
but that's because we received a LocationProxy
>>> print type(located_polloi).__name__
LocationProxy
If we unwrap located_polloi and look at it directly, we'll see it stores a
reference to the real Foo object
>>> from zope.proxy import removeAllProxies
>>> removeAllProxies(located_polloi).__parent__ is foo4
True
>>> removeAllProxies(located_polloi).__name__
'my.other.key'
|