[Zope3-checkins] CVS: Zope3/src/zope/app/rdb - __init__.py:1.7.4.1 configure.zcml:1.3.12.1 gadflyda.py:1.3.10.1

Grégoire Weber zope@i-con.ch
Sun, 22 Jun 2003 10:24:21 -0400


Update of /cvs-repository/Zope3/src/zope/app/rdb
In directory cvs.zope.org:/tmp/cvs-serv24874/src/zope/app/rdb

Modified Files:
      Tag: cw-mail-branch
	__init__.py configure.zcml gadflyda.py 
Log Message:
Synced up with HEAD

=== Zope3/src/zope/app/rdb/__init__.py 1.7 => 1.7.4.1 ===
--- Zope3/src/zope/app/rdb/__init__.py:1.7	Mon May 12 05:01:24 2003
+++ Zope3/src/zope/app/rdb/__init__.py	Sun Jun 22 10:23:20 2003
@@ -19,6 +19,8 @@
 
 $Id$
 """
+__metaclass__ = type
+
 from types import StringTypes
 
 from persistence import Persistent
@@ -26,7 +28,9 @@
 from transaction import get_transaction
 from transaction.interfaces import IDataManager
 
-from zope.security import checker
+from zope.security.checker import NamesChecker
+
+from zope.interface import implements
 
 from zope.app.interfaces.rdb import DatabaseException
 from zope.app.interfaces.rdb import IResultSet, ISQLCommand
@@ -42,7 +46,7 @@
     Currently we don't do lazy instantation of rows.
     """
 
-    __implements__ = IResultSet
+    implements(IResultSet)
     __slots__ = ('columns',)
 
     def __init__(self, columns, rows):
@@ -74,7 +78,7 @@
 class SQLCommand:
     """A simple version of a SQL Command."""
 
-    __implements__ = ISQLCommand
+    implements(ISQLCommand)
 
     def __init__(self, connection_name='', sql=''):
         self.connectionName = connection_name
@@ -96,7 +100,7 @@
 
 class ZopeDatabaseAdapter(Persistent):
 
-    __implements__ = IZopeDatabaseAdapter
+    implements(IZopeDatabaseAdapter)
     _v_connection =  None
 
     def __init__(self, dsn):
@@ -118,8 +122,16 @@
     def connect(self):
         'See IZopeDatabaseAdapter'
         if not self.isConnected():
-            self._v_connection = ZopeConnection(self._connection_factory(),
-                                                self)
+            try:
+                self._v_connection = ZopeConnection(
+                    self._connection_factory(), self)
+            # Note: I added the general Exception, since the DA can return
+            # implementation-specific errors. But we really want to catch all
+            # issues at this point, so that we can convert it to a
+            # DatabaseException. 
+            except Exception, error:
+                raise DatabaseException, str(error)
+
 
     def disconnect(self):
         'See IZopeDatabaseAdapter'
