summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apt/progress/base.py164
-rw-r--r--apt/progress/text.py39
2 files changed, 183 insertions, 20 deletions
diff --git a/apt/progress/base.py b/apt/progress/base.py
new file mode 100644
index 00000000..fc5a8107
--- /dev/null
+++ b/apt/progress/base.py
@@ -0,0 +1,164 @@
+# apt/progress/base.py - Base classes for progress reporting.
+#
+# Copyright (C) 2009 Julian Andres Klode <jak@debian.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+# USA
+"""Base classes for progress reporting.
+
+Custom progress classes should inherit from these classes. They can also be
+used as dummy progress classes which simply do nothing.
+"""
+
+
+class AcquireProgress(object):
+ """Monitor object for downloads controlled by the Acquire class.
+
+ This is an mostly abstract class. You should subclass it and implement the
+ methods to get something useful.
+ """
+
+ current_bytes = current_cps = fetched_bytes = last_bytes = total_bytes \
+ = 0.0
+ current_items = elapsed_time = total_items = 0
+
+ def done(self, item):
+ """Invoked when an item is successfully and completely fetched."""
+
+ def fail(self, item):
+ """Invoked when an item could not be fetched."""
+
+ def fetch(self, item):
+ """Invoked when some of the item's data is fetched."""
+
+ def ims_hit(self, item):
+ """Invoked when an item is confirmed to be up-to-date.
+
+ Invoked when an item is confirmed to be up-to-date. For instance,
+ when an HTTP download is informed that the file on the server was
+ not modified.
+ """
+
+ def media_change(self, media, drive):
+ """Prompt the user to change the inserted removable media.
+
+ The parameter 'media' decribes the name of the the media type that
+ should be changed, whereas the parameter 'drive' should be the
+ identifying name of the drive whose media should be changed.
+
+ This method should not return until the user has confirmed to the user
+ interface that the media change is complete. It must return True if
+ the user confirms the media change, or False to cancel it.
+ """
+ return False
+
+ def pulse(self, owner):
+ """Periodically invoked while the Acquire process is underway.
+
+ This method gets invoked while the Acquire progress given by the
+ parameter 'owner' is underway. It should display information about
+ the current state.
+
+ This function returns a boolean value indicating whether the
+ acquisition should be continued (True) or cancelled (False).
+ """
+ return True
+
+ def start(self):
+ """Invoked when the Acquire process starts running."""
+ # Reset all our values.
+ self.current_bytes = 0.0
+ self.current_cps = 0.0
+ self.current_items = 0
+ self.elapsed_time = 0
+ self.fetched_bytes = 0.0
+ self.last_bytes = 0.0
+ self.total_bytes = 0.0
+ self.total_items = 0
+
+ def stop(self):
+ """Invoked when the Acquire process stops running."""
+
+
+class CdromProgress(object):
+ """Base class for reporting the progress of adding a cdrom.
+
+ Can be used with apt_pkg.Cdrom to produce an utility like apt-cdrom. The
+ attribute 'total_steps' defines the total number of steps and can be used
+ in update() to display the current progress.
+ """
+
+ total_steps = 0
+
+ def ask_cdrom_name(self):
+ """Ask for the name of the cdrom.
+
+ If a name has been provided, return it. Otherwise, return None to
+ cancel the operation.
+ """
+
+ def change_cdrom(self):
+ """Ask for the CD-ROM to be changed.
+
+ Return True once the cdrom has been changed or False to cancel the
+ operation.
+ """
+
+ def update(self, text, current):
+ """Periodically invoked to update the interface.
+
+ The string 'text' defines the text which should be displayed. The
+ integer 'current' defines the number of completed steps.
+ """
+
+
+class InstallProgress(object):
+ """Report the install progress.
+
+ Subclass this class to implement install progress reporting.
+ """
+
+ def start_update(self):
+ """Start update."""
+
+ def run(self, pm):
+ """Start installation."""
+ return pm.do_install()
+
+ def finish_update(self):
+ """Called when update has finished."""
+
+ def update_interface(self):
+ """Called periodically to update the user interface."""
+
+
+class OpProgress(object):
+ """Monitor objects for operations.
+
+ Display the progress of operations such as opening the cache."""
+
+ major_change, op, percent, subop = False, "", 0.0, ""
+
+ def update(self, percent=None):
+ """Called periodically to update the user interface.
+
+ You may use the optional argument 'percent' to set the attribute
+ 'percent' in this call.
+ """
+ if percent is not None:
+ self.percent = percent
+
+ def done(self):
+ """Called once an operation has been completed."""
diff --git a/apt/progress/text.py b/apt/progress/text.py
index c9e2882b..54a35704 100644
--- a/apt/progress/text.py
+++ b/apt/progress/text.py
@@ -18,8 +18,9 @@
import sys
import apt_pkg
+import apt.progress.base
-__all__ = ['AcquireProgress', 'OpProgress']
+__all__ = ['AcquireProgress', 'CdromProgress', 'OpProgress']
def _(msg):
@@ -54,7 +55,7 @@ class TextProgress(object):
self._file.flush()
-class OpProgress(apt_pkg.OpProgress, TextProgress):
+class OpProgress(apt.progress.base.OpProgress, TextProgress):
"""Operation progress reporting.
This closely resembles OpTextProgress in libapt-pkg.
@@ -62,14 +63,12 @@ class OpProgress(apt_pkg.OpProgress, TextProgress):
def __init__(self, outfile=None):
TextProgress.__init__(self, outfile)
- apt_pkg.OpProgress.__init__(self)
+ apt.progress.base.OpProgress.__init__(self)
self.old_op = ""
def update(self, percent=None):
"""Called periodically to update the user interface."""
- if percent:
- self.percent = percent
- apt_pkg.OpProgress.update(self)
+ apt.progress.base.OpProgress.update(self, percent)
if self.major_change and self.old_op:
self._write(self.old_op)
self._write("%s... %i%%\r" % (self.op, self.percent), False, True)
@@ -77,18 +76,18 @@ class OpProgress(apt_pkg.OpProgress, TextProgress):
def done(self):
"""Called once an operation has been completed."""
- apt_pkg.OpProgress.done(self)
+ apt.progress.base.OpProgress.done(self)
if self.old_op:
self._write(_("%c%s... Done") % ('\r', self.old_op), True, True)
self.old_op = ""
-class AcquireProgress(apt_pkg.AcquireProgress, TextProgress):
+class AcquireProgress(apt.progress.base.AcquireProgress, TextProgress):
"""AcquireProgress for the text interface."""
def __init__(self, outfile=None):
TextProgress.__init__(self, outfile)
- apt_pkg.AcquireProgress.__init__(self)
+ apt.progress.base.AcquireProgress.__init__(self)
self._signal = None
self._width = 80
self._id = 1
@@ -99,7 +98,7 @@ class AcquireProgress(apt_pkg.AcquireProgress, TextProgress):
In this case, the function sets up a signal handler for SIGWINCH, i.e.
window resize signals. And it also sets id to 1.
"""
- apt_pkg.AcquireProgress.start(self)
+ apt.progress.base.AcquireProgress.start(self)
import signal
self._signal = signal.signal(signal.SIGWINCH, self._winch)
# Get the window size.
@@ -117,7 +116,7 @@ class AcquireProgress(apt_pkg.AcquireProgress, TextProgress):
def ims_hit(self, item):
"""Called when an item is update (e.g. not modified on the server)."""
- apt_pkg.AcquireProgress.ims_hit(self, item)
+ apt.progress.base.AcquireProgress.ims_hit(self, item)
line = _('Hit ') + item.description
if item.owner.filesize:
line += ' [%sB]' % apt_pkg.size_to_str(item.owner.filesize)
@@ -125,7 +124,7 @@ class AcquireProgress(apt_pkg.AcquireProgress, TextProgress):
def fail(self, item):
"""Called when an item is failed."""
- apt_pkg.AcquireProgress.fail(self, item)
+ apt.progress.base.AcquireProgress.fail(self, item)
if item.owner.status == item.owner.stat_done:
self._write(_("Ign ") + item.description)
else:
@@ -134,7 +133,7 @@ class AcquireProgress(apt_pkg.AcquireProgress, TextProgress):
def fetch(self, item):
"""Called when some of the item's data is fetched."""
- apt_pkg.AcquireProgress.fetch(self, item)
+ apt.progress.base.AcquireProgress.fetch(self, item)
# It's complete already (e.g. Hit)
if item.owner.complete:
return
@@ -150,7 +149,7 @@ class AcquireProgress(apt_pkg.AcquireProgress, TextProgress):
"""Periodically invoked while the Acquire process is underway.
Return False if the user asked to cancel the whole Acquire process."""
- apt_pkg.AcquireProgress.pulse(self, owner)
+ apt.progress.base.AcquireProgress.pulse(self, owner)
percent = (((self.current_bytes + self.current_items) * 100.0) /
float(self.total_bytes + self.total_items))
@@ -210,7 +209,7 @@ class AcquireProgress(apt_pkg.AcquireProgress, TextProgress):
def media_change(self, medium, drive):
"""Prompt the user to change the inserted removable media."""
- apt_pkg.AcquireProgress.media_change(self, medium, drive)
+ apt.progress.base.AcquireProgress.media_change(self, medium, drive)
self._write(_("Media change: please insert the disc labeled\n"
" '%s'\n"
"in the drive '%s' and press enter\n") % (medium, drive))
@@ -218,7 +217,7 @@ class AcquireProgress(apt_pkg.AcquireProgress, TextProgress):
def stop(self):
"""Invoked when the Acquire process stops running."""
- apt_pkg.AcquireProgress.stop(self)
+ apt.progress.base.AcquireProgress.stop(self)
# Trick for getting a translation from apt
self._write((_("Fetched %sB in %s (%sB/s)\n") % (
apt_pkg.size_to_str(self.fetched_bytes),
@@ -230,12 +229,12 @@ class AcquireProgress(apt_pkg.AcquireProgress, TextProgress):
signal.signal(signal.SIGWINCH, self._signal)
-class CdromProgress(apt_pkg.CdromProgress, TextProgress):
+class CdromProgress(apt.progress.base.CdromProgress, TextProgress):
"""Text CD-ROM progress."""
def ask_cdrom_name(self):
"""Ask the user to provide a name for the disc."""
- apt_pkg.CdromProgress.ask_cdrom_name(self)
+ apt.progress.base.CdromProgress.ask_cdrom_name(self)
self._write(_("Please provide a name for this Disc, such as "
"'Debian 2.1r1 Disk 1'"), False)
try:
@@ -245,13 +244,13 @@ class CdromProgress(apt_pkg.CdromProgress, TextProgress):
def update(self, text, current):
"""Set the current progress."""
- apt_pkg.CdromProgress.update(self, text, current)
+ apt.progress.base.CdromProgress.update(self, text, current)
if text:
self._write(text, False)
def change_cdrom(self):
"""Ask the user to change the CD-ROM."""
- apt_pkg.CdromProgress.change_cdrom(self)
+ apt.progress.base.CdromProgress.change_cdrom(self)
self._write(_("Please insert a Disc in the drive and press enter"),
False)
try: