[Checkins] SVN: z3c.resource/ Implemented local resource including a local ++resource++ namespace

Roger Ineichen roger at projekt01.ch
Tue Feb 13 22:44:41 EST 2007


Log message for revision 72566:
  Implemented local resource including a local ++resource++ namespace
  Added unit and doc tests

Changed:
  A   z3c.resource/branches/
  A   z3c.resource/tags/
  A   z3c.resource/trunk/
  A   z3c.resource/trunk/src/
  A   z3c.resource/trunk/src/z3c/
  A   z3c.resource/trunk/src/z3c/resource/
  A   z3c.resource/trunk/src/z3c/resource/README.txt
  A   z3c.resource/trunk/src/z3c/resource/SETUP.cfg
  A   z3c.resource/trunk/src/z3c/resource/__init__.py
  A   z3c.resource/trunk/src/z3c/resource/adapter.py
  A   z3c.resource/trunk/src/z3c/resource/browser/
  A   z3c.resource/trunk/src/z3c/resource/browser/__init__.py
  A   z3c.resource/trunk/src/z3c/resource/browser/configure.zcml
  A   z3c.resource/trunk/src/z3c/resource/browser/resource.gif
  A   z3c.resource/trunk/src/z3c/resource/browser/tests.py
  A   z3c.resource/trunk/src/z3c/resource/browser/views.py
  A   z3c.resource/trunk/src/z3c/resource/configure.zcml
  A   z3c.resource/trunk/src/z3c/resource/generations/
  A   z3c.resource/trunk/src/z3c/resource/generations/__init__.py
  A   z3c.resource/trunk/src/z3c/resource/generations/configure.zcml
  A   z3c.resource/trunk/src/z3c/resource/interfaces.py
  A   z3c.resource/trunk/src/z3c/resource/namespace.py
  A   z3c.resource/trunk/src/z3c/resource/proxy.py
  A   z3c.resource/trunk/src/z3c/resource/resource.py
  A   z3c.resource/trunk/src/z3c/resource/testing.py
  A   z3c.resource/trunk/src/z3c/resource/tests.py
  A   z3c.resource/trunk/src/z3c/resource/z3c.resource-configure.zcml

-=-
Added: z3c.resource/trunk/src/z3c/resource/README.txt
===================================================================
--- z3c.resource/trunk/src/z3c/resource/README.txt	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/README.txt	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,72 @@
+======
+README
+======
+
+A IResource is a container located in a object annotation providing a 
+container which contains IResourceItem. A resource item can be a file or
+a image implementation marked with a IResourceItem interface. Such resource
+items can get ``local`` traversed via the ``++resource++`` namespace. 
+Let's show how this works:
+
+  >>> from z3c.resource import interfaces
+  >>> from z3c.resource.resource import Resource
+  >>> res = Resource()
+  >>> interfaces.IResource.providedBy(res)
+  True
+
+For the next test, we define a contant object providing IResourceTraversable:
+
+  >>> import zope.interface
+  >>> from zope.annotation import IAttributeAnnotatable
+  >>> from zope.annotation.attribute import AttributeAnnotations
+  >>> zope.component.provideAdapter(AttributeAnnotations)
+  >>> class Content(object):
+  ...     zope.interface.implements(interfaces.IResourceTraversable,
+  ...                               IAttributeAnnotatable)
+
+  >>> content = Content()
+
+Such IResourceTraversable object can get adapted to IResource:
+
+  >>> import zope.component
+  >>> from z3c.resource.adapter import getResource
+  >>> zope.component.provideAdapter(getResource)
+  >>> res = interfaces.IResource(content)
+  >>> res
+  <z3c.resource.resource.Resource object at ...>
+
+  >>> len(res)
+  0
+
+We can add a IResourceItem to this resource:
+
+  >>> class Item(object):
+  ...     zope.interface.implements(interfaces.IResourceItem)
+  >>> item = Item()
+  >>> res['item'] = item
+  >>> len(res)
+  1
+
+There is also a namespace which makes the resource item traversable on 
+the content object:
+
+  >>> from z3c.resource.namespace import resource as resourceNamspace
+  >>> from zope.publisher.browser import TestRequest
+  >>> request = TestRequest()
+  >>> traverser = resourceNamspace(content, request)
+
+We can traverse to the IResource:
+
+  >>> traverser.traverse(None, None)
+  <z3c.resource.resource.Resource object at ...>
+
+  >>> traverser.traverse(None, None).__parent__ is content
+  True
+
+  >>> traverser.traverse(None, None).__name__ == u'++resource++'
+  True
+
+And we can traverse to the resource item by it's name:
+
+  >>> traverser.traverse('item', None)
+  <Item object at ...>


