[Checkins] SVN: Sandbox/adamg/ocql/branches/dbprovider/src/ocql/ added index to collect interfaces of a kind

Adam Groszer agroszer at gmail.com
Thu Jun 19 11:37:21 EDT 2008


Log message for revision 87553:
  added index to collect interfaces of a kind

Changed:
  A   Sandbox/adamg/ocql/branches/dbprovider/src/ocql/database/index.py
  A   Sandbox/adamg/ocql/branches/dbprovider/src/ocql/database/tests.py
  U   Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/mentor.py
  U   Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/organization.py
  U   Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/project.py
  U   Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/student.py
  U   Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/utils.py
  U   Sandbox/adamg/ocql/branches/dbprovider/src/ocql/tests/test_old.py

-=-
Added: Sandbox/adamg/ocql/branches/dbprovider/src/ocql/database/index.py
===================================================================
--- Sandbox/adamg/ocql/branches/dbprovider/src/ocql/database/index.py	                        (rev 0)
+++ Sandbox/adamg/ocql/branches/dbprovider/src/ocql/database/index.py	2008-06-19 15:37:19 UTC (rev 87553)
@@ -0,0 +1,137 @@
+# -*- coding: UTF-8 -*-
+
+"""Special index to collect all objects providing an interface
+
+$Id$
+"""
+
+#TODO: not optimal, uses FieldIndex with a value of always 1
+
+import zope.index.field
+import zope.interface
+import zope.schema
+
+from zope.i18nmessageid import ZopeMessageFactory as _
+
+import zope.app.container.contained
+import zope.app.catalog.attribute
+import zope.app.catalog.interfaces
+
+class IAllIndex(zope.interface.Interface):
+    """I index objects by first adapting them to an interface, then
+       retrieving a field on the adapted object.
+    """
+
+    interface = zope.schema.Choice(
+        title=_(u"Interface"),
+        description=_(u"Objects will be adapted to this interface"),
+        vocabulary="Interfaces",
+        required=False,
+        )
+
+
+class IAllMixinIndex(IAllIndex,
+                  zope.app.catalog.interfaces.ICatalogIndex):
+    """Interface-based catalog index
+    """
+
+class AllMixinIndex(object):
+    """Index interface-defined attributes
+
+       Mixin for indexing all objects providing a particular interface.
+
+       The class is meant to be mixed with a base class that defines an
+       index_doc method:
+
+         >>> class BaseIndex(object):
+         ...     def __init__(self):
+         ...         self.data = []
+         ...     def index_doc(self, id, value):
+         ...         self.data.append((id, value))
+
+         >>> class Index(AllMixinIndex, BaseIndex):
+         ...     pass
+
+         Let's setup two interfaces with classes to test:
+
+         >>> from zope.interface import Interface
+         >>> from zope.interface import implements
+
+         >>> class I1(Interface):
+         ...     pass
+
+         >>> class I2(Interface):
+         ...     pass
+
+         >>> class Data1:
+         ...     implements(I1)
+         ...     def __init__(self, v):
+         ...         self.x = v
+
+         >>> class Data2:
+         ...     implements(I2)
+         ...     def __init__(self, v):
+         ...         self.x = v
+
+         >>> class Data3:
+         ...     implements(I1, I2)
+         ...     def __init__(self, v):
+         ...         self.x = v
+
+         Our index will index all objects providing I1:
+
+         >>> index1 = Index(interface=I1)
+
+         Those two provide it, they get indexed:
+
+         >>> index1.index_doc(11, Data1(1))
+         >>> index1.index_doc(22, Data1(2))
+         >>> index1.data
+         [(11, 1), (22, 1)]
+
+         Those two provide I2, they DONT get indexed:
+
+         >>> index1.index_doc(110, Data2(21))
+         >>> index1.index_doc(220, Data2(22))
+         >>> index1.data
+         [(11, 1), (22, 1)]
+
+         Those two provide also it, they get indexed:
+
+         >>> index1.index_doc(1100, Data3(21))
+         >>> index1.index_doc(2200, Data3(22))
+         >>> index1.data
+         [(11, 1), (22, 1), (1100, 1), (2200, 1)]
+
+
+       """
+
+    zope.interface.implements(IAllMixinIndex)
+
+    default_interface = None
+
+    def __init__(self, interface=None,
+                 *args, **kwargs):
+        #we don't care about field_name and field_callable
+        super(AllMixinIndex, self).__init__(*args, **kwargs)
+        if interface is None:
+            self.interface = self.default_interface
+        else:
+            self.interface = interface
+
+        if self.interface is None:
+            raise ValueError("Must pass an interface")
+
+    def index_doc(self, docid, object):
+        if not self.interface.providedBy(object):
+            return None
+
+        return super(AllMixinIndex, self).index_doc(docid, 1)
+
+
+
+class AllIndex(AllMixinIndex,
+                 zope.index.field.FieldIndex,
+                 zope.app.container.contained.Contained):
+
+    zope.interface.implements(IAllIndex)


