From 0ebf2110be99836069f4d57fdd33568238fd0cef Mon Sep 17 00:00:00 2001
From: Vojtech Trefny <vtrefny@redhat.com>
Date: Thu, 7 May 2015 09:12:32 +0200
Subject: [PATCH] Add progress report callback for action processing

Signed-off-by: Vojtech Trefny <vtrefny@redhat.com>
---
 blivet/callbacks.py    | 11 ++++++++---
 blivet/deviceaction.py | 22 +++++++++++++---------
 2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/blivet/callbacks.py b/blivet/callbacks.py
index 742b768..db52dc0 100644
--- a/blivet/callbacks.py
+++ b/blivet/callbacks.py
@@ -35,13 +35,15 @@ _CallbacksRegister = namedtuple("_CallbacksRegister",
                                  "create_format_post",
                                  "resize_format_pre",
                                  "resize_format_post",
-                                 "wait_for_entropy"])
+                                 "wait_for_entropy",
+                                 "report_progress"])
 
 def create_new_callbacks_register(create_format_pre=None,
                                   create_format_post=None,
                                   resize_format_pre=None,
                                   resize_format_post=None,
-                                  wait_for_entropy=None):
+                                  wait_for_entropy=None,
+                                  report_progress=None):
     """
     A function for creating a new opaque object holding the references to
     callbacks. The point of this function is to hide the implementation of such
@@ -56,12 +58,13 @@ def create_new_callbacks_register(create_format_pre=None,
                              value indicates whether continuing regardless of
                              available entropy should be forced (True) or not (False)
     :type wait_for_entropy: :class:`.WaitForEntropyData` -> bool
+    :type report_progress: :class:`.ReportProgressData` -> NoneType
 
     """
 
     return _CallbacksRegister(create_format_pre, create_format_post,
                               resize_format_pre, resize_format_post,
-                              wait_for_entropy)
+                              wait_for_entropy, report_progress)
 
 CreateFormatPreData = namedtuple("CreateFormatPreData",
                                  ["msg"])
@@ -73,3 +76,5 @@ ResizeFormatPostData = namedtuple("ResizeFormatPostData",
                                   ["msg"])
 WaitForEntropyData = namedtuple("WaitForEntropyData",
                                 ["msg", "min_entropy"])
+ReportProgressData = namedtuple("ReportProgressData",
+                                 ["msg"])
diff --git a/blivet/deviceaction.py b/blivet/deviceaction.py
index 4d10765..d38f99c 100644
--- a/blivet/deviceaction.py
+++ b/blivet/deviceaction.py
@@ -32,7 +32,7 @@ from parted import partitionFlag, PARTITION_LBA
 from .i18n import _, N_
 from .callbacks import CreateFormatPreData, CreateFormatPostData
 from .callbacks import ResizeFormatPreData, ResizeFormatPostData
-from .callbacks import WaitForEntropyData
+from .callbacks import WaitForEntropyData, ReportProgressData
 from .size import Size
 
 import logging
@@ -168,6 +168,10 @@ class DeviceAction(util.ObjectID):
         if not self._applied:
             raise RuntimeError("cannot execute unapplied action")
 
+        if callbacks and callbacks.report_progress:
+            msg = _("Executing %(action)s") % {"action": str(self)}
+            callbacks.report_progress(ReportProgressData(msg))
+
     def cancel(self):
         """ cancel the action """
         self._applied = False
@@ -298,7 +302,7 @@ class ActionCreateDevice(DeviceAction):
         DeviceAction.__init__(self, device)
 
     def execute(self, callbacks=None):
-        super(ActionCreateDevice, self).execute(callbacks=None)
+        super(ActionCreateDevice, self).execute(callbacks=callbacks)
         self.device.create()
 
     def requires(self, action):
@@ -341,7 +345,7 @@ class ActionDestroyDevice(DeviceAction):
         DeviceAction.__init__(self, device)
 
     def execute(self, callbacks=None):
-        super(ActionDestroyDevice, self).execute(callbacks=None)
+        super(ActionDestroyDevice, self).execute(callbacks=callbacks)
         self.device.destroy()
 
     def requires(self, action):
@@ -444,7 +448,7 @@ class ActionResizeDevice(DeviceAction):
         super(ActionResizeDevice, self).apply()
 
     def execute(self, callbacks=None):
-        super(ActionResizeDevice, self).execute(callbacks=None)
+        super(ActionResizeDevice, self).execute(callbacks=callbacks)
         self.device.resize()
 
     def cancel(self):
@@ -525,7 +529,7 @@ class ActionCreateFormat(DeviceAction):
         super(ActionCreateFormat, self).apply()
 
     def execute(self, callbacks=None):
-        super(ActionCreateFormat, self).execute(callbacks=None)
+        super(ActionCreateFormat, self).execute(callbacks=callbacks)
         if callbacks and callbacks.create_format_pre:
             msg = _("Creating %(type)s on %(device)s") % {"type": self.device.format.type, "device": self.device.path}
             callbacks.create_format_pre(CreateFormatPreData(msg))
@@ -647,7 +651,7 @@ class ActionDestroyFormat(DeviceAction):
 
     def execute(self, callbacks=None):
         """ wipe the filesystem signature from the device """
-        super(ActionDestroyFormat, self).execute(callbacks=None)
+        super(ActionDestroyFormat, self).execute(callbacks=callbacks)
         status = self.device.status
         self.device.setup(orig=True)
         self.format.destroy()
@@ -750,7 +754,7 @@ class ActionResizeFormat(DeviceAction):
         super(ActionResizeFormat, self).apply()
 
     def execute(self, callbacks=None):
-        super(ActionResizeFormat, self).execute(callbacks=None)
+        super(ActionResizeFormat, self).execute(callbacks=callbacks)
         if callbacks and callbacks.resize_format_pre:
             msg = _("Resizing filesystem on %(device)s") % {"device": self.device.path}
             callbacks.resize_format_pre(ResizeFormatPreData(msg))
@@ -823,7 +827,7 @@ class ActionAddMember(DeviceAction):
         super(ActionAddMember, self).cancel()
 
     def execute(self, callbacks=None):
-        super(ActionAddMember, self).execute(callbacks=None)
+        super(ActionAddMember, self).execute(callbacks=callbacks)
         self.container.add(self.device)
 
     def requires(self, action):
@@ -887,7 +891,7 @@ class ActionRemoveMember(DeviceAction):
         super(ActionRemoveMember, self).cancel()
 
     def execute(self, callbacks=None):
-        super(ActionRemoveMember, self).execute(callbacks=None)
+        super(ActionRemoveMember, self).execute(callbacks=callbacks)
         self.container.remove(self.device)
 
     def requires(self, action):
-- 
2.1.0

