home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / python-apt / examples / progress.py < prev    next >
Encoding:
Python Source  |  2006-03-02  |  2.4 KB  |  90 lines

  1. import apt
  2. from apt import SizeToStr
  3. import sys
  4. import time
  5. import string
  6.  
  7. class TextProgress(apt.OpProgress):
  8.     def __init__(self):
  9.         self.last=0.0
  10.  
  11.     def update(self, percent):
  12.         if (self.last + 1.0) <= percent:
  13.             sys.stdout.write("\rProgress: %i.2          " % (percent))
  14.             self.last = percent
  15.         if percent >= 100:
  16.             self.last = 0.0
  17.  
  18.     def done(self):
  19.         self.last = 0.0
  20.         print "\rDone                      "
  21.  
  22.  
  23. class TextFetchProgress(apt.FetchProgress):
  24.     def __init__(self):
  25.         pass
  26.     
  27.     def start(self):
  28.         pass
  29.     
  30.     def stop(self):
  31.         pass
  32.     
  33.     def updateStatus(self, uri, descr, shortDescr, status):
  34.         print "UpdateStatus: '%s' '%s' '%s' '%i'" % (uri,descr,shortDescr, status)
  35.     def pulse(self):
  36.         print "Pulse: CPS: %s/s; Bytes: %s/%s; Item: %s/%s" % (SizeToStr(self.currentCPS), SizeToStr(self.currentBytes), SizeToStr(self.totalBytes), self.currentItems, self.totalItems)
  37.         return True
  38.  
  39.     def mediaChange(self, medium, drive):
  40.     print "Please insert medium %s in drive %s" % (medium, drive)
  41.     sys.stdin.readline()
  42.         #return False
  43.  
  44.  
  45. class TextInstallProgress(apt.InstallProgress):
  46.     def __init__(self):
  47.         apt.InstallProgress.__init__(self)
  48.         pass
  49.     def startUpdate(self):
  50.         print "StartUpdate"
  51.     def finishUpdate(self):
  52.         print "FinishUpdate"
  53.     def statusChange(self, pkg, percent, status):
  54.         print "[%s] %s: %s" % (percent, pkg, status)
  55.     def updateInterface(self):
  56.         apt.InstallProgress.updateInterface(self)
  57.         # usefull to e.g. redraw a GUI
  58.         time.sleep(0.1)
  59.  
  60.  
  61. class TextCdromProgress(apt.CdromProgress):
  62.     def __init__(self):
  63.         pass
  64.     # update is called regularly so that the gui can be redrawn
  65.     def update(self, text, step):
  66.         # check if we actually have some text to display
  67.         if text != "":
  68.             print "Update: %s %s" % (string.strip(text), step)
  69.     def askCdromName(self):
  70.         print "Please enter cd-name: ",
  71.         cd_name = sys.stdin.readline()
  72.         return (True, string.strip(cd_name))
  73.     def changeCdrom(self):
  74.         print "Please insert cdrom and press <ENTER>"
  75.         answer =  sys.stdin.readline()
  76.         return True
  77.  
  78.  
  79. if __name__ == "__main__":
  80.     c = apt.Cache()
  81.     pkg = c["3dchess"]
  82.     if pkg.isInstalled:
  83.         pkg.markDelete()
  84.     else:
  85.         pkg.markInstall()
  86.  
  87.     res = c.commit(TextFetchProgress(), TextInstallProgress())
  88.  
  89.     print res
  90.