[Checkins] SVN: zope.psycopgda/trunk/ - Use psycopg2 instead of psycopg

Nikolay Kim fafhrd91 at gmail.com
Wed Jan 6 12:29:23 EST 2010


Log message for revision 107748:
  - Use psycopg2 instead of psycopg
  
  - Move ``zope.app.form`` dependency to 'zcml' extras
  
  - Added missing dependencies
  
  
  

Changed:
  U   zope.psycopgda/trunk/CHANGES.txt
  U   zope.psycopgda/trunk/setup.py
  U   zope.psycopgda/trunk/src/zope/psycopgda/adapter.py
  U   zope.psycopgda/trunk/src/zope/psycopgda/configure.zcml
  U   zope.psycopgda/trunk/src/zope/psycopgda/tests.py

-=-
Modified: zope.psycopgda/trunk/CHANGES.txt
===================================================================
--- zope.psycopgda/trunk/CHANGES.txt	2010-01-06 16:52:56 UTC (rev 107747)
+++ zope.psycopgda/trunk/CHANGES.txt	2010-01-06 17:29:23 UTC (rev 107748)
@@ -2,12 +2,23 @@
 CHANGES
 =======
 
-1.1.1 (2008/01/26)
+1.2.0 (2010-01-06)
 ------------------
 
+- Use psycopg2 instead of psycopg
+
+- Move ``zope.app.form`` dependency to 'zcml' extras
+
+- Added missing dependencies
+
+
+1.1.1 (2008-01-26)
+------------------
+
 - Bug: Some classes were incorrectly looked up.
 
-1.1.0 (2008/01/26)
+
+1.1.0 (2008-01-26)
 ------------------
 
 - Feature: Produced a real egg.

Modified: zope.psycopgda/trunk/setup.py
===================================================================
--- zope.psycopgda/trunk/setup.py	2010-01-06 16:52:56 UTC (rev 107747)
+++ zope.psycopgda/trunk/setup.py	2010-01-06 17:29:23 UTC (rev 107748)
@@ -1,6 +1,6 @@
 ##############################################################################
 #
-# Copyright (c) 2007 Zope Corporation and Contributors.
+# Copyright (c) 2007 Zope Foundation and Contributors.
 # All Rights Reserved.
 #
 # This software is subject to the provisions of the Zope Public License,
@@ -24,10 +24,11 @@
 def read(*rnames):
     return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
 
+
 setup(name='zope.psycopgda',
       version = '1.2.0dev',
-      author='Zope Corporation and Contributors',
-      author_email='zope3-dev at zope.org',
+      author='Zope Foundation and Contributors',
+      author_email='zope-dev at zope.org',
       description='Psycopg Database Adapter for Zope 3',
       long_description=(
           read('README.txt')
@@ -52,15 +53,17 @@
       namespace_packages=['zope'],
       extras_require = dict(
           test=['zope.testing',
-                ]),
+                ],
+          zcml=['zope.app.form']),
       install_requires = [
-          # Versions 1.x is not a proper egg.
-          #'psycopgda',
+          # Versions 2.x is not a proper egg.
+          #'psycopg2',
           'setuptools',
           'zope.component',
           'zope.interface',
           'zope.rdb',
-          'zope.app.form',
+          'zope.datetime',
+          'zope.publisher',
           ],
       include_package_data = True,
       zip_safe = False,

Modified: zope.psycopgda/trunk/src/zope/psycopgda/adapter.py
===================================================================
--- zope.psycopgda/trunk/src/zope/psycopgda/adapter.py	2010-01-06 16:52:56 UTC (rev 107747)
+++ zope.psycopgda/trunk/src/zope/psycopgda/adapter.py	2010-01-06 17:29:23 UTC (rev 107748)
@@ -1,6 +1,6 @@
 ##############################################################################
 #
-# Copyright (c) 2002-2006 Zope Corporation and Contributors.
+# Copyright (c) 2002-2006 Zope Foundation and Contributors.
 # All Rights Reserved.
 #
 # This software is subject to the provisions of the Zope Public License,
