2006年07月15日 星期六 16:27
#!/usr/bin/env python
# -*- encoding: utf8 -*-
# Filename: copy.py
# Discripion:
# Author(s): yetist
# Version:
import os
import shutil
import gtk
import gobject
import gui_dialog
#import com_i18n
#import pygtk
import gettext
#com_i18n.init()
#_ = com_i18n._
_ = gettext.textdomain
class CopyDialog(gtk.Dialog):
def __init__(self,src_list,dst_dir,modal=True):
gtk.Dialog.__init__(self)
self.set_title(_("Copying..."))
self.set_default_size(400,200)
self.connect("destroy", self.quit)
self.connect("delete_event", self.quit)
if modal:
self.grab_add()
self.src_list=src_list
self.dst_dir=dst_dir
self.nnum=self.get_all_number(src_list)
self.cnum=0
self.run()
def filecopy(self,srcfile, dstfile):
self.fileprg.set_fraction(.0)
self.fileprg.set_text("0%")
while gtk.events_pending():
gtk.main_iteration()
length = os.stat(srcfile).st_size
fp_s=open(srcfile,"r")
fp_d=open(dstfile,"wb+")
i=0.0
while i< length:
if length-i < 1024:
lastsize=int(length-i)
data1=fp_s.read(lastsize)
i+=lastsize
else:
data1=fp_s.read(1024)
i+=1024
b=i/length
num=str(int(b*100))+"%"
if b <=1:
self.fileprg.set_fraction(b)
self.fileprg.set_text(num)
while gtk.events_pending():
gtk.main_iteration()
else:
self.fileprg.set_fraction(1)
self.fileprg.set_text("100%")
while gtk.events_pending():
gtk.main_iteration()
fp_d.write(data1)
fp_s.close()
fp_d.close()
def showtxt(self,srcpath,dstpath):
self.infotxt.set_text(_("copy files:"))
self.infotxt.set_justify(gtk.JUSTIFY_FILL)
file=os.path.basename(srcpath)
step=self.cnum/float(self.nnum)
self.prg.set_fraction(step)
while gtk.events_pending():
gtk.main_iteration()
self.prg.set_text(_("%s of %s") % (self.cnum,self.nnum))
self.filetxt.set_text(_("file:")+file)
self.fromtxt.set_text(_("from:")+srcpath)
self.totxt.set_text(_("to:")+dstpath)
def process(self):
for item in self.src_list:
if os.path.isdir(item):
#self.copy_dir(item)
basename=os.path.basename(item)
ddir=os.path.join(self.dst_dir,basename)
self.replace = True
if os.path.exists(ddir):
if gui_dialog.yesno(_("directory [%s] exist,replace
it?") % basename) == gtk.RESPONSE_NO:
self.replace=False
if self.replace== False:
for root, dirs, files in os.walk(item): #遍历目录
if len(dirs) !=0:
self.cnum+=len(dirs)
if len(files) != 0:
self.cnum+=len(files)
self.showtxt(item,ddir)
else:
if not os.path.exists(ddir):
os.mkdir(ddir)
self.cnum+=1
self.showtxt(item,ddir)
self.copy_dir(item,ddir)
elif os.path.isfile(item):
basename=os.path.basename(item)
dfile=os.path.join(self.dst_dir,basename)
self.replace = True
if os.path.exists(dfile):
if gui_dialog.yesno("file [%s] exist,replace it?" %
basename) == gtk.RESPONSE_NO:
self.replace=False
if self.replace== False:
self.cnum+=1
self.showtxt(item,dfile)
else:
self.cnum+=1
self.showtxt(item,dfile)
self.filecopy(item,dfile)
def copy_dir(self,src_dir,dst_dir):
"""把src_dir目录复制为dst_dir目录,不是复制为dst_dir目录下的子目
录"""
if src_dir[-1] != "/":
src_dir+="/"
for root, dirs, files in os.walk(src_dir): #遍历目录
if len(dirs) !=0:
for dir in dirs:
newroot=root.replace(src_dir,"")
dstdir=os.path.join(os.path.join(dst_dir,newroot),dir)
if os.path.exists(dstdir):
shutil.rmtree(dstdir)
os.mkdir(dstdir)
self.cnum+=1
pdir=os.path.join(root,dir)
self.showtxt(pdir,dstdir)
if len(files) !=0:
for file in files:
newroot=root.replace(src_dir,"")
dstfile=os.path.join(os.path.join(dst_dir,newroot),file)
sfile=os.path.join(root,file)
self.showtxt(sfile,dstfile)
self.filecopy(sfile,dstfile)
self.cnum+=1
def run(self):
hbox=gtk.HBox()
self.infotxt=gtk.Label(_("start copying..."))
hbox.pack_start(self.infotxt,False,False,0)
label=gtk.Label("")
hbox.pack_start(label,True,True,0)
self.vbox.pack_start(hbox,False,False,5)
self.prg=gtk.ProgressBar()
self.prg.set_text("")
self.vbox.pack_start(self.prg,False,False,3)
self.vbox.pack_start(gtk.HSeparator(),False,False,3)
self.fileprg=gtk.ProgressBar()
self.fileprg.set_text("")
self.vbox.pack_start(self.fileprg,False,False,3)
hbox=gtk.HBox()
self.filetxt=gtk.Label("")
hbox.pack_start(self.filetxt,False,False,0)
label=gtk.Label("")
hbox.pack_start(label,True,True,0)
self.vbox.pack_start(hbox,False,False,5)
hbox=gtk.HBox()
self.fromtxt=gtk.Label("")
hbox.pack_start(self.fromtxt,False,False,0)
label=gtk.Label("")
hbox.pack_start(label,True,True,0)
self.vbox.pack_start(hbox,False,False,5)
hbox=gtk.HBox()
self.totxt=gtk.Label("")
hbox.pack_start(self.totxt,False,False,0)
label=gtk.Label("")
hbox.pack_start(label,True,True,0)
self.vbox.pack_start(hbox,False,False,5)
self.show_all()
self.process()
self.destroy()
def quit(self, w=None, event=None):
self.hide()
self.destroy()
def get_all_number(self,srclist):
num=0L
for item in srclist:
if os.path.isdir(item): #如果是目录,就计算整个目录的大小
for root, dirs, files in os.walk(item): #遍历目录
if len(dirs) !=0:
num+=len(dirs)
if len(files) !=0:
num+=len(files)
else: #如果是文件就加一
num+=1
return num
if __name__ == "__main__":
srclist=[ '/etc/fonts',
'/etc/fdmount.conf','/home/yetist/tmp/knoppix_2.6.12_wu.iso',
'/etc/fam.conf', '/etc/exim4']
dstdir="/tmp"
a=CopyDialog(srclist,dstdir,True)
-----------------------
这是一个显示多文件复制过程的进度对话框,srclist列表中为要复制的文件/目
录,dstdir为要复制到的目录。
现在此对话框工作正常,可是还需要加入一个取消按钮,我曾经加入取消按钮,可
是点击之后,程序的运行就比较异常,谁能帮我在里面加入一个取消按钮?谢了。
附件是对话框。
运行时把这两个文件放在同一目录下就可以。
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gui_dialog.py
Type: text/x-python
Size: 9433 bytes
Desc: not available
Url : http://lists.exoweb.net/pipermail/python-chinese/attachments/20060715/70c4c344/gui_dialog.py
Zeuux © 2025
京ICP备05028076号