Property changes on: z3c.resource/trunk/src/z3c/resource/README.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/SETUP.cfg
===================================================================
--- z3c.resource/trunk/src/z3c/resource/SETUP.cfg	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/SETUP.cfg	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,3 @@
+<data-files zopeskel/etc/package-includes>
+  z3c.resource-*.zcml
+</data-files>


Property changes on: z3c.resource/trunk/src/z3c/resource/SETUP.cfg
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/__init__.py
===================================================================
--- z3c.resource/trunk/src/z3c/resource/__init__.py	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/__init__.py	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,16 @@
+##############################################################################
+#
+# Copyright (c) 2007 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$
+"""


Property changes on: z3c.resource/trunk/src/z3c/resource/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/adapter.py
===================================================================
--- z3c.resource/trunk/src/z3c/resource/adapter.py	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/adapter.py	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,46 @@
+##############################################################################
+#
+# Copyright (c) 2007 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 zope.component
+import zope.interface
+from zope.event import notify
+from zope.lifecycleevent import ObjectCreatedEvent
+from zope.annotation.interfaces import IAnnotations
+
+from z3c.resource import resource
+from z3c.resource import interfaces
+from z3c.resource.interfaces import RESOURCE_KEY
+
+
+ at zope.interface.implementer(interfaces.IResource)
+ at zope.component.adapter(interfaces.IResourceTraversable)
+def getResource(context):
+    """Adapt an IResourceTraversable object to IResource."""
+    annotations = IAnnotations(context)
+    try:
+        return annotations[RESOURCE_KEY]
+    except KeyError:
+        res = resource.Resource()
+        notify(ObjectCreatedEvent(res))
+        annotations[RESOURCE_KEY] = res
+        annotations[RESOURCE_KEY].__parent__ = context
+        annotations[RESOURCE_KEY].__name__ = '++resource++'
+        return annotations[RESOURCE_KEY]
+# Help out apidoc
+getResource.factory = resource.Resource


Property changes on: z3c.resource/trunk/src/z3c/resource/adapter.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/browser/__init__.py
===================================================================
--- z3c.resource/trunk/src/z3c/resource/browser/__init__.py	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/browser/__init__.py	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,16 @@
+##############################################################################
+#
+# Copyright (c) 2007 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$
+"""


Property changes on: z3c.resource/trunk/src/z3c/resource/browser/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/browser/configure.zcml
===================================================================
--- z3c.resource/trunk/src/z3c/resource/browser/configure.zcml	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/browser/configure.zcml	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,49 @@
+<configure
+    xmlns:zope="http://namespaces.zope.org/zope"
+    xmlns="http://namespaces.zope.org/browser"
+    i18n_domain="z3c">
+
+  <!-- Resource view called "/@@/" -->
+  <page
+      name=""
+      for="..interfaces.IResourceTraversable"
+      class=".views.Resources"
+      permission="zope.Public"
+      allowed_interface="zope.publisher.interfaces.browser.IBrowserPublisher"
+      />
+
+  <!-- IResource views -->
+  <addMenuItem
+      class="..resource.Resource"
+      title="Resource"
+      permission="zope.ManageContent"
+      />
+
+  <icon
+      name="zmi_icon"
+      for="..interfaces.IResource"
+      file="resource.gif"
+      />
+
+  <menuItem
+      menu="zmi_actions"
+      title="Resource"
+      for="..interfaces.IResourceTraversable"
+      action="++resource++/@@contents.html"
+      permission="zope.ManageContent"
+      />
+
+  <!-- IResourceLocation views -->
+  <containerViews
+      for="..interfaces.IResource"
+      index="zope.Public"
+      contents="zope.ManageContent"
+      add="zope.ManageContent"
+      />
+
+  <defaultView
+      name="contents.html"
+      for="..interfaces.IResource"
+      />
+
+</configure>


