/usr/share/pyshared/zope/dublincore/tests/partial.txt is in python-zope.dublincore 3.8.2-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 | ====================================
Dublin Core metadata as content data
====================================
Sometimes we want to include data in content objects which mirrors one
or more Dublin Core fields. In these cases, we want the Dublin Core
structures to use the data in the content object rather than keeping a
separate value in the annotations typically used. What fields we want
to do this with can vary, however, and we may not want the Dublin Core
APIs to constrain our choices of field names for our content objects.
To deal with this, we can use speciallized adapter implementations
tailored to specific content objects. To make this a bit easier,
there is a factory for such adapters.
Let's take a look at the simplest case of this to start with. We have
some content object with a `title` attribute that should mirror the
Dublin Core `title` field::
>>> import zope.interface
>>> import zope.annotation.interfaces
>>> class Content(object):
...
... zope.interface.implements(
... zope.annotation.interfaces.IAttributeAnnotatable)
...
... title = u""
... description = u""
To avoid having a discrepency between the `title` attribute of our
content object and the equivalent Dublin Core field, we can provide a
specific adapter for our object::
>>> from zope.dublincore import annotatableadapter
>>> factory = annotatableadapter.partialAnnotatableAdapterFactory(
... ["title"])
This creates an adapter factory that maps the Dublin Core `title`
field to the `title` attribute on instances of our `Content` class.
Multiple mappings may be specified by naming the additional fields in
the sequence passed to `partialAnnotatableAdapterFactory()`. (We'll
see later how to use different attribute names for Dublin Core
fields.)
Let's see what happens when we use the adapter.
When using the adapter to retrieve a field set to use the content
object, the value stored on the content object is used::
>>> content = Content()
>>> adapter = factory(content)
>>> adapter.title
u''
>>> content.title = u"New Title"
>>> adapter.title
u'New Title'
If we set the relevant Dublin Core field using the adapter, the
content object is updated::
>>> adapter.title = u"Adapted Title"
>>> content.title
u'Adapted Title'
Dublin Core fields which are not specifically mapped to the content
object do not affect the content object::
>>> adapter.description = u"Some long description."
>>> content.description
u''
>>> adapter.description
u'Some long description.'
Using arbitrary field names
===========================
We've seen the simple approach, allowing a Dublin Core field to be
stored on the content object using an attribute of the same name as
the DC field. However, we may want to use a different name for some
reason. The `partialAnnotatableAdapterFactory()` supports this as
well.
If we call `partialAnnotatableAdapterFactory()` with a mapping instead
of a sequence, the mapping is used to map Dublin Core field names to
attribute names on the content object.
Let's look at an example where we want the `abstract` attribute on the
content object to be used for the `description` Dublin Core field::
>>> class Content(object):
...
... zope.interface.implements(
... zope.annotation.interfaces.IAttributeAnnotatable)
...
... abstract = u""
We can create the adapter factory by passing a mapping to
`partialAnnotatableAdapterFactory()`::
>>> factory = annotatableadapter.partialAnnotatableAdapterFactory(
... {"description": "abstract"})
We can check the effects of the adapter as before::
>>> content = Content()
>>> adapter = factory(content)
>>> adapter.description
u''
>>> content.abstract = u"What it's about."
>>> adapter.description
u"What it's about."
>>> adapter.description = u"Change of plans."
>>> content.abstract
u'Change of plans.'
Limitations
===========
The current implementation has a number of limitations to be aware of;
hopefully these can be removed in the future.
- Only simple string properties, like `title`, are supported. This is
largely because other field types have not been given sufficient
thought. Attempting to use this for other fields will cause a
`ValueError` to be raised by `partialAnnotatableAdapterFactory()`.
- The CMF-like APIs are not supported in the generated adapters. It
is not clear that these APIs are used, but content object
implementations should be aware of this limitation.
|