[Zodb-checkins] SVN: ZODB/trunk/src/ As a convenience, the connection root method for returning teh root

Jim Fulton jim at zope.com
Tue Apr 28 11:28:50 EDT 2009


Log message for revision 99560:
  As a convenience, the connection root method for returning teh root
  object can now *also* be used as an object with attributes mapped to
  the root-object keys.
  

Changed:
  U   ZODB/trunk/src/CHANGES.txt
  U   ZODB/trunk/src/ZODB/Connection.py
  U   ZODB/trunk/src/ZODB/tests/testConnection.py
  U   ZODB/trunk/src/ZODB/tests/testDB.py

-=-
Modified: ZODB/trunk/src/CHANGES.txt
===================================================================
--- ZODB/trunk/src/CHANGES.txt	2009-04-28 14:32:13 UTC (rev 99559)
+++ ZODB/trunk/src/CHANGES.txt	2009-04-28 15:28:49 UTC (rev 99560)
@@ -14,6 +14,10 @@
 New Features
 ------------
 
+- As a convenience, the connection root method for returning teh root
+  object can now *also* be used as an object with attributes mapped to
+  the root-object keys.
+
 - Databases have a new method, transaction, that can be used with the
   Python (2.5 and later) with statement::
 

Modified: ZODB/trunk/src/ZODB/Connection.py
===================================================================
--- ZODB/trunk/src/ZODB/Connection.py	2009-04-28 14:32:13 UTC (rev 99559)
+++ ZODB/trunk/src/ZODB/Connection.py	2009-04-28 15:28:49 UTC (rev 99560)
@@ -356,9 +356,10 @@
         finally:
             self._inv_lock.release()
 
+    @property
     def root(self):
         """Return the database root object."""
-        return self.get(z64)
+        return RootConvenience(self.get(z64))
 
     def get_connection(self, database_name):
         """Return a Connection for the named database."""
@@ -1333,3 +1334,32 @@
         # a copy of the index here.  An alternative would be to ensure that
         # all callers pass copies.  As is, our callers do not make copies.
         self.index = index.copy()
+
+class RootConvenience(object):
+
+    def __init__(self, root):
+        self.__dict__['_root'] = root
+
+    def __getattr__(self, name):
+        try:
+            return self._root[name]
+        except KeyError:
+            raise AttributeError(name)
+
+    def __setattr__(self, name, v):
+        self._root[name] = v
+
+    def __delattr__(self, name):
+        try:
+            del self._root[name]
+        except KeyError:
+            raise AttributeError(name)
+
+    def __call__(self):
+        return self._root
+
+    def __repr__(self):
+        names = " ".join(sorted(self._root))
+        if len(names) > 60:
+            names = names[:57].rsplit(' ', 1)[0] + ' ...'
+        return "<root: %s>" % names

Modified: ZODB/trunk/src/ZODB/tests/testConnection.py
===================================================================
--- ZODB/trunk/src/ZODB/tests/testConnection.py	2009-04-28 14:32:13 UTC (rev 99559)
+++ ZODB/trunk/src/ZODB/tests/testConnection.py	2009-04-28 15:28:49 UTC (rev 99560)
@@ -519,6 +519,43 @@
         >>> db.close()
     """
 
+def connection_root_convenience():
+    """Connection root attributes can now be used as objects with attributes
+
+    >>> db = ZODB.tests.util.DB()
+    >>> conn = db.open()
+    >>> conn.root.x
+    Traceback (most recent call last):
+    ...
+    AttributeError: x
+
+    >>> del conn.root.x
+    Traceback (most recent call last):
+    ...
+    AttributeError: x
+
+    >>> conn.root()['x'] = 1
+    >>> conn.root.x
+    1
+    >>> conn.root.y = 2
+    >>> sorted(conn.root().items())
+    [('x', 1), ('y', 2)]
+
+    >>> conn.root
+    <root: x y>
+
+    >>> del conn.root.x
+    >>> sorted(conn.root().items())
+    [('y', 2)]
+
+    >>> conn.root.rather_long_name = 1
+    >>> conn.root.rather_long_name2 = 1
+    >>> conn.root.rather_long_name4 = 1
+    >>> conn.root.rather_long_name5 = 1
+    >>> conn.root
+    <root: rather_long_name rather_long_name2 rather_long_name4 ...>
+    """
+
 class _PlayPersistent(Persistent):
     def setValueWithSize(self, size=0): self.value = size*' '
     __init__ = setValueWithSize

Modified: ZODB/trunk/src/ZODB/tests/testDB.py
===================================================================
--- ZODB/trunk/src/ZODB/tests/testDB.py	2009-04-28 14:32:13 UTC (rev 99559)
+++ ZODB/trunk/src/ZODB/tests/testDB.py	2009-04-28 15:28:49 UTC (rev 99560)
@@ -192,7 +192,7 @@
         and see that it isn't automatically committed or aborted as we use
         the transaction context manager.
 
-        >>> db = ZODB.DB('data.fs')
+        >>> db = ZODB.tests.util.DB()
         >>> conn = db.open()
         >>> conn.root()['x'] = conn.root().__class__()
         >>> transaction.commit()
@@ -236,6 +236,8 @@
         >>> conn3.root()['x']
         {'x': 1}
 
+
+        >>> db.close()
         """
 
 def test_suite():



More information about the Zodb-checkins mailing list