Property changes on: z3c.resource/trunk/src/z3c/resource/browser/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/browser/resource.gif
===================================================================
(Binary files differ)


Property changes on: z3c.resource/trunk/src/z3c/resource/browser/resource.gif
___________________________________________________________________
Name: svn:mime-type
   + image/gif

Added: z3c.resource/trunk/src/z3c/resource/browser/tests.py
===================================================================
--- z3c.resource/trunk/src/z3c/resource/browser/tests.py	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/browser/tests.py	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,83 @@
+##############################################################################
+#
+# Copyright (c) 2007 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$
+"""
+
+import unittest
+import zope.component
+from zope.app.testing import ztapi
+from zope.app.testing.placelesssetup import PlacelessSetup
+
+from zope.annotation.attribute import AttributeAnnotations
+from zope.publisher.interfaces import NotFound
+from zope.publisher.http import HTTPCharsets
+from zope.publisher.browser import TestRequest
+
+from z3c.resource import adapter
+from z3c.resource import testing
+from z3c.resource.browser.views import Resources
+
+
+class Test(PlacelessSetup, unittest.TestCase):
+
+    def setUp(self):
+        super(Test, self).setUp()
+        zope.component.provideAdapter(HTTPCharsets)
+        zope.component.provideAdapter(AttributeAnnotations)
+        zope.component.provideAdapter(adapter.getResource)
+
+    def test_publishTraverse(self):
+        
+        request = TestRequest()
+
+        class ResourceItem(testing.TestResourceItem):
+            def __init__(self, request): pass
+            def __call__(self): return 42
+
+        ztapi.browserResource('test', ResourceItem)
+        content = testing.Content()
+        view = Resources(content, request)
+        result = view.publishTraverse(request, 'test')
+        self.assertEqual(result(), 42)
+
+    def test_getitem(self):
+        request = TestRequest()
+
+        class ResourceItem(testing.TestResourceItem):
+            def __init__(self, request): pass
+            def __call__(self): return 42
+
+        ztapi.browserResource('test', ResourceItem)
+        content = testing.Content()
+        view = Resources(content, request)
+        result = view['test']
+        self.assertEqual(result(), 42)
+
+    def testNotFound(self):
+        request = TestRequest()
+        content = testing.Content()
+        view = Resources(content, request)
+        self.assertRaises(NotFound,
+                          view.publishTraverse,
+                          request, 'test'
+                          )
+
+
+def test_suite():
+    return unittest.makeSuite(Test)
+
+
+if __name__=='__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: z3c.resource/trunk/src/z3c/resource/browser/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/browser/views.py
===================================================================
--- z3c.resource/trunk/src/z3c/resource/browser/views.py	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/browser/views.py	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,84 @@
+##############################################################################
+#
+# Copyright (c) 2007 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$
+"""
+
+from zope.interface import implements
+from zope.location import locate
+from zope.publisher.interfaces import NotFound
+from zope.publisher.interfaces.browser import IBrowserPublisher
+from zope.traversing.interfaces import TraversalError
+from zope.publisher.browser import BrowserView
+
+from zope.app import zapi
+
+from z3c.resource import interfaces
+
+
+class Resources(BrowserView):
+    """Provide a URL-accessible resource view."""
+
+    implements(IBrowserPublisher)
+
+    def publishTraverse(self, request, name):
+        """See zope.publisher.interfaces.browser.IBrowserPublisher"""
+
+        if not interfaces.IResourceTraversable.providedBy(self.context):
+            raise TraversalError(self.context, 
+                'Parameter context: IResourceTraversable required.')
+
+        # get the resource instance
+        resource = interfaces.IResource(self.context)
+
+        # first check if we get a name, if not, we return the resource
+        # container itself. No fear local resource can't override global 
+        # resources because we lookup on different context for local resource 
+        # then for a global resource.
+        if name == '':
+            return resource
+
+        # first search for a resource in local resource location
+        try:
+            return resource[name]
+        except KeyError, e:
+            # no resource item found in local resource, doesn't matter.
+            pass
+
+        # query the default site resource and raise not found if there is no
+        # resource item found. If you don't like to lookup at global resources
+        # then you can override Resources view in your layer and skip this 
+        # part.
+        return self._getSiteResource(name)
+
+    def _getSiteResource(self, name):
+        """This will lookup resources on a ISite."""
+        resource = zapi.queryAdapter(self.request, name=name)
+        if resource is None:
+            raise NotFound(self, name)
+
+        sm = zapi.getSiteManager()
+        locate(resource, sm, name)
+        return resource
+
+    def browserDefault(self, request):
+        """See zope.publisher.interfaces.browser.IBrowserPublisher"""
+        return empty, ()
+
+    def __getitem__(self, name):
+        return self.publishTraverse(self.request, name)
+
+
+def empty():
+    return ''


