[Checkins] SVN: ZODB/trunk/src/ Added convenience methods ZODB.DB.open and ZEO.DB.open provide a

Jim Fulton jim at zope.com
Fri Apr 24 18:37:50 EDT 2009


Log message for revision 99480:
  Added convenience methods ZODB.DB.open and ZEO.DB.open provide a
  convenient way to open a connection to a database.  They open a
  database and return a connection to it. When the connection is
  closed, the database is closed as well.
  

Changed:
  U   ZODB/trunk/src/CHANGES.txt
  U   ZODB/trunk/src/ZEO/__init__.py
  U   ZODB/trunk/src/ZEO/tests/testZEO.py
  U   ZODB/trunk/src/ZODB/DB.py
  U   ZODB/trunk/src/ZODB/tests/testDB.py

-=-
Modified: ZODB/trunk/src/CHANGES.txt
===================================================================
--- ZODB/trunk/src/CHANGES.txt	2009-04-24 22:37:28 UTC (rev 99479)
+++ ZODB/trunk/src/CHANGES.txt	2009-04-24 22:37:50 UTC (rev 99480)
@@ -14,6 +14,11 @@
 New Features
 ------------
 
+- Convenience methods ZODB.DB.open and ZEO.DB.open provide a
+  convenient way to open a connection to a database.  They open a
+  database and return a connection to it. When the connection is
+  closed, the database is closed as well.
+
 - The zeopack script has gotten a number of improvements:
 
   - Simplified command-line interface. (The old interface is still
@@ -64,7 +69,7 @@
   increasingly expensive over time.
 
 - The monitor server didn't correctly report the actual number of
-  clients. 
+  clients.
 
 3.9.0a11 (2009-02-17)
 =====================

Modified: ZODB/trunk/src/ZEO/__init__.py
===================================================================
--- ZODB/trunk/src/ZEO/__init__.py	2009-04-24 22:37:28 UTC (rev 99479)
+++ ZODB/trunk/src/ZEO/__init__.py	2009-04-24 22:37:50 UTC (rev 99480)
@@ -25,6 +25,15 @@
     import ZEO.ClientStorage, ZODB
     return ZODB.DB(ZEO.ClientStorage.ClientStorage(*args, **kw))
 
+def open(*args, **kw):
+    db = DB(*args, **kw)
+    conn = db.open()
+    conn.onCloseCallback(db.close)
+    return conn
+
+DB.open = open
+del open
+
 def server(path=None, blob_dir=None, storage_conf=None, zeo_conf=None,
            port=None):
     """Convenience function to start a server for interactive exploration

Modified: ZODB/trunk/src/ZEO/tests/testZEO.py
===================================================================
--- ZODB/trunk/src/ZEO/tests/testZEO.py	2009-04-24 22:37:28 UTC (rev 99479)
+++ ZODB/trunk/src/ZEO/tests/testZEO.py	2009-04-24 22:37:50 UTC (rev 99480)
@@ -1151,6 +1151,30 @@
     >>> cs.close()
     """
 
+
+def open_convenience():
+    """Often, we just want to open a single connection.
+
+    >>> addr, _ = start_server(path='data.fs')
+    >>> conn = ZEO.DB.open(addr)
+    >>> conn.root()
+    {}
+
+    >>> conn.root()['x'] = 1
+    >>> transaction.commit()
+    >>> conn.close()
+
+    Let's make sure the database was cloased when we closed the
+    connection, and that the data is there.
+
+    >>> db = ZEO.DB(addr)
+    >>> conn = db.open()
+    >>> conn.root()
+    {'x': 1}
+    >>> db.close()
+    """
+
+
 slow_test_classes = [
     BlobAdaptedFileStorageTests, BlobWritableCacheTests,
     DemoStorageTests, FileStorageTests, MappingStorageTests,

Modified: ZODB/trunk/src/ZODB/DB.py
===================================================================
--- ZODB/trunk/src/ZODB/DB.py	2009-04-24 22:37:28 UTC (rev 99479)
+++ ZODB/trunk/src/ZODB/DB.py	2009-04-24 22:37:50 UTC (rev 99480)
@@ -329,6 +329,20 @@
     return before
 
 
+class Methods(object):
+
+    def __init__(self, name, ifunc, cfunc=None):
+        self.__name__ = name
+        self.im_func = ifunc
+        self.cm_func = cfunc
+
+    def __get__(self, inst, class_):
+        if inst is None:
+            if self.cm_func is None:
+                raise AttributeError("Only in instances", self.__name__)
+            return self.cm_func.__get__(class_, type(class_))
+        return self.im_func.__get__(inst, class_)
+
 class DB(object):
     """The Object Database
     -------------------
@@ -755,6 +769,14 @@
         finally:
             self._r()
 
+    def class_open(class_, *args, **kw):
+        db = class_(*args, **kw)
+        conn = db.open()
+        conn.onCloseCallback(db.close)
+        return conn
+
+    open = Methods('open', open, class_open)
+
     def connectionDebugInfo(self):
         result = []
         t = time.time()

Modified: ZODB/trunk/src/ZODB/tests/testDB.py
===================================================================
--- ZODB/trunk/src/ZODB/tests/testDB.py	2009-04-24 22:37:28 UTC (rev 99479)
+++ ZODB/trunk/src/ZODB/tests/testDB.py	2009-04-24 22:37:50 UTC (rev 99480)
@@ -146,8 +146,44 @@
 
     """
 
+def passing_a_file_name_to_DB():
+    """You can pass a file-storage file name to DB.
 
+    (Also note that we can access DB in ZODB.)
+
+    >>> db = ZODB.DB('data.fs')
+    >>> db.storage # doctest: +ELLIPSIS
+    <ZODB.FileStorage.FileStorage.FileStorage object at ...
+    >>> os.path.exists('data.fs')
+    True
+
+    >>> db.close()
+    """
+
+def open_convenience():
+    """Often, we just want to open a single connection.
+
+    >>> conn = ZODB.DB.open('data.fs')
+    >>> conn.root()
+    {}
+
+    >>> conn.root()['x'] = 1
+    >>> transaction.commit()
+    >>> conn.close()
+
+    Let's make sure the database was cloased when we closed the
+    connection, and that the data is there.
+
+    >>> db = ZODB.DB('data.fs')
+    >>> conn = db.open()
+    >>> conn.root()
+    {'x': 1}
+    >>> db.close()
+    """
+
 def test_suite():
     s = unittest.makeSuite(DBTests)
-    s.addTest(doctest.DocTestSuite())
+    s.addTest(doctest.DocTestSuite(
+        setUp=ZODB.tests.util.setUp, tearDown=ZODB.tests.util.tearDown,
+        ))
     return s



More information about the Checkins mailing list