From 25fbd475e4f4f4cc3905f024f521da7fecf4339c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Michel=20De=20Rainville?= Date: Mon, 26 Jan 2015 08:17:28 -0500 Subject: Added context management to the Vimba object Allows clean shutdown when an exception is unhandled. --- pymba/vimba.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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. -- cgit v1.2.3