Property changes on: z3c.resource/trunk/src/z3c/resource/browser/views.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/configure.zcml
===================================================================
--- z3c.resource/trunk/src/z3c/resource/configure.zcml	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/configure.zcml	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,49 @@
+<configure 
+  xmlns="http://namespaces.zope.org/zope"
+  xmlns:browser="http://namespaces.zope.org/browser"
+  i18n_domain="z3c">
+
+  <!-- generation -->
+  <include package=".generations" />
+
+  <!-- register interface for apidoc -->
+  <interface interface=".interfaces.IResource" />
+  <interface interface=".interfaces.IResourceTraversable" />
+
+  <!-- IResource -->
+  <class class=".resource.Resource">
+    <implements 
+        interface="zope.annotation.interfaces.IAttributeAnnotatable"
+        />
+    <require permission="zope.Public"
+        interface="zope.app.container.interfaces.IReadContainer"
+        />
+    <require permission="zope.ManageContent" 
+        interface="zope.app.container.interfaces.IWriteContainer"
+        />
+  </class>
+
+  <adapter
+      factory=".adapter.getResource"
+      trusted="True"
+      />
+
+  <!-- traverser ++resource++ -->
+  <view
+      name="resource" type="*"
+      for=".interfaces.IResourceTraversable"
+      provides="zope.traversing.interfaces.ITraversable"
+      factory=".namespace.resource"
+      />
+
+  <adapter
+      name="resource"
+      for=".interfaces.IResourceTraversable"
+      provides="zope.traversing.interfaces.ITraversable"
+      factory=".namespace.resource"
+      />
+
+
+  <include package=".browser" />
+
+</configure>


Property changes on: z3c.resource/trunk/src/z3c/resource/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/generations/__init__.py
===================================================================
--- z3c.resource/trunk/src/z3c/resource/generations/__init__.py	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/generations/__init__.py	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,24 @@
+##############################################################################
+#
+# Copyright (c) 2007 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.
+#
+##############################################################################
+__docformat__ = "reStructuredText"
+
+from zope.app.generations.generations import SchemaManager
+
+pkg = 'z3c.resource.generations'
+
+
+schemaManager = SchemaManager(
+    minimum_generation=0,
+    generation=0,
+    package_name=pkg)


Property changes on: z3c.resource/trunk/src/z3c/resource/generations/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/generations/configure.zcml
===================================================================
--- z3c.resource/trunk/src/z3c/resource/generations/configure.zcml	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/generations/configure.zcml	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,9 @@
+<configure xmlns="http://namespaces.zope.org/zope">
+
+  <utility
+      name="z3c.resource" 
+      provides="zope.app.generations.interfaces.ISchemaManager"
+      component=".schemaManager"
+      />
+
+</configure>


Property changes on: z3c.resource/trunk/src/z3c/resource/generations/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/interfaces.py
===================================================================
--- z3c.resource/trunk/src/z3c/resource/interfaces.py	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/interfaces.py	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,49 @@
+##############################################################################
+#
+# Copyright (c) 2007 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'
+
+from zope.interface import Interface
+from zope.location.interfaces import ILocation
+from zope.schema import Field
+from zope.annotation.interfaces import IAttributeAnnotatable
+
+from zope.app.container.constraints import containers
+from zope.app.container.constraints import contains
+from zope.app.container.constraints import ContainerTypesConstraint
+from zope.app.container.constraints import ItemTypePrecondition
+from zope.app.container.interfaces import IContainer
+
+RESOURCE_KEY = 'z3c.resource.IResource'
+
+
+class IResourceItem(Interface):
+    """Resource item inside a resource container."""
+
+    containers('z3c.resource.interfaces.IResource')
+
+
+class IResource(ILocation, IContainer):
+    """Resource container."""
+
+    contains(IResourceItem)
+
+
+class IResourceTraversable(IAttributeAnnotatable):
+    """Marker for component that can be adapted to resource.
+
+    The resource container can be traversed within the /@@/, too.
+    """


Property changes on: z3c.resource/trunk/src/z3c/resource/interfaces.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/namespace.py
===================================================================
--- z3c.resource/trunk/src/z3c/resource/namespace.py	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/namespace.py	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,113 @@
+##############################################################################
+#
+# Copyright (c) 2007 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'
+
+from zope.app.component import hooks
+from zope.traversing.interfaces import TraversalError
+from zope.traversing.namespace import view
+from zope.traversing.namespace import getResource
+
+from z3c.resource import interfaces
+
+
+
+class resource(view):
+    """Traversal handler for ++resource++
+    
+    Placeless setup:
+
+    >>> import zope.component
+    >>> from zope.app.testing import placelesssetup, ztapi
+    >>> placelesssetup.setUp()
+    >>> from zope.annotation.attribute import AttributeAnnotations
+    >>> from z3c.resource.adapter import getResource
+    >>> from z3c.resource import testing
+    >>> from z3c.resource.resource import Resource
+    >>> zope.component.provideAdapter(AttributeAnnotations)
+
+    Setup IResourceLocation and IResource adapters:
+
+    >>> zope.component.provideAdapter(getResource)
+
+    Now we create our resource traversable container:
+
+    >>> from z3c.resource.testing import Content
+    >>> from zope.publisher.browser import TestRequest
+    >>> content = Content()
+    >>> request = TestRequest
+
+    Traverse the resource traversable container without a name:
+
+    >>> traverser = resource(content, request)
+    >>> isinstance(traverser.traverse(None, ''), Resource)
+    True
+    >>> traverser.traverse(None, '').__parent__ is content
+    True
+    >>> traverser.traverse(None, '').__name__ == u'++resource++'
+    True
+
+    Traverse an existing name:
+
+    >>> foo = testing.TestResourceItem()
+    >>> res = interfaces.IResource(content)
+    >>> res[u'foo'] = foo
+    >>> traverser.traverse(u'foo', '') == foo
+    True
+
+    Traverse a non IResourceTraversable access:
+
+    >>> fake = object()
+    >>> traverser = resource(fake, request)
+    >>> try:
+    ...     traverser.traverse('', None)
+    ... except TraversalError, e:
+    ...     e[0] is fake, e[1]
+    (True, 'Parameter context: IResourceTraversable required.')
+
+    Tear down::
+
+    >>> placelesssetup.tearDown()
+
+    """
+
+    def __init__(self, context, request=None):
+        self.context = context
+        self.request = request
+
+    def traverse(self, name, ignored):
+        # preconditions
+        traversable = self.context
+
+        if not interfaces.IResourceTraversable.providedBy(traversable):
+            raise TraversalError(traversable, 
+                'Parameter context: IResourceTraversable required.')
+
+        # essentials
+        resource = interfaces.IResource(traversable)
+
+        if name:
+            try:
+                return resource[name]
+            except KeyError, e:
+                pass
+
+            site = hooks.getSite()
+            return getResource(site, name, self.request)
+
+        else:
+            return resource


