From: Chris Larson Date: Thu, 10 Feb 2011 00:57:03 +0000 (-0700) Subject: persist_data: make SQLTable a context manager X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d6fba8bad6277eb2a19e94f93a7a4baf74b29ef3;p=thirdparty%2Fopenembedded%2Fopenembedded-core-contrib.git persist_data: make SQLTable a context manager 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 --- diff --git a/lib/bb/persist_data.py b/lib/bb/persist_data.py index aa2ede4b937..af96b76f401 100644 --- a/lib/bb/persist_data.py +++ b/lib/bb/persist_data.py @@ -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])