[Yum-devel] [UG][PATCH] text field

Terje Rosten terje.rosten at ntnu.no
Tue Jan 11 18:32:00 UTC 2005


This patch let urlgrabber.urlgrab() pass a alternativ text to the
progress bar, which is used in the front of the bar.

Example: 

 myug = URLGrabber(progress_obj=TextMeter(fo=sys.stdout))
 remote = "http://web/rpms/xorg-x11-sdk-6.8.1-12.FC3.21.i386.rpm"
 text = "[1/3]: "
 local= "test1.rpm"
 result = myug.urlgrab(remote, local, text=text + os.path.basename(remote))

Gives:

 [1/3]: xorg-x11-sdk-6.8.1 100% |=========================| 7.7 MB    00:01

I want to use this feature in a coming patch to Yum.


 - Terje


diff -uNr urlgrabber.orig/urlgrabber/grabber.py urlgrabber/urlgrabber/grabber.py
--- urlgrabber.orig/urlgrabber/grabber.py	2004-10-17 03:53:13.000000000 +0200
+++ urlgrabber/urlgrabber/grabber.py	2005-01-11 16:33:26.585123753 +0100
@@ -48,11 +48,16 @@
   progress_obj = None
 
     a class instance that supports the following methods:
-      po.start(filename, url, basename, length)
+      po.start(filename, url, basename, length, text)
       # length will be None if unknown
       po.update(read) # read == bytes read so far
       po.end()
 
+  text = None
+  
+    specifies an alternativ text item in the beginning of the progress
+    bar line. If not given, the basename of the file is used.
+
   throttle = 1.0
 
     a number - if it's an int, it's the bytes/second throttle limit.
@@ -506,6 +511,7 @@
         self.opener = None
         self.cache_openers = True
         self.timeout = None
+        self.text = None
  
 class URLGrabber:
     """Provides easy opening of URLs with a variety of options.
@@ -778,7 +784,8 @@
             except: length = None
             self.opts.progress_obj.start(str(self.filename), self.url, 
                                          os.path.basename(path), 
-                                         length)
+                                         length,
+                                         text=self.opts.text)
             self.opts.progress_obj.update(0)
         (self.fo, self.hdr) = (fo, hdr)
     
diff -uNr urlgrabber.orig/urlgrabber/progress.py urlgrabber/urlgrabber/progress.py
--- urlgrabber.orig/urlgrabber/progress.py	2004-09-07 23:19:54.000000000 +0200
+++ urlgrabber/urlgrabber/progress.py	2005-01-11 16:33:26.595121920 +0100
@@ -31,6 +31,7 @@
         self.filename   = None
         self.url        = None
         self.basename   = None
+        self.text       = None
         self.size       = None
         self.start_time = None
         self.last_amount_read = 0
@@ -38,10 +39,11 @@
         self.re = RateEstimator()
         
     def start(self, filename=None, url=None, basename=None,
-              size=None, now=None):
+              size=None, now=None, text=None):
         self.filename = filename
         self.url      = url
         self.basename = basename
+        self.text     = text
 
         #size = None #########  TESTING
         self.size = size
@@ -91,29 +93,39 @@
         fetime = format_time(etime)
         fread = format_number(amount_read)
         #self.size = None
+        if self.text is not None:
+            text = self.text
+        else:
+            text = self.basename
         if self.size is None:
             out = '\r%-60.60s    %5sB %s ' % \
-                  (self.basename, fread, fetime)
+                  (text, fread, fetime)
         else:
             rtime = self.re.remaining_time()
             frtime = format_time(rtime)
             frac = self.re.fraction_read()
             bar = '='*int(25 * frac)
+
             out = '\r%-25.25s %3i%% |%-25.25s| %5sB %8s ETA ' % \
-                  (self.basename, frac*100, bar, fread, frtime)
+                  (text, frac*100, bar, fread, frtime)
+
         self.fo.write(out)
         self.fo.flush()
 
     def _do_end(self, amount_read, now=None):
         total_time = format_time(self.re.elapsed_time())
         total_size = format_number(amount_read)
+        if self.text is not None:
+            text = self.text
+        else:
+            text = self.basename
         if self.size is None:
             out = '\r%-60.60s    %5sB %s ' % \
-                  (self.basename, total_size, total_time)
+                  (text, total_size, total_time)
         else:
             bar = '='*25
             out = '\r%-25.25s %3i%% |%-25.25s| %5sB %8s     ' % \
-                  (self.basename, 100, bar, total_size, total_time)
+                  (text, 100, bar, total_size, total_time)
         self.fo.write(out + '\n')
         self.fo.flush()



More information about the Yum-devel mailing list