[Zope3-checkins] SVN: Zope3/trunk/src/zope/dublincore/ Dublin Core
property to use Dublin Core meta data as simple properties.
Jürgen Kartnaller
juergen at kartnaller.at
Mon Aug 14 06:52:57 EDT 2006
Log message for revision 69463:
Dublin Core property to use Dublin Core meta data as simple properties.
Changed:
A Zope3/trunk/src/zope/dublincore/property.py
A Zope3/trunk/src/zope/dublincore/property.txt
A Zope3/trunk/src/zope/dublincore/tests/test_property.py
-=-
Added: Zope3/trunk/src/zope/dublincore/property.py
===================================================================
--- Zope3/trunk/src/zope/dublincore/property.py 2006-08-14 05:34:05 UTC (rev 69462)
+++ Zope3/trunk/src/zope/dublincore/property.py 2006-08-14 10:52:56 UTC (rev 69463)
@@ -0,0 +1,100 @@
+##############################################################################
+#
+# Copyright (c) 2005 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id: $
+"""
+__docformat__ = 'restructuredtext'
+
+import re,os
+
+from zope import schema
+from zope import interface
+
+from zope.dublincore.interfaces import IZopeDublinCore
+from zope.dublincore.zopedublincore import SequenceProperty
+
+_marker = object()
+
+
+class DCProperty(object):
+ """Adapt to a dublin core property
+
+ Handles DC list properties as scalar property.
+ """
+
+ def __init__(self, name):
+ self.__name = name
+
+ def __get__(self, inst, klass):
+ if inst is None:
+ return self
+ name = self.__name
+ inst = IZopeDublinCore(inst)
+ value = getattr(inst, name, _marker)
+ if value is _marker:
+ field = IZopeDublinCore[name].bind(inst)
+ value = getattr(field, 'default', _marker)
+ if value is _marker:
+ raise AttributeError(name)
+ if isinstance(value, (list, tuple)):
+ value = value[0]
+ return value
+
+ def __set__(self, inst, value):
+ name = self.__name
+ inst = IZopeDublinCore(inst)
+ field = IZopeDublinCore[name].bind(inst)
+ if ( not isinstance(value, list)
+ and isinstance(field, schema.List)):
+ value = [value]
+ field.validate(value)
+ if field.readonly and inst.__dict__.has_key(name):
+ raise ValueError(name, 'field is readonly')
+ setattr(inst, name, value)
+
+ def __getattr__(self, name):
+ return getattr(IZopeDublinCore[self.__name], name)
+
+
+class DCListProperty(DCProperty):
+ """Adapt to a dublin core list property
+
+ Returns the DC property unchanged.
+ """
+
+ def __init__(self, name):
+ self.__name = name
+
+ def __get__(self, inst, klass):
+ if inst is None:
+ return self
+ name = self.__name
+ inst = IZopeDublinCore(inst)
+ value = getattr(inst, name, _marker)
+ if value is _marker:
+ field = IZopeDublinCore[name].bind(inst)
+ value = getattr(field, 'default', _marker)
+ if value is _marker:
+ raise AttributeError(name)
+ return value
+
+ def __set__(self, inst, value):
+ name = self.__name
+ inst = IZopeDublinCore(inst)
+ field = IZopeDublinCore[name].bind(inst)
+ field.validate(value)
+ if field.readonly and inst.__dict__.has_key(name):
+ raise ValueError(name, 'field is readonly')
+ setattr(inst, name, value)
+
Property changes on: Zope3/trunk/src/zope/dublincore/property.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/dublincore/property.txt
===================================================================
--- Zope3/trunk/src/zope/dublincore/property.txt 2006-08-14 05:34:05 UTC (rev 69462)
+++ Zope3/trunk/src/zope/dublincore/property.txt 2006-08-14 10:52:56 UTC (rev 69463)
@@ -0,0 +1,45 @@
+======================
+Dublin Core Properties
+======================
+
+A dublin core property allows us to use properties from dublin core
+by simply defining a property as DCProperty.
+
+ >>> from zope.dublincore import property
+
+ >>> from zope import interface
+ >>> from zope.annotation.interfaces import IAttributeAnnotatable
+ >>> class DC(object):
+ ... interface.implements(IAttributeAnnotatable)
+ ... title = property.DCProperty('title')
+ ... author = property.DCProperty('creators')
+ ... authors = property.DCListProperty('creators')
+
+ >>> obj = DC()
+ >>> obj.title = u'My title'
+ >>> obj.title
+ u'My title'
+
+Let's see if the title is really stored in dublin core :
+
+ >>> from zope.dublincore.interfaces import IZopeDublinCore
+ >>> IZopeDublinCore(obj).title
+ u'My title'
+
+Even if a dublin core property is a list property we can set and get the
+property as scalar type :
+
+ >>> obj.author = u'me'
+ >>> obj.author
+ u'me'
+
+DCListProperty acts on the list :
+
+ >>> obj.authors
+ (u'me',)
+ >>> obj.authors = [u'I', u'others']
+ >>> obj.authors
+ (u'I', u'others')
+ >>> obj.author
+ u'I'
+
Property changes on: Zope3/trunk/src/zope/dublincore/property.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/dublincore/tests/test_property.py
===================================================================
--- Zope3/trunk/src/zope/dublincore/tests/test_property.py 2006-08-14 05:34:05 UTC (rev 69462)
+++ Zope3/trunk/src/zope/dublincore/tests/test_property.py 2006-08-14 10:52:56 UTC (rev 69463)
@@ -0,0 +1,55 @@
+##############################################################################
+#
+# Copyright (c) 2006 Lovely Systems and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Test the Dublin Core Property implementation
+
+$Id: $
+"""
+__docformat__ = "reStructuredText"
+
+import doctest
+import unittest
+
+from zope import component
+
+from zope.testing.doctestunit import DocFileSuite
+
+from zope.app.testing import setup, placelesssetup
+from zope.dublincore import annotatableadapter
+from zope.dublincore.interfaces import IWriteZopeDublinCore
+
+
+def setUp(test):
+ setup.placefulSetUp()
+ setup.setUpAnnotations()
+ component.provideAdapter(annotatableadapter.ZDCAnnotatableAdapter,
+ provides=IWriteZopeDublinCore,
+ )
+
+def tearDown(test):
+ setup.placefulTearDown()
+
+
+def test_suite():
+
+ return unittest.TestSuite(
+ (
+ DocFileSuite('../property.txt',
+ setUp=setUp,
+ tearDown=tearDown,
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+ ),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Property changes on: Zope3/trunk/src/zope/dublincore/tests/test_property.py
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the Zope3-Checkins
mailing list