On Sun, Feb 04, 2007 at 08:51:22PM +0100, Magnus Wirstr?m wrote:
> Hi all
>
> I'm workinga on a program that will upload a large file to a server
> using ftp. I'm using ftplib to do this. I'm using a gui with wxpython
> and i would like to have a progressbar showing in % how much have been
> transfered. I have been googling but i can't make any sense of what i
> have found. does anyone have a good example or could explain how to do
> this ?
This code was originally given to me. I've adapted it some.
Your will have to modify it for wxPython. But, at least it gives
you some scaffolding to start with. Note the "fancy" flag and
variable, which is probably what you want, not too sure.
Hope this helps.
Dave
--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
#!/usr/bin/env python
import sys, os
import ftplib
import getopt
DEBUG = 1
PW_CLEAN = "put your password here"
PW = '%s\r' % PW_CLEAN
def pdebug(msg):
if DEBUG > 0:
print msg
PROGRESS_CHAR = '!'
class dot_FTP(ftplib.FTP):
def storbinary(self, cmd, fp, fancy, blocksize=8192):
''' Store a file in binary mode.'''
self.voidcmd('TYPE I')
conn = self.transfercmd(cmd)
if fancy:
bar = progressBar()
amount = 1
while 1:
buf = fp.read(blocksize)
if not buf: break
conn.send(buf)
sys.stdout.write(PROGRESS_CHAR)
sys.stdout.flush()
if fancy:
amount += 2
bar.updateAmount(amount)
print bar, "\r"
conn.close()
return self.voidresp()
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.updateAmount(0) # Build progress bar string
def updateAmount(self, 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.amount - self.min)
percentDone = (diffFromMin / float(self.span)) * 100.0
percentDone = round(percentDone)
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(numHashes))
# build a progress bar with hashes and spaces
self.progBar = "[" + PROGRESS_CHAR*numHashes + ' '*(allFull-numHashes) + "]"
# figure out where to put the percentage, roughly centered
percentPlace = (len(self.progBar) / 2) - len(str(percentDone))
percentString = str(percentDone) + "%"
# slice the percentage into the bar
self.progBar = self.progBar[0:percentPlace] + percentString + \
self.progBar[percentPlace+len(percentString):]
def __str__(self):
return str(self.progBar)
def upload(destDirectory, fileNames, fancy):
ftp = dot_FTP('cutter.rexx.com', 'dkuhlman', PW)
ftp.cwd(destDirectory)
ftp.set_pasv(not ftp.passiveserver)
wd = ftp.pwd()
print 'Working directory:', wd
reply = raw_input('Continue (y/n): ')
if reply == 'y':
for fileName in fileNames:
inFile = file(fileName, 'r')
s1 = 'STOR %s' % fileName
ftp.storbinary(s1, inFile, fancy)
print '\n%s' % s1
inFile.close()
pdebug('Closing connection')
ftp.quit()
USAGE_TEXT = """
Usage: python ftpupload.py [options] <dest_dir> <file> <file> ...
Options:
-f, --fancy Show facier progress bar.
"""
def usage():
print USAGE_TEXT
sys.exit(-1)
def main():
args = sys.argv[1:]
try:
opts, args = getopt.getopt(args, 'hf', ['help',
'fancy', ])
except:
usage()
fancy = False
for opt, val in opts:
if opt in ('-h', '--help'):
usage()
elif opt in ('-f', '--fancy'):
fancy = True
if len(args) < 2:
usage()
destDirectory = args[0]
fileNames = args[1:]
upload(destDirectory, fileNames, fancy)
if __name__ == '__main__':
main()
#import pdb
#pdb.run('main()')
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor