[Zope-CVS] CVS: Packages/Moztop/moztopsupport/rdf - adapter.py:1.3 container.py:1.4 interfaces.py:1.3

Stephan Richter srichter@cbu.edu
Fri, 21 Mar 2003 11:56:23 -0500


Update of /cvs-repository/Packages/Moztop/moztopsupport/rdf
In directory cvs.zope.org:/tmp/cvs-serv22939/moztopsupport/rdf

Modified Files:
	adapter.py container.py interfaces.py 
Log Message:
More cleanup on generating the contents in RDF format. Now the adapters
handle all of the RDF generation as it should be. Had to change the 
interfaces accordingly.


=== Packages/Moztop/moztopsupport/rdf/adapter.py 1.2 => 1.3 ===
--- Packages/Moztop/moztopsupport/rdf/adapter.py:1.2	Fri Mar 21 08:53:51 2003
+++ Packages/Moztop/moztopsupport/rdf/adapter.py	Fri Mar 21 11:56:23 2003
@@ -1,6 +1,6 @@
 ##############################################################################
 #
-# Copyright (c) 2002, 2003 Zope Corporation and Contributors.
+# Copyright (c) 2003 Zope Corporation and Contributors.
 # All Rights Reserved.
 #
 # This software is subject to the provisions of the Zope Public License,
@@ -11,7 +11,7 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-__doc__ = """ Adapters for RDF Support of Moztop Extension Product
+"""Adapters for RDF Support of Moztop Extension Product
 
 $Id$
 """
@@ -20,8 +20,9 @@
 from moztopsupport.interfaces import IResourceTypesUtility
 from zope.app.interfaces.dublincore import IZopeDublinCore
 from zope.component import getAdapter, getView, getUtility
+from zope.proxy.context import ContextWrapper
 
-class RDFNode:
+class RDFNode(object):
     '''RDF representation of a node.'''
 
     __implements__ = IRDFNode
@@ -30,6 +31,7 @@
         self.context = object
 
     def getInfo(self, id, request):
+        """See moztopsupport.rdf.interfaces.IRDFNode"""
         dc = getAdapter(self.context, IZopeDublinCore)
         url = str(getView(self.context, 'absolute_url', request))
         utility = getUtility(self.context, IResourceTypesUtility)
@@ -37,26 +39,30 @@
 
         return {'title' : dc.title or id,
                 'rdf_url' : url,
-                'type' : resource_type}
+                'type' : resource_type,
+                'misc' : ''}
 
     def getListEntry(self, id, request):
+        """See moztopsupport.rdf.interfaces.IRDFNode"""
         return self._listEntry %self.getInfo(id, request)
 
     def getDescription(self, id, request):
+        """See moztopsupport.rdf.interfaces.IRDFNode"""
         return self._description %self.getInfo(id, request)
 
-    _listEntry = '''      <rdf:li resource="%(rdf_url)s"/>\n'''
+    _listEntry = '''      <rdf:li resource="%(rdf_url)s"/>'''
 
     _description = '''\
 <rdf:Description rdf:about="%(rdf_url)s">
   <dc:title>%(title)s</dc:title>
-  <oscom:resourcetype rdf:resource="urn:moztop:resourcetypes:%(type)s"/>
+  <oscom:resourcetype rdf:resource="urn:moztop:resourcetypes:%(type)s"/>\
+%(misc)s
 </rdf:Description>
 
 '''
 
 
-class RDFContainer:
+class RDFContainer(RDFNode):
     '''RDF representation of a Container.'''
 
     __implements__ = IRDFContainer
@@ -64,12 +70,26 @@
     def __init__(self, object):
         self.context = object
 
-    def getInfo(self, request):
-        context = self.context
-        url = str(getView(context, 'absolute_url', request))
-        items = context.items()
-        subs = []
-        for item in items:
-            subs.append('%s/%s' % (url, item[0]))
-        return subs
-
+    def getInfo(self, id, request):
+        """See moztopsupport.rdf.interfaces.IRDFNode"""
+        info = super(RDFContainer, self).getInfo(id, request)
+        if self.context.keys():
+            info['misc'] = '\n'+self.getSubNodes(request)
+        return info
+
+    def getSubNodes(self, request):
+        """See moztopsupport.rdf.interfaces.IRDFContainer"""
+        subs_rdf = []
+        for id, obj in self.context.items():
+            wrapped = ContextWrapper(obj, self.context, name=id)
+            node = getAdapter(wrapped, IRDFNode)
+            subs_rdf.append(node.getListEntry(id, request))
+
+        return self._subNodes %{'subs_rdf' : '\n'.join(subs_rdf)}
+
+    _subNodes = '''\
+  <nc:subitems>
+    <rdf:Seq>
+%(subs_rdf)s
+    </rdf:Seq>
+  </nc:subitems>'''


=== Packages/Moztop/moztopsupport/rdf/container.py 1.3 => 1.4 ===
--- Packages/Moztop/moztopsupport/rdf/container.py:1.3	Fri Mar 21 09:29:24 2003
+++ Packages/Moztop/moztopsupport/rdf/container.py	Fri Mar 21 11:56:23 2003
@@ -28,8 +28,10 @@
 from zope.proxy.context import ContextWrapper
 from zope.publisher.browser import BrowserView
 from zope.component.exceptions import ComponentLookupError
+
 from moztopsupport.rdf.interfaces import IRDFNode, IRDFContainer
-from moztopsupport.interfaces import IResourceTypesUtility, IServiceManagerUtility
+from moztopsupport.interfaces import \
+     IResourceTypesUtility, IServiceManagerUtility
 
 
 class Contents(BrowserView):
@@ -113,41 +115,38 @@
     def _makeSubtree(self, id, base):
         """Create the contents tree in RDF format. This is of course a
         recursive method."""
-        
         rdf = ''
-        fillIn = {}
 
-        node = queryAdapter(base, IRDFNode)
+        # Add a node's (object's) description to the RDF output
+        node = queryAdapter(base, IRDFContainer)
         if node is not None:
             rdf += node.getDescription(id, self.request)
+        else:
+            node = queryAdapter(base, IRDFNode)
+            rdf += node.getDescription(id, self.request)
+
+        # Check whether there are any sub-objects, they should be looked into
+        # as well.
+        if IReadContainer.isImplementedBy(base):
+            for id, obj in base.items():
+                wrapped = ContextWrapper(obj, base, name=id)
+                rdf += self._makeSubtree(id, wrapped)
 
-        rdf_container = queryAdapter(base, IRDFContainer)
-        if fillIn and rdf_container is not None:
-            subs = rdf_container.getInfo(self.request)
-            if subs:
-                subs_rdf = ''
-                for sub in subs:
-                    subs_rdf += _sub_node %{'rdf_url' : sub}
-                rdf += _sub_nodes % ({'rdf_url':fillIn['rdf_url'],
-                                      'subs_rdf':subs_rdf})
-
-        if fillIn and IReadContainer.isImplementedBy(base):
-            items = base.items()
-            for id, obj in items:
-                w_obj = ContextWrapper(obj, base, name=id)
-                rdf += self._makeSubtree(id, w_obj)
         return rdf
 
 
     def contents(self):
         smut = getUtility(self.context, IServiceManagerUtility)
         if not smut.hasLocalServiceManager(self.context):
-            raise ComponentLookupError('Local ServiceManager could not be found')
+            raise ComponentLookupError(
+                'Local ServiceManager could not be found')
         self.request.response.setHeader('content-type', 'text/xml')
         return self._template()
     
     _template = ViewPageTemplateFile("contents.pt")
 
+
+
 class ResourceTypes(BrowserView):
     """A view that manages the RDF listing of all available resource types."""
     
@@ -166,7 +165,8 @@
     def resource_types(self):
         smut = getUtility(self.context, IServiceManagerUtility)
         if not smut.hasLocalServiceManager(self.context):
-            raise ComponentLookupError('Local ServiceManager could not be found')
+            raise ComponentLookupError(
+                'Local ServiceManager could not be found')
         self.request.response.setHeader('content-type', 'text/xml')
         return self._template()
 
@@ -174,19 +174,6 @@
 
 
 # Some useful raw RDF snippets.
-
-_sub_node = '''      <rdf:li resource="%(rdf_url)s"/>\n'''
-
-_sub_nodes = '''\
-<rdf:Description rdf:about="%(rdf_url)s">
-  <nc:subitems>
-    <rdf:Seq>
-%(subs_rdf)s
-    </rdf:Seq>
-  </nc:subitems>
-</rdf:Description>
-
-'''
 
 _resource_node = '''\
 <rdf:Description about="urn:moztop:resourcetypes:%(resource_type)s">


=== Packages/Moztop/moztopsupport/rdf/interfaces.py 1.2 => 1.3 ===
--- Packages/Moztop/moztopsupport/rdf/interfaces.py:1.2	Fri Mar 21 08:53:51 2003
+++ Packages/Moztop/moztopsupport/rdf/interfaces.py	Fri Mar 21 11:56:23 2003
@@ -1,6 +1,6 @@
 ##############################################################################
 #
-# Copyright (c) 2002, 2003 Zope Corporation and Contributors.
+# Copyright (c) 2003 Zope Corporation and Contributors.
 # All Rights Reserved.
 #
 # This software is subject to the provisions of the Zope Public License,
@@ -11,15 +11,16 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-__doc__ = """ Interfaces definition for RDF Support of Moztop Extension Product
+"""Interfaces definition for RDF Support of Moztop Extension Product
 
 $Id$
 """
-
 from zope.interface import Interface
 
 class IRDFNode(Interface):
-    '''RDF representation of a node.'''
+    '''RDF representation of a particular object. This interface is
+    responsible for returning the object in an appropriate RDF Node
+    representation.'''
 
     def getInfo():
         '''Returns a dict containing the needed data to build
@@ -32,10 +33,8 @@
         """Return a rdf:Description for this node."""
 
 
-class IRDFContainer(Interface):
-    '''RDF representation of a container.'''
-
-    def getInfo():
-        '''Returns a list of the subitems for building
-        a RDF container.'''
+class IRDFContainer(IRDFNode):
+    '''An expansion on the RDF Node, so that subitems can be handled.'''
 
+    def getSubNodes():
+        """Builds the subitems of the node."""