python/pyosaf/utils/immom/ccb.py |  25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)


Make the Ccb class work with the with statement in Python. The CCB will
be automatically applied when the with statement is exited successfully and
aborted if an exception is raised before during the with statement.

Verify by using Ccb in with:

with Ccb() as ccb:
    ccb.create(...)
    ccb.delete(...)

    # Apply is automatically done when with is exited

diff --git a/python/pyosaf/utils/immom/ccb.py b/python/pyosaf/utils/immom/ccb.py
--- a/python/pyosaf/utils/immom/ccb.py
+++ b/python/pyosaf/utils/immom/ccb.py
@@ -114,6 +114,29 @@ class Ccb(object):
             print "saImmOmCcbInitialize: %s" % eSaAisErrorT.whatis(err)
             raise SafException(err)
 
+    def __enter__(self):
+        ''' Called when Ccb is used in a with statement:
+        
+            with Ccb() as ccb:
+                ...
+
+            The call is invoked before any code within is run
+        '''
+        return self
+
+    def __exit__(self, type, value, traceback):
+        ''' Called when Ccb is used in a with statement, 
+            just before it exits 
+
+            type, value and traceback are only set if the with
+            statement was left via an exception
+        '''
+        if type or value or traceback:
+            self.__del__()
+        else:
+            self.apply()
+            self.__del__()
+
     def __del__(self):
         one_sec_sleeps = 0
         error = saImmOm.saImmOmAdminOwnerFinalize(self.owner_handle)
@@ -139,6 +162,7 @@ class Ccb(object):
                 msg = "saImmOmAdminOwnerSet: %s - obj:%s" % \
                     (_parent_name, eSaAisErrorT.whatis(err))
                 raise SafException(err, msg)
+
         else:
             parent_name = None
 
@@ -345,6 +369,7 @@ class Ccb(object):
         if err != eSaAisErrorT.SA_AIS_OK:
             raise SafException(err)
 
+
 def test():
     ccb = Ccb()
     ccb.modify("safAmfCluster=myAmfCluster", "saAmfClusterStartupTimeout",

------------------------------------------------------------------------------
_______________________________________________
Opensaf-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensaf-devel

Reply via email to