[Zope-Checkins] SVN: Zope/trunk/src/Products/ZCatalog/ Brain PEP8 cleanup and define __contains__ for brains.

Hanno Schlichting hannosch at hannosch.eu
Sat Jul 31 16:54:50 EDT 2010


Log message for revision 115283:
  Brain PEP8 cleanup and define __contains__ for brains.
  

Changed:
  U   Zope/trunk/src/Products/ZCatalog/CatalogBrains.py
  U   Zope/trunk/src/Products/ZCatalog/interfaces.py
  U   Zope/trunk/src/Products/ZCatalog/tests/test_brains.py

-=-
Modified: Zope/trunk/src/Products/ZCatalog/CatalogBrains.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/CatalogBrains.py	2010-07-31 20:45:52 UTC (rev 115282)
+++ Zope/trunk/src/Products/ZCatalog/CatalogBrains.py	2010-07-31 20:54:49 UTC (rev 115283)
@@ -13,7 +13,8 @@
 
 from zope.interface import implements
 
-import Acquisition, Record
+import Acquisition
+import Record
 from ZODB.POSException import ConflictError
 
 from interfaces import ICatalogBrain
@@ -22,16 +23,20 @@
 # Use 'catalog-getObject-raises off' in zope.conf to restore old behavior.
 GETOBJECT_RAISES = True
 
+
 class AbstractCatalogBrain(Record.Record, Acquisition.Implicit):
     """Abstract base brain that handles looking up attributes as
     required, and provides just enough smarts to let us get the URL, path,
     and cataloged object without having to ask the catalog directly.
     """
     implements(ICatalogBrain)
-    
+
     def has_key(self, key):
-        return self.__record_schema__.has_key(key)
+        return key in self.__record_schema__
 
+    def __contains__(self, name):
+        return name in self.__record_schema__
+
     def getPath(self):
         """Get the physical path for this record"""
         return self.aq_parent.getpath(self.data_record_id_)
@@ -39,11 +44,11 @@
     def getURL(self, relative=0):
         """Generate a URL for this record"""
         # XXX The previous implementation attempted to eat errors coming from
-        #     REQUEST.physicalPathToURL. Unfortunately it also ate 
-        #     ConflictErrors (from getPath), which is bad. Staring at the 
-        #     relevent code in HTTPRequest.py it's unclear to me what could be 
-        #     raised by it so I'm removing the exception handling here all 
-        #     together. If undesired exceptions get raised somehow we should 
+        #     REQUEST.physicalPathToURL. Unfortunately it also ate
+        #     ConflictErrors (from getPath), which is bad. Staring at the
+        #     relevent code in HTTPRequest.py it's unclear to me what could be
+        #     raised by it so I'm removing the exception handling here all
+        #     together. If undesired exceptions get raised somehow we should
         #     avoid bare except band-aids and find a real solution.
         return self.REQUEST.physicalPathToURL(self.getPath(), relative)
 
@@ -101,6 +106,7 @@
         """Return the record ID for this object."""
         return self.data_record_id_
 
+
 class NoBrainer:
     """ This is an empty class to use when no brain is specified. """
     pass

Modified: Zope/trunk/src/Products/ZCatalog/interfaces.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/interfaces.py	2010-07-31 20:45:52 UTC (rev 115282)
+++ Zope/trunk/src/Products/ZCatalog/interfaces.py	2010-07-31 20:54:49 UTC (rev 115283)
@@ -244,19 +244,23 @@
         pghandler -- optional Progresshandler as defined in ProgressHandler.py
         (see also README.txt)
         """
-        
-# XXX This should inherit from an IRecord interface, if there ever is one.
+
+# This should inherit from an IRecord interface, if there ever is one.
 class ICatalogBrain(Interface):
     """Catalog brain that handles looking up attributes as
     required, and provides just enough smarts to let us get the URL, path,
     and cataloged object without having to ask the catalog directly.
     """
+
     def has_key(key):
         """Record has this field"""
 
+    def __contains__(self, name):
+        """Record has this field"""
+
     def getPath():
         """Get the physical path for this record"""
-        
+
     def getURL(relative=0):
         """Generate a URL for this record"""
 
@@ -272,7 +276,6 @@
         Will return None if the object cannot be found via its cataloged path
         (i.e., it was deleted or moved without recataloging), or if the user is
         not authorized to access the object.
-
         """
 
     def getRID():

