aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrançois-Michel De Rainville <f.derainville@gmail.com>2015-01-26 08:17:28 -0500
committerFrançois-Michel De Rainville <f.derainville@gmail.com>2015-01-26 08:17:28 -0500
commit25fbd475e4f4f4cc3905f024f521da7fecf4339c (patch)
tree39f7caa51b7cf073f38945df6fe5d22dfabea627
parent0697fc9a94755ed45e6bc8695c3a3f48d096cdaf (diff)
downloadpymba-25fbd475e4f4f4cc3905f024f521da7fecf4339c.tar.gz
pymba-25fbd475e4f4f4cc3905f024f521da7fecf4339c.zip
Added context management to the Vimba object
Allows clean shutdown when an exception is unhandled.
-rw-r--r--pymba/vimba.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/pymba/vimba.py b/pymba/vimba.py
index e301aa2..97eb96e 100644
--- a/pymba/vimba.py
+++ b/pymba/vimba.py
@@ -35,6 +35,27 @@ class Vimba(object):
# forget them
self._interfaces = {}
+ def __enter__(self):
+ """
+ Define vimba context for safe execution.
+
+ The vimba object should be used like this:
+ # start Vimba
+ with Vimba() as vimba:
+ system = vimba.getSystem()
+ # ...
+ """
+ self.startup()
+ return self
+
+ def __exit__(self, type, value, traceback):
+ """
+ Shutdown Vimba when the with context is left. This allows cleanup
+ when an error occurs in the main program. The system will not hang
+ on a kernel call after an exception.
+ """
+ self.shutdown()
+
def _getInterfaceInfos(self):
"""
Gets interface info of all available interfaces.