Skip to content

Commit

Permalink
Merge pull request #108 from vojtechtrefny/progress_callbacks
Browse files Browse the repository at this point in the history
Add progress report callback for action processing
  • Loading branch information
vojtechtrefny committed May 12, 2015
2 parents bc90c21 + 0ebf211 commit bf8e88c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
11 changes: 8 additions & 3 deletions blivet/callbacks.py
Expand Up @@ -35,13 +35,15 @@
"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
Expand All @@ -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"])
Expand All @@ -73,3 +76,5 @@ def create_new_callbacks_register(create_format_pre=None,
["msg"])
WaitForEntropyData = namedtuple("WaitForEntropyData",
["msg", "min_entropy"])
ReportProgressData = namedtuple("ReportProgressData",
["msg"])
22 changes: 13 additions & 9 deletions blivet/deviceaction.py
Expand Up @@ -32,7 +32,7 @@
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
Expand Down Expand Up @@ -168,6 +168,10 @@ def execute(self, callbacks=None):
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
Expand Down Expand Up @@ -298,7 +302,7 @@ def __init__(self, device):
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):
Expand Down Expand Up @@ -341,7 +345,7 @@ def __init__(self, device):
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):
Expand Down Expand Up @@ -444,7 +448,7 @@ def apply(self):
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):
Expand Down Expand Up @@ -525,7 +529,7 @@ def apply(self):
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))
Expand Down Expand Up @@ -647,7 +651,7 @@ def apply(self):

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()
Expand Down Expand Up @@ -750,7 +754,7 @@ def apply(self):
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))
Expand Down Expand Up @@ -823,7 +827,7 @@ def cancel(self):
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):
Expand Down Expand Up @@ -887,7 +891,7 @@ def cancel(self):
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):
Expand Down

0 comments on commit bf8e88c

Please sign in to comment.