]> git.ipfire.org Git - thirdparty/openembedded/openembedded-core-contrib.git/commitdiff
persist_data: make SQLTable a context manager
authorChris Larson <chris_larson@mentor.com>
Thu, 10 Feb 2011 00:57:03 +0000 (17:57 -0700)
committerChris Larson <chris_larson@mentor.com>
Wed, 6 Apr 2011 00:21:48 +0000 (17:21 -0700)
This can be used for more control over the underlying transactions.  Unlike
the context manager of, say, a file object, we can still use the object even
after the end of a given with block, as the context manager exit only ensures
we've committed to the database, not that we have closed the database.

Signed-off-by: Chris Larson <chris_larson@mentor.com>
lib/bb/persist_data.py

index aa2ede4b937c79ec763fc915d29ed715bfe91b30..af96b76f401fa88ccf45ad34281a8995ea88853c 100644 (file)
@@ -55,6 +55,13 @@ class SQLTable(collections.MutableMapping):
     def _execute(self, *query):
         return self.cursor.execute(*query)
 
+    def __enter__(self):
+        self.cursor.__enter__()
+        return self
+
+    def __exit__(self, *excinfo):
+        self.cursor.__exit__(*excinfo)
+
     def __getitem__(self, key):
         data = self._execute("SELECT * from %s where key=?;" %
                              self.table, [key])