urllib2.urlopen Progress bar

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ritesh Raj Sarraf

    #1

    urllib2.urlopen Progress bar

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hi,

    In urllib.urlretri eve I can use reporthook to implement a progress bar.

    But in urllib2.urlopen I can't.
    I have to use urllib2.urlopen because it throws exceptions which are
    important to be handled where as urlretrieve is dumb.

    Is there a way to implement urllib2.urlopen to enable reporthook ?

    progress.py code:

    #!/usr/bin/env python

    import time, urllib2, urllib

    class progressBar:
    def __init__(self, minValue = 0, maxValue = 10, totalWidth=12):
    self.progBar = "[]" # This holds the progress bar string
    self.min = minValue
    self.max = maxValue
    self.span = maxValue - minValue
    self.width = totalWidth
    self.amount = 0 # When amount == max, we are 100% done
    self.updateAmou nt(0) # Build progress bar string

    def updateAmount(se lf, newAmount = 0):
    if newAmount < self.min: newAmount = self.min
    if newAmount > self.max: newAmount = self.max
    self.amount = newAmount

    # Figure out the new percent done, round to an integer
    diffFromMin = float(self.amou nt - self.min)
    percentDone = (diffFromMin / float(self.span )) * 100.0
    percentDone = round(percentDo ne)
    percentDone = int(percentDone )

    # Figure out how many hash bars the percentage should be
    allFull = self.width - 2
    numHashes = (percentDone / 100.0) * allFull
    numHashes = int(round(numHa shes))

    # build a progress bar with hashes and spaces
    self.progBar = "[" + '#'*numHashes + ' '*(allFull-numHashes) + "]"

    # figure out where to put the percentage, roughly centered
    percentPlace = (len(self.progB ar) / 2) - len(str(percent Done))
    percentString = str(percentDone ) + "%"

    # slice the percentage into the bar
    #self.progBar = self.progBar[0:percentPlace] + percentString +
    self.progBar[percentPlace+le n(percentString ):]
    self.progBar = self.progBar[0:percentPlace] +
    self.progBar[percentPlace+le n(percentString ):] + percentString

    def __str__(self):
    return str(self.progBa r)

    def myReportHook(co unt, blockSize, totalSize):
    import sys
    global prog

    if prog == "":
    prog = progressBar(0,t otalSize,50)
    #print count, blockSize, totalSize
    #prog = progressBar(0, totalSize, 77)
    prog.updateAmou nt(count*blockS ize)
    sys.stdout.writ e (str(prog))
    sys.stdout.writ e ("\r")
    #print count * (blockSize/1024) , "kb of " , (totalSize/1024) , "kb
    downloaded.\n"
    prog = ""
    sFile = "new.rpm"
    sUrl="http://localhost/new.rpm"
    urllib.urlretri eve(sUrl, sFile, reporthook=myRe portHook)
    print "\n\n"
    #temp = urllib2.urlopen (sUrl)
    #lastval = int(temp.header s['Content-Length'])
    #prog = progressBar(0, lastval, 77)
    #
    #for x in range(101):
    # prog.updateAmou nt(x)
    # print prog, "\r", time.sleep(0.5)
    #
    #data = open(sFile,'wb' )
    #data.write(tem p.read())
    #data.close()
    #temp.close()


    Regards,
    Ritesh
    - --
    Ritesh Raj Sarraf
    RESEARCHUT -- http://www.researchut.com
    Gnupg Key ID: 04F130BC
    "Stealing logic from one person is plagiarism, stealing from many is
    research."
    "Necessity is the mother of invention."
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)

    iD8DBQFDyfkM4Rh i6gTxMLwRAlmMAJ 9yePv6DvXCcnUb0 DYhWSuIonciSACg rxDj
    aKSGkfg8I1T/q3r6zWLTXmw=
    =uomB
    -----END PGP SIGNATURE-----

Working...