@@ -169,9 +181,11 @@
        dbname       database name
        parameters   a mapping of additional parameters to their values
     """
-    # XXX these should not be assertions
-    assert isinstance(dsn, StringTypes), 'The dsn is not a string.'
-    assert dsn.startswith('dbi://'), 'Invalid DSN; must start with "dbi://"'
+    if not isinstance(dsn, StringTypes):
+        raise ValueError('The dsn is not a string. It is a %s'
+                         % repr(type(dsn)))
+    if not dsn.startswith('dbi://'):
+        raise ValueError('Invalid DSN; must start with "dbi://": %s' % dsn)
 
     result = {}
 
@@ -206,8 +220,7 @@
 
     # Get username and password from DSN
     if dsn:
-        # XXX: what if password contains a colon?
-        username, password = dsn.split(':')
+        username, password = dsn.split(':', 1)
     else:
         username, password = '', ''
 
@@ -218,7 +231,7 @@
 
 
 class ZopeCursor:
-    __implements__ = IZopeCursor
+    implements(IZopeCursor)
 
     def __init__(self, cursor, connection):
         self.cursor = cursor
@@ -256,8 +269,9 @@
         converters = [getConverter(col_info[1])
                       for col_info in self.cursor.description]
 ## A possible optimization -- need benchmarks to check if it is worth it
-##      if filter(lambda x: x is not ZopeDatabaseAdapter.identity, converters):
+##      if [x for x in converters if x is not ZopeDatabaseAdapter.identity]:
 ##          return results  # optimize away
+
         def convertRow(row):
             return map(lambda converter, value: converter(value),
                        converters, row)
@@ -266,7 +280,7 @@
 
 class ZopeConnection:
 
-    __implements__ =  IZopeConnection
+    implements(IZopeConnection)
 
     def __init__(self, conn, typeinfo):
         self.conn = conn
@@ -293,7 +307,6 @@
         self._txn_registered = False
         self.conn.commit()
 
-
     def rollback(self):
         'See IDBIConnection'
         self._txn_registered = False
@@ -312,7 +325,7 @@
 
     try:
         cursor.execute(query)
-    except Exception, error:
+    except Exception, error:  # XXX This looks a bit yucky.
         raise DatabaseException(str(error))
 
     if cursor.description is not None:
@@ -328,7 +341,7 @@
 
 class ZopeDBTransactionManager:
 
-    __implements__ =  IDataManager
+    implements(IDataManager)
 
     def __init__(self, dbconn):
         self._dbconn = dbconn
@@ -371,12 +384,28 @@
                 return c
         return 0
 
+class InstanceOnlyDescriptor:
+    __marker = object()
+    def __init__(self, value=__marker):
+        if value is not self.__marker:
+            self.value = value
+
+    def __get__(self, inst, cls=None):
+        if inst is None:
+            raise AttributeError
+        return self.value
+
+    def __set__(self, inst, value):
+        self.value = value
+
+    def __delete__(self, inst):
+        del self.value
 
 def RowClassFactory(columns):
     """Creates a Row object"""
     klass_namespace = {}
-
-    klass_namespace['__Security_checker__'] = checker.NamesChecker(columns)
+    klass_namespace['__Security_checker__'] = InstanceOnlyDescriptor(
+        NamesChecker(columns))
     klass_namespace['__slots__'] = tuple(columns)
 
     return type('GeneratedRowClass', (Row,), klass_namespace)


=== Zope3/src/zope/app/rdb/configure.zcml 1.3 => 1.3.12.1 ===
--- Zope3/src/zope/app/rdb/configure.zcml:1.3	Tue Apr 22 18:17:40 2003
+++ Zope3/src/zope/app/rdb/configure.zcml	Sun Jun 22 10:23:20 2003
@@ -3,15 +3,28 @@
 >
 
 <content class="zope.app.rdb.gadflyda.GadflyAdapter">
-  <factory id="GadflyDA"
-      permission="zope.Public" />
-  <require permission="zope.Public" 
-      interface="zope.app.interfaces.rdb.IZopeDatabaseAdapter" />
+  <factory
+      id="GadflyDA"
+      permission="zope.Public"
+      />
+  <require
+      permission="zope.Public"
+      interface="zope.app.interfaces.rdb.IZopeDatabaseAdapter"
+      />
   <implements
-      interface="zope.app.interfaces.annotation.IAttributeAnnotatable" />
+      interface="zope.app.interfaces.annotation.IAttributeAnnotatable"
+      />
   <implements
       interface=
-      "zope.app.interfaces.services.configuration.IAttributeUseConfigurable"
+      "zope.app.interfaces.services.registration.IAttributeRegisterable"
+      />
+</content>
+
+<content class="zope.app.rdb.ResultSet">
+  <require
+      permission="zope.View"
+      attributes="__getitem__ __getslice__ __len__ __iter__ __contains__
+                  index count __str__ __add__ __radd__"
       />
 </content>
 


=== Zope3/src/zope/app/rdb/gadflyda.py 1.3 => 1.3.10.1 ===
--- Zope3/src/zope/app/rdb/gadflyda.py:1.3	Thu May  1 15:35:30 2003
+++ Zope3/src/zope/app/rdb/gadflyda.py	Sun Jun 22 10:23:20 2003
@@ -29,8 +29,6 @@
 class GadflyAdapter(ZopeDatabaseAdapter):
     """A Gadfly adapter for Zope3"""
 
-    __implements__ = ZopeDatabaseAdapter.__implements__
-
     def _getGadflyRoot(self):
         # XXX: Need to write a configuration directive for setting this up
         # At the moment gadfly root is 'gadfly' under the instance home (which