Property changes on: Sandbox/adamg/ocql/branches/dbprovider/src/ocql/database/index.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/adamg/ocql/branches/dbprovider/src/ocql/database/tests.py
===================================================================
--- Sandbox/adamg/ocql/branches/dbprovider/src/ocql/database/tests.py	                        (rev 0)
+++ Sandbox/adamg/ocql/branches/dbprovider/src/ocql/database/tests.py	2008-06-19 15:37:19 UTC (rev 87553)
@@ -0,0 +1,13 @@
+import unittest
+import doctest
+from zope.testing.doctestunit import DocTestSuite,DocFileSuite
+
+def test_suite():
+    flags =  doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS
+    return unittest.TestSuite((
+        DocTestSuite('ocql.database.index')
+        ))
+
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: Sandbox/adamg/ocql/branches/dbprovider/src/ocql/database/tests.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Modified: Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/mentor.py
===================================================================
--- Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/mentor.py	2008-06-19 14:09:09 UTC (rev 87552)
+++ Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/mentor.py	2008-06-19 15:37:19 UTC (rev 87553)
@@ -27,3 +27,6 @@
     name = u''
 
     project=None
+
+    def __repr__(self):
+        return "%s <%s>" % (self.__class__.__name__, self.name)

Modified: Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/organization.py
===================================================================
--- Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/organization.py	2008-06-19 14:09:09 UTC (rev 87552)
+++ Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/organization.py	2008-06-19 15:37:19 UTC (rev 87553)
@@ -28,3 +28,6 @@
 
     # See google.interfaces.IOrganization
     name = u''
+
+    def __repr__(self):
+        return "%s <%s>" % (self.__class__.__name__, self.name)

Modified: Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/project.py
===================================================================
--- Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/project.py	2008-06-19 14:09:09 UTC (rev 87552)
+++ Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/project.py	2008-06-19 15:37:19 UTC (rev 87553)
@@ -29,3 +29,6 @@
 
     # See google.interfaces.IProject
     name = u''
+
+    def __repr__(self):
+        return "%s <%s>" % (self.__class__.__name__, self.name)

Modified: Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/student.py
===================================================================
--- Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/student.py	2008-06-19 14:09:09 UTC (rev 87552)
+++ Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/sample/student.py	2008-06-19 15:37:19 UTC (rev 87553)
@@ -26,3 +26,6 @@
     implements(IStudent)
 
     name = u''
+
+    def __repr__(self):
+        return "%s <%s>" % (self.__class__.__name__, self.name)

Modified: Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/utils.py
===================================================================
--- Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/utils.py	2008-06-19 14:09:09 UTC (rev 87552)
+++ Sandbox/adamg/ocql/branches/dbprovider/src/ocql/testing/utils.py	2008-06-19 15:37:19 UTC (rev 87553)
@@ -11,6 +11,7 @@
 from zope.app.catalog.catalog import Catalog
 from zope.app.catalog.interfaces import ICatalog
 from zope.app.catalog.field import FieldIndex
+from ocql.database.index import AllIndex
 
 from zope.app.intid import IntIds
 from zope.app.intid.interfaces import IIntIds
@@ -49,6 +50,11 @@
 
     cat['mentor_name'] = FieldIndex('name', IMentor)
 
+    cat['all_students'] = AllIndex(IStudent)
+    cat['all_mentors'] = AllIndex(IMentor)
+    cat['all_projects'] = AllIndex(IProject)
+    cat['all_orgs'] = AllIndex(IOrganization)
+
     m1 = Mentor()
     m1.name = u"John Doe"
     id = intids.register(m1)
@@ -90,4 +96,10 @@
 
     for r in results:
         obj = intids.getObject(r)
+        print obj
+
+    results = cat.apply({'all_students':(1,1)})
+
+    for r in results:
+        obj = intids.getObject(r)
         print obj
\ No newline at end of file

Modified: Sandbox/adamg/ocql/branches/dbprovider/src/ocql/tests/test_old.py
===================================================================
--- Sandbox/adamg/ocql/branches/dbprovider/src/ocql/tests/test_old.py	2008-06-19 14:09:09 UTC (rev 87552)
+++ Sandbox/adamg/ocql/branches/dbprovider/src/ocql/tests/test_old.py	2008-06-19 15:37:19 UTC (rev 87553)
@@ -506,7 +506,7 @@
         #                 Identifier(metadata, symbols, 'code')))
         #    ] ,Identifier(metadata, symbols,'c') )
         #
-        #self.doit(qo, set(['C1','C2','C3']))
+        #self.doit(query, qo, set(['C1','C2','C3']))
 
 def test_suite():
     flags =  doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS



More information about the Checkins mailing list