@@ -16,7 +16,8 @@
 $Id$
 """
 import datetime
-import psycopg
+import psycopg2
+import psycopg2.extensions
 import re
 import sys
 import zope.interface
@@ -285,13 +286,15 @@
     return _conv_string
 
 # User-defined types
-DATE = psycopg.new_type((DATE_OID,), "ZDATE", _conv_date)
-TIME = psycopg.new_type((TIME_OID,), "ZTIME", _conv_time)
-TIMETZ = psycopg.new_type((TIMETZ_OID,), "ZTIMETZ", _conv_timetz)
-TIMESTAMP = psycopg.new_type((TIMESTAMP_OID,), "ZTIMESTAMP", _conv_timestamp)
-TIMESTAMPTZ = psycopg.new_type((TIMESTAMPTZ_OID,), "ZTIMESTAMPTZ",
-                                _conv_timestamptz)
-INTERVAL = psycopg.new_type((INTERVAL_OID,), "ZINTERVAL", _conv_interval)
+DATE = psycopg2.extensions.new_type((DATE_OID,), "ZDATE", _conv_date)
+TIME = psycopg2.extensions.new_type((TIME_OID,), "ZTIME", _conv_time)
+TIMETZ = psycopg2.extensions.new_type((TIMETZ_OID,), "ZTIMETZ", _conv_timetz)
+TIMESTAMP = psycopg2.extensions.new_type(
+    (TIMESTAMP_OID,), "ZTIMESTAMP", _conv_timestamp)
+TIMESTAMPTZ = psycopg2.extensions.new_type(
+    (TIMESTAMPTZ_OID,), "ZTIMESTAMPTZ", _conv_timestamptz)
+INTERVAL = psycopg2.extensions.new_type(
+    (INTERVAL_OID,), "ZINTERVAL", _conv_interval)
 
 
 dsn2option_mapping = {'host': 'host',
@@ -302,16 +305,19 @@
 
 def registerTypes(encoding):
     """Register type conversions for psycopg"""
-    psycopg.register_type(DATE)
-    psycopg.register_type(TIME)
-    psycopg.register_type(TIMETZ)
-    psycopg.register_type(TIMESTAMP)
-    psycopg.register_type(TIMESTAMPTZ)
-    psycopg.register_type(INTERVAL)
-    STRING = psycopg.new_type((CHAR_OID, TEXT_OID, BPCHAR_OID, VARCHAR_OID),
-                              "ZSTRING", _get_string_conv(encoding))
-    psycopg.register_type(STRING)
+    psycopg2.extensions.register_type(DATE)
+    psycopg2.extensions.register_type(TIME)
+    psycopg2.extensions.register_type(TIMETZ)
+    psycopg2.extensions.register_type(TIMESTAMP)
+    psycopg2.extensions.register_type(TIMESTAMPTZ)
+    psycopg2.extensions.register_type(INTERVAL)
+    STRING = psycopg.extensions.new_type(
+        (CHAR_OID, TEXT_OID, BPCHAR_OID, VARCHAR_OID),
+        "ZSTRING", _get_string_conv(encoding))
+    psycopg2.extensions.register_type(STRING)
+    psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
 
+
 class PsycopgAdapter(zope.rdb.ZopeDatabaseAdapter):
     """A PsycoPG adapter for Zope3.
 
@@ -348,36 +354,35 @@
             if conn_info[dsnname]:
                 conn_list.append('%s=%s' % (optname, conn_info[dsnname]))
         conn_str = ' '.join(conn_list)
-        connection = psycopg.connect(conn_str)
+        connection = psycopg2.connect(conn_str)
 
         # Ensure we are in SERIALIZABLE transaction isolation level.
         # This is the default under psycopg1, but changed to READ COMMITTED
         # under psycopg2. This should become an option if anyone wants
         # different isolation levels.
-        connection.set_isolation_level(3)
+        connection.set_isolation_level(
+            psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE)
 
         return connection
 
+    def disconnect(self):
+        if self.isConnected():
+            try:
+                self._v_connection.close()
+            except psycopg2.InterfaceError:
+                pass
 
+            self._v_connection = None
+
+
 def _handle_psycopg_exception(error):
     """Called from a exception handler for psycopg.Error.
 
     If we have a serialization exception or a deadlock, we should retry the
     transaction by raising a Retry exception. Otherwise, we reraise.
     """
-    if not error.args:
-        raise
-    msg = error.args[0]
-    # These messages are from PostgreSQL 8.0. They may change between
-    # PostgreSQL releases - if so, the different messages should be added
-    # rather than the existing ones changed so this logic works with
-    # different versions.
-    if msg.startswith(
-            'ERROR:  could not serialize access due to concurrent update'
-            ):
+    if isinstance(error, psycopg2.extensions.TransactionRollbackError):
         raise Retry(sys.exc_info())
-    if msg.startswith('ERROR:  deadlock detected'):
-        raise Retry(sys.exc_info())
     raise
 
 
@@ -405,7 +410,7 @@
         """See IZopeCursor"""
         try:
             return zope.rdb.ZopeCursor.execute(self, operation, parameters)
-        except psycopg.Error, error:
+        except psycopg2.Error, error:
             _handle_psycopg_exception(error)
 
     def executemany(operation, seq_of_parameters=None):

Modified: zope.psycopgda/trunk/src/zope/psycopgda/configure.zcml
===================================================================
--- zope.psycopgda/trunk/src/zope/psycopgda/configure.zcml	2010-01-06 16:52:56 UTC (rev 107747)
+++ zope.psycopgda/trunk/src/zope/psycopgda/configure.zcml	2010-01-06 17:29:23 UTC (rev 107748)
@@ -1,51 +1,31 @@
 <configure
-    xmlns="http://namespaces.zope.org/zope"
-    xmlns:browser="http://namespaces.zope.org/browser"
-    i18n_domain="psycopgda">
+   xmlns="http://namespaces.zope.org/zope"
+   i18n_domain="psycopgda">
 
   <class class=".adapter.PsycopgAdapter">
     <factory id="zope.da.PsycopgDA" />
     <require
-        permission="zope.rdb.Use"
-        interface="zope.rdb.interfaces.IZopeDatabaseAdapter"
-        />
+       permission="zope.rdb.Use"
+       interface="zope.rdb.interfaces.IZopeDatabaseAdapter"
+       />
     <require
-        permission="zope.ManageServices"
-        interface="zope.rdb.interfaces.IZopeDatabaseAdapterManagement"
-        />
+       permission="zope.ManageServices"
+       interface="zope.rdb.interfaces.IZopeDatabaseAdapterManagement"
+       />
   </class>
-
+  
   <class class=".adapter.PsycopgConnection">
     <require
-        permission="zope.rdb.Use"
-        interface="zope.rdb.interfaces.IZopeConnection"
-        />
+       permission="zope.rdb.Use"
+       interface="zope.rdb.interfaces.IZopeConnection"
+       />
   </class>
-
+  
   <class class=".adapter.PsycopgCursor">
     <require
-        permission="zope.rdb.Use"
-        interface="zope.rdb.interfaces.IZopeCursor"
-        />
+       permission="zope.rdb.Use"
+       interface="zope.rdb.interfaces.IZopeCursor"
+       />
   </class>
 
-  <browser:addform
-      name="AddPsycopgDA"
-      schema="zope.rdb.interfaces.IManageableZopeDatabaseAdapter"
-      label="Add Psycopg (PostGreSQL) Database Adapter"
-      content_factory=".adapter.PsycopgAdapter"
-      arguments="dsn"
-      fields="dsn"
-      permission="zope.ManageContent"
-      />
-
-  <!-- Menu entry for "add utility" menu -->
-  <browser:addMenuItem
-      class=".adapter.PsycopgAdapter"
-      title="Psycopg DA"
-      description="A PostgreSQL Database Adapter using the Psycopg driver"
-      permission="zope.ManageApplication"
-      view="AddPsycopgDA"
-      />
-
 </configure>

Modified: zope.psycopgda/trunk/src/zope/psycopgda/tests.py
===================================================================
--- zope.psycopgda/trunk/src/zope/psycopgda/tests.py	2010-01-06 16:52:56 UTC (rev 107747)
+++ zope.psycopgda/trunk/src/zope/psycopgda/tests.py	2010-01-06 17:29:23 UTC (rev 107748)
@@ -1,6 +1,6 @@
 ##############################################################################
 #
-# Copyright (c) 2002-2004 Zope Corporation and Contributors.
+# Copyright (c) 2002-2004 Zope Foundation and Contributors.
 # All Rights Reserved.
 #
 # This software is subject to the provisions of the Zope Public License,
@@ -16,7 +16,7 @@
 $Id$
 """
 import datetime
-import psycopg
+import psycopg2
 import zope.psycopgda.adapter as adapter
 from unittest import TestCase, TestSuite, main, makeSuite
 from zope.psycopgda.adapter import _conv_date, _conv_time, _conv_timestamp
@@ -59,10 +59,10 @@
 
     __shared_state = {}     # 'Borg' design pattern
 
-    DATE = psycopg.DATE
-    TIME = psycopg.TIME
-    DATETIME = psycopg.DATETIME
-    INTERVAL = psycopg.INTERVAL
+    DATE = psycopg2.DATE
+    TIME = psycopg2.TIME
+    DATETIME = psycopg2.DATETIME
+    INTERVAL = psycopg2.INTERVAL
 
     def __init__(self):
         self.__dict__ = self.__shared_state
@@ -179,11 +179,11 @@
 class TestPsycopgAdapter(TestCase):
 
     def setUp(self):
-        self.real_psycopg = adapter.psycopg
-        adapter.psycopg = self.psycopg_stub = PsycopgStub()
+        self.real_psycopg = adapter.psycopg2
+        adapter.psycopg2 = self.psycopg_stub = PsycopgStub()
 
     def tearDown(self):
-        adapter.psycopg = self.real_psycopg
+        adapter.psycopg2 = self.real_psycopg
 
     def test_connection_factory(self):
         a = adapter.PsycopgAdapter(



More information about the checkins mailing list