Author: dmeyer
Date: Fri Aug 31 14:34:55 2007
New Revision: 2790

Log:
move generic Status object to kaa.notifier.Progress

Modified:
   trunk/WIP/netsearch/src/feed/download.py
   trunk/base/src/notifier/__init__.py
   trunk/base/src/notifier/async.py

Modified: trunk/WIP/netsearch/src/feed/download.py
==============================================================================
--- trunk/WIP/netsearch/src/feed/download.py    (original)
+++ trunk/WIP/netsearch/src/feed/download.py    Fri Aug 31 14:34:55 2007
@@ -4,39 +4,6 @@
 import urllib2
 import kaa.notifier
 
-class Status(kaa.notifier.Signal):
-    """
-    Generic status object for InProgress
-    """
-    def __init__(self):
-        super(Status,self).__init__()
-        self.percent = 0
-        self.pos = 0
-        self.max = 0
-
-    def set(self, pos, max=None):
-        if max is not None:
-            self.max = max
-        self.pos = pos
-        if pos > self.max:
-            self.max = pos
-        if self.max:
-            self.percent = (self.pos * 100) / self.max
-        else:
-            self.percent = 0
-        self.emit()
-
-    def update(self, diff):
-        self.set(self.pos + diff)
-
-
-    def __str__(self):
-        n = 0
-        if self.max:
-            n = int((self.pos / float(self.max)) * 50)
-        return "|%51s| %d / %d" % (("="*n + ">").ljust(51), self.pos, self.max)
-
-
 def fetch_HTTP(url, filename):
     """
     Fetch HTTP URL.
@@ -64,7 +31,7 @@
         # stupid url encoding in url
         url = url[:8+url[8:].find('/')] + \
               urllib.quote(url[8+url[8:].find('/'):])
-    s = Status()
+    s = kaa.notifier.Progress()
     t = kaa.notifier.Thread(download, url, filename, s)
     t.wait_on_exit(False)
     async = t.start()

Modified: trunk/base/src/notifier/__init__.py
==============================================================================
--- trunk/base/src/notifier/__init__.py (original)
+++ trunk/base/src/notifier/__init__.py Fri Aug 31 14:34:55 2007
@@ -53,7 +53,7 @@
 from yieldfunc import *
 from jobserver import ThreadCallback, execute_in_thread
 from jobserver import killall as kill_jobserver
-from async import InProgress
+from async import Progress, InProgress
 
 from decorators import execute_in_timer, execute_in_mainloop
 

Modified: trunk/base/src/notifier/async.py
==============================================================================
--- trunk/base/src/notifier/async.py    (original)
+++ trunk/base/src/notifier/async.py    Fri Aug 31 14:34:55 2007
@@ -29,7 +29,7 @@
 #
 # -----------------------------------------------------------------------------
 
-__all__ = [ 'InProgress' ]
+__all__ = [ 'Progress', 'InProgress' ]
 
 # python imports
 import logging
@@ -42,6 +42,54 @@
 log = logging.getLogger('notifier.async')
 
 
+class Progress(Signal):
+    """
+    Generic progress status object for InProgress. This object can be connected
+    to an InProgress object with set_status and the caller can monitor the
+    progress. The caller can read the current position (pos) of the maximum
+    (max) or the percentage of both (percentage).
+    """
+    def __init__(self):
+        super(Progress, self).__init__()
+        self.percentage = 0
+        self.pos = 0
+        self.max = 0
+
+
+    def set(self, pos, max=None):
+        """
+        Set new status. The new status is pos of max.
+        """
+        if max is not None:
+            self.max = max
+        self.pos = pos
+        if pos > self.max:
+            self.max = pos
+        if self.max:
+            self.percentage = (self.pos * 100) / self.max
+        else:
+            self.percentage = 0
+        self.emit()
+
+
+    def update(self, diff):
+        """
+        Update position by the given difference.
+        """
+        self.set(self.pos + diff)
+
+
+    def get_progressbar(self, width=70):
+        """
+        Return a small ASCII art progressbar.
+        """
+        n = 0
+        if self.max:
+            n = int((self.pos / float(self.max)) * (width-3))
+        s = '|%%%ss|' % (width-2)
+        return s % ("="*n + ">").ljust(width-2)
+
+
 class InProgress(Signal):
     """
     An InProgress class used to return from function calls

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to