Modified: Zope/trunk/src/Products/ZCatalog/tests/test_brains.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/tests/test_brains.py	2010-07-31 20:45:52 UTC (rev 115282)
+++ Zope/trunk/src/Products/ZCatalog/tests/test_brains.py	2010-07-31 20:54:49 UTC (rev 115283)
@@ -14,27 +14,38 @@
 """
 
 import unittest
+
 import Acquisition
 from zExceptions import Unauthorized
 from ZODB.POSException import ConflictError
 
+_marker = object()
+
+
 class Happy(Acquisition.Implicit):
     """Happy content"""
+
     def __init__(self, id):
         self.id = id
+
     def check(self):
         pass
 
+
 class Secret(Happy):
     """Object that raises Unauthorized when accessed"""
+
     def check(self):
         raise Unauthorized
 
+
 class Conflicter(Happy):
     """Object that raises ConflictError when accessed"""
+
     def check(self):
         raise ConflictError
 
+
 class DummyRequest(Acquisition.Implicit):
 
     def physicalPathToURL(self, path, relative=False):
@@ -42,13 +53,12 @@
             path = 'http://superbad.com' + path
         return path
 
-_marker = object()
 
 class DummyCatalog(Acquisition.Implicit):
 
-    _objs = {'/happy':Happy('happy'),
-             '/secret':Secret('secret'),
-             '/conflicter':Conflicter('conflicter')}
+    _objs = {'/happy': Happy('happy'),
+             '/secret': Secret('secret'),
+             '/conflicter': Conflicter('conflicter')}
     _paths = _objs.keys() + ['/zonked']
     _paths.sort()
 
@@ -78,14 +88,17 @@
         return self.restrictedTraverse(self._paths[rid])
 
     def resolve_url(self, path, REQUEST):
-        path =  path[path.find('/', path.find('//')+1):] # strip server part
+        # strip server part
+        path = path[path.find('/', path.find('//') + 1):]
         return self.restrictedTraverse(path)
 
+
 class ConflictingCatalog(DummyCatalog):
 
     def getpath(self, rid):
         raise ConflictError
 
+
 class BrainsTestBase:
 
     _old_flag = None
@@ -110,15 +123,17 @@
 
     def _makeBrain(self, rid):
         from Products.ZCatalog.CatalogBrains import AbstractCatalogBrain
+
         class Brain(AbstractCatalogBrain):
-            __record_schema__ = {'test_field': 0, 'data_record_id_':1}
+            __record_schema__ = {'test_field': 0, 'data_record_id_': 1}
+
         return Brain(('test', rid)).__of__(self.cat)
 
     def testHasKey(self):
         b = self._makeBrain(1)
-        self.failUnless(b.has_key('test_field'))
-        self.failUnless(b.has_key('data_record_id_'))
-        self.failIf(b.has_key('godel'))
+        self.failUnless('test_field' in b)
+        self.failUnless('data_record_id_' in b)
+        self.failIf('godel' in b)
 
     def testGetPath(self):
         b = [self._makeBrain(rid) for rid in range(3)]
@@ -149,6 +164,7 @@
         self.assertEqual(b.getPath(), '/conflicter')
         self.assertRaises(ConflictError, b.getObject)
 
+
 class TestBrains(BrainsTestBase, unittest.TestCase):
 
     def _flag_value(self):
@@ -167,6 +183,7 @@
         self.assertRaises(KeyError, self.cat.getobject, 3)
         self.assertRaises((NotFound, AttributeError, KeyError), b.getObject)
 
+
 class TestBrainsOldBehavior(BrainsTestBase, unittest.TestCase):
 
     def _flag_value(self):
@@ -183,6 +200,7 @@
         self.assertRaises(KeyError, self.cat.getobject, 3)
         self.assertEqual(b.getObject(), None)
 
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(TestBrains))



More information about the Zope-Checkins mailing list