Property changes on: z3c.resource/trunk/src/z3c/resource/namespace.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/proxy.py
===================================================================
--- z3c.resource/trunk/src/z3c/resource/proxy.py	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/proxy.py	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,127 @@
+##############################################################################
+#
+# Copyright (c) 2007 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'
+
+from zope.interface import implements
+
+from z3c.resource import interfaces
+from z3c.proxy.container import ContainerLocationProxy
+
+
+
+class ResourceLocationProxy(ContainerLocationProxy):
+    """Proxy a resource container.
+
+    The specific resource container implementation of a traversable component
+    can be proxied.
+
+    Now we create our resource test implementation. We take a container that
+    implements ResourceTraversable:
+    
+        >>> from z3c.resource.testing import ResourceTraversableContainer
+        >>> resourcetraversable = ResourceTraversableContainer()
+
+    Inside this resource traversabel we put our resource container:
+
+        >>> from z3c.resource.testing import TestResourceContainer
+        >>> resourcetraversable['pc'] = pc = TestResourceContainer()
+
+    Now we proxy our prototyped resource container implementation. The
+    resourcetraversable will be the parent of the proxied resource container and
+    that should be named to '@@':
+
+        >>> pcproxy = ResourceLocationProxy(pc, resourcetraversable)
+        >>> pcproxy.__name__ == u'@@'
+        True
+        >>> pcproxy.__parent__ is resourcetraversable
+        True
+
+    Containment put into the resource container appears in the
+    resource location proxy:
+
+        >>> from z3c.resource.testing import TestResourceItem
+        >>> pc['x'] = x = TestResourceItem()
+
+        >>> x1 = pcproxy['x']
+        >>> from zope.proxy import isProxy
+        >>> isProxy(x1)
+        True
+        >>> x1 == x
+        True
+
+    Containment put into the resource location proxy appears in 
+    the resource container:
+
+        >>> pcproxy['y'] = y = TestResourceItem()
+        >>> pc['y'] is y
+        True
+
+        >>> y1 = pcproxy['y']
+        >>> isProxy(y1)
+        True
+        >>> y1 == y
+        True
+
+    Finaly we check all other methods of the proxy:
+
+        >>> 'x' in pcproxy
+        True
+        >>> pcproxy.has_key('x')
+        1
+        >>> keys = [key for key in pcproxy.keys()]; keys.sort(); keys
+        [u'x', u'y']
+        >>> items = [item for item in pcproxy.items()]; items.sort()
+        >>> items == [(u'x', x), (u'y', y)]
+        True
+        >>> pcproxy.get('x') == x
+        True
+        >>> iterator = iter(pcproxy)
+        >>> iterator.next() in pcproxy
+        True
+        >>> iterator.next() in pcproxy
+        True
+        >>> iterator.next()     
+        Traceback (most recent call last):
+        ...
+        StopIteration
+        >>> values = pcproxy.values(); values.sort();
+        >>> x in values, y in values
+        (True, True)
+        >>> len(pcproxy)
+        2
+        >>> del pcproxy['x']
+        >>> 'x' in pcproxy
+        False
+
+    """
+
+    implements(interfaces.IResourceLocationProxy)
+
+    def __init__(self, context, parent):
+        # preconditions
+        if not interfaces.IResourceContainer.providedBy(context):
+            raise ValueError(
+                'Parameter context: IResourceContainer required.')
+
+        if not interfaces.IResourceTraversable.providedBy(parent):
+            raise ValueError(
+                'Parameter parent: IResourceTraversable required.')
+
+        # essentails
+        super(ResourceLocationProxy, self).__init__(context)
+        self.__name__ = u'@@'
+        self.__parent__ = parent


