[Checkins] SVN: lovely.tag/trunk/src/lovely/tag/ Added the possibility to use the tagging engine in catalogs.

Jürgen Kartnaller juergen at kartnaller.at
Wed Oct 4 04:24:41 EDT 2006


Log message for revision 70517:
  Added the possibility to use the tagging engine in catalogs.
    It is now possible to add an index to a catalog which uses the tagging
    engine to look up for items containing tags.
  
    Alllows to query for 'any_of' and 'all_of'.
  

Changed:
  A   lovely.tag/trunk/src/lovely/tag/index.py
  A   lovely.tag/trunk/src/lovely/tag/index.txt
  U   lovely.tag/trunk/src/lovely/tag/interfaces.py
  U   lovely.tag/trunk/src/lovely/tag/tests.py

-=-
Added: lovely.tag/trunk/src/lovely/tag/index.py
===================================================================
--- lovely.tag/trunk/src/lovely/tag/index.py	2006-10-04 08:21:50 UTC (rev 70516)
+++ lovely.tag/trunk/src/lovely/tag/index.py	2006-10-04 08:24:40 UTC (rev 70517)
@@ -0,0 +1,71 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""A catalog index wich uses the tagging engine.
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import persistent
+from BTrees.IFBTree import IFTreeSet
+
+from zope import interface
+from zope import component
+
+from zope.cachedescriptors.property import Lazy
+from zope.index.interfaces import IInjection
+
+from zope.app.container.contained import Contained
+
+from lovely.tag.interfaces import ITaggingEngine, ITagIndex
+
+
+class TagIndex(persistent.Persistent, Contained):
+    interface.implements(ITagIndex, IInjection)
+
+    def __init__(self, engineName=''):
+        self.engineName = engineName
+
+    @Lazy
+    def engine(self):
+        return component.getUtility(ITaggingEngine, name = self.engineName)
+
+    def index_doc(self, docid, value):
+        # not used but makes the catalog happy
+        pass
+
+    def unindex_doc(self, docid, value):
+        # not used but makes the catalog happy
+        pass
+
+    def apply(self, query):
+        if 'any_of' in query:
+            tags = query['any_of']
+            items = self.engine.getItems(tags)
+        elif 'all_of' in query:
+            tags = query['all_of']
+            items = None
+            for tag in tags:
+                it = self.engine.getItems([tag])
+                if items is None:
+                    items = it
+                else:
+                    items = items.intersection(it)
+                if not items:
+                    break
+        if items:
+            items = [id for id in items]
+            return IFTreeSet(items)
+        return None
+


Property changes on: lovely.tag/trunk/src/lovely/tag/index.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: lovely.tag/trunk/src/lovely/tag/index.txt
===================================================================
--- lovely.tag/trunk/src/lovely/tag/index.txt	2006-10-04 08:21:50 UTC (rev 70516)
+++ lovely.tag/trunk/src/lovely/tag/index.txt	2006-10-04 08:24:40 UTC (rev 70517)
@@ -0,0 +1,72 @@
+=========
+Tag Index
+=========
+
+The tag index allows the use of the tags of a tagging engine in a catallog
+lookup.
+
+  >>> from lovely.tag.index import TagIndex
+
+  >>> index = TagIndex()
+
+  >>> index.apply({'any_of':['lovely']})
+  Traceback (most recent call last):
+  ...
+  ComponentLookupError: (<InterfaceClass lovely.tag.interfaces.ITaggingEngine>, '')
+
+We need a tagging engine.
+
+  >>> from zope import component
+  >>> from lovely.tag.engine import TaggingEngine
+  >>> from lovely.tag.interfaces import ITaggingEngine
+  >>> engine = TaggingEngine()
+  >>> component.provideUtility(engine, ITaggingEngine)
+  >>> index.apply({'any_of':['lovely']}) is None
+  True
+
+  >>> import zope.component
+  >>> from zope.app.keyreference import testing
+  >>> zope.component.provideAdapter(testing.SimpleKeyReference)
+
+  >>> engine.update(1, u'srichter', [u'USA', u'personal'])
+  >>> engine.update(2, u'srichter', [u'austria', u'lovely'])
+  >>> engine.update(3, u'jodok', [u'Austria', u'personal'])
+  >>> engine.update(4, u'jodok', [u'austria', u'lovely', u'work'])
+
+  >>> sorted(index.apply({'any_of':['lovely']}))
+  [2, 4]
+  >>> sorted(index.apply({'all_of':['lovely', 'work']}))
+  [4]
+
+
+Use With A Named Engine
+-----------------------
+
+  >>> namedEngine = TaggingEngine()
+  >>> component.provideUtility(namedEngine, ITaggingEngine, 'named')
+  >>> namedIndex = TagIndex('named')
+
+  >>> namedEngine.update(5, u'kartnaller', [u'Austria', u'personal'])
+  >>> namedEngine.update(6, u'kartnaller', [u'austria', u'lovely'])
+  >>> namedEngine.update(4, u'jodok', [u'austria', u'lovely', u'work'])
+
+  >>> sorted(namedIndex.apply({'any_of':['lovely']}))
+  [4, 6]
+
+
+Use In A Catalog
+----------------
+
+  >>> from zope.app.catalog.catalog import Catalog
+  >>> cat = Catalog()
+  >>> cat['tags'] = index
+
+  >>> sorted(cat.apply({'tags':{'any_of':['lovely']}}))
+  [2, 4]
+
+  >>> cat['namedTags'] = namedIndex
+  >>> sorted(cat.apply({'tags':{'any_of':['lovely']},
+  ...                   'namedTags':{'any_of':['lovely']}
+  ...                  }))
+  [4]
+


Property changes on: lovely.tag/trunk/src/lovely/tag/index.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: lovely.tag/trunk/src/lovely/tag/interfaces.py
===================================================================
--- lovely.tag/trunk/src/lovely/tag/interfaces.py	2006-10-04 08:21:50 UTC (rev 70516)
+++ lovely.tag/trunk/src/lovely/tag/interfaces.py	2006-10-04 08:24:40 UTC (rev 70517)
@@ -135,4 +135,19 @@
     tags = zope.schema.Set(title=u'Tags',
                            description=u'Tags for the current User',
                            required=False)
-    
+
+
+class ITagIndex(zope.interface.Interface):
+
+    def apply(query):
+        """Return None or an IFTreeSet of the doc ids that match the query.
+
+        query is a dict with one of the following keys: and, or
+
+        Any one of the keys may be used; using more than one is not allowed.
+
+        'any_of' : docs containing at least one of the keys are returned. The
+                   value is a list containing the tags.
+        'all_of' : docs containing at alll of the tags are returned. The value
+                   is a list containing the tags.
+        """

Modified: lovely.tag/trunk/src/lovely/tag/tests.py
===================================================================
--- lovely.tag/trunk/src/lovely/tag/tests.py	2006-10-04 08:21:50 UTC (rev 70516)
+++ lovely.tag/trunk/src/lovely/tag/tests.py	2006-10-04 08:24:40 UTC (rev 70517)
@@ -37,6 +37,7 @@
                      tearDown=placelesssetup.tearDown,
                      optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
                      ),
+        DocFileSuite('index.txt',),
         stressSuite
         ))
 



More information about the Checkins mailing list