Property changes on: z3c.resource/trunk/src/z3c/resource/proxy.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/resource.py
===================================================================
--- z3c.resource/trunk/src/z3c/resource/resource.py	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/resource.py	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,40 @@
+##############################################################################
+#
+# Copyright (c) 2007 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 zope.interface
+from zope.app.container import btree
+from z3c.resource import interfaces
+
+
+class Resource(btree.BTreeContainer, object):
+    """Resource implementation.
+
+    >>> resource = Resource()
+
+    Such a resource provides:
+
+    >>> interfaces.IResource.providedBy(resource)
+    True
+
+    >>> from zope.app.container.interfaces import IContainer
+    >>> IContainer.providedBy(resource)
+    True
+
+    """
+
+    zope.interface.implements(interfaces.IResource)


Property changes on: z3c.resource/trunk/src/z3c/resource/resource.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/testing.py
===================================================================
--- z3c.resource/trunk/src/z3c/resource/testing.py	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/testing.py	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,72 @@
+##############################################################################
+#
+# Copyright (c) 2007 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 zope.interface
+
+from zope.app.container.tests.test_icontainer import BaseTestIContainer
+from zope.app.container.tests.test_icontainer import DefaultTestData
+from zope.app.container import contained
+
+from z3c.resource import interfaces
+from z3c import testing
+
+
+################################################################################
+#
+# Resource Test implementations
+#
+################################################################################
+
+class Content(object):
+    """Test content."""
+
+    zope.interface.implements(interfaces.IResourceTraversable)
+
+
+
+class TestResourceItem(contained.Contained, object):
+    """Test resource item."""
+
+    zope.interface.implements(interfaces.IResourceItem)
+
+
+################################################################################
+#
+# Resource Base Tests
+#
+################################################################################
+
+class BaseTestIResource(testing.BaseTestIContainer):
+
+    def getTestInterface(self):
+        return interfaces.IResource
+
+    def makeTestData(self):
+        return DefaultTestData()
+
+    def getUnknownKey(self):
+        return '10'
+
+    def getBadKeyTypes(self):
+        return [None, ['foo'], 1, '\xf3abc']
+
+
+class BaseTestIResourceItem(testing.InterfaceBaseTest):
+
+    def getTestInterface(self):
+        return interfaces.IResourceItem


Property changes on: z3c.resource/trunk/src/z3c/resource/testing.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/tests.py
===================================================================
--- z3c.resource/trunk/src/z3c/resource/tests.py	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/tests.py	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,45 @@
+##############################################################################
+#
+# Copyright (c) 2007 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 doctest
+import unittest
+from zope.testing.doctestunit import DocFileSuite
+from zope.app.testing.placelesssetup import setUp
+from zope.app.testing.placelesssetup import tearDown
+
+from z3c.resource.resource import Resource
+from z3c.resource.testing import BaseTestIResource
+
+
+class TestResource(BaseTestIResource):
+
+    def getTestClass(self):
+        return Resource
+
+
+def test_suite():
+    return unittest.TestSuite((
+        unittest.makeSuite(TestResource),
+        doctest.DocTestSuite('z3c.resource.resource'),
+        doctest.DocTestSuite('z3c.resource.namespace'),
+        DocFileSuite('README.txt', setUp=setUp, tearDown=tearDown,
+            optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS),
+        ))
+
+if __name__ == '__main__': unittest.main()


Property changes on: z3c.resource/trunk/src/z3c/resource/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.resource/trunk/src/z3c/resource/z3c.resource-configure.zcml
===================================================================
--- z3c.resource/trunk/src/z3c/resource/z3c.resource-configure.zcml	2007-02-14 03:36:06 UTC (rev 72565)
+++ z3c.resource/trunk/src/z3c/resource/z3c.resource-configure.zcml	2007-02-14 03:44:40 UTC (rev 72566)
@@ -0,0 +1,5 @@
+<configure xmlns="http://namespaces.zope.org/zope">
+
+  <include package="z3c.resource" />
+
+</configure>


Property changes on: z3c.resource/trunk/src/z3c/resource/z3c.resource-configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Checkins mailing list