daily pastebin goal
28%
SHARE
TWEET

zipme

phjoe Oct 2nd, 2015 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import appuifw as A
  2. import os
  3. import powlite_fm
  4.  
  5.  
  6. class ZipME:
  7.  def __init__(self):
  8.   self.run=0
  9.  
  10.  def fman(self,arg):
  11.   if arg>0:
  12.    p=fm.AskUser(find='dir')
  13.   else:
  14.    p=fm.AskUser('E:',ext=Exts)
  15.   return p
  16.  
  17.  def go(self):
  18.   while 1:
  19.    p=self.fman(1)
  20.    if not p:
  21.     # jika path tidak ada yg dipilih, tanya apakah mau memilih lagi
  22.     if A.query(u'Browse again ?','query'):
  23.      # jika ya, lanjut memilih lagi
  24.      continue
  25.     else:
  26.      # jika tidak, stop
  27.      break
  28.  
  29.    # lanjutkan jika ada path yg dipilih
  30.    self.pack(p)
  31.    break
  32.  
  33.  def scan(self,path):
  34.   # scan files dalam folder dan  sub-folder nya
  35.   p=[path]
  36.   lst=[]
  37.   while len(p)>0:
  38.    visit=p.pop()
  39.    for i in os.listdir(visit):
  40.     fp=os.path.join(visit,i)
  41.     if os.path.isfile(fp):
  42.      lst.append(os.path.abspath(fp))
  43.     elif os.path.isdir(fp):
  44.      p.append(fp)
  45.   lst.sort(lambda x,y: cmp(x.upper(),y.upper()))
  46.   return lst
  47.  
  48.  def pack(self, path):
  49.   A.e32.ao_sleep(0)
  50.   files=self.scan(path)
  51.  
  52.   # path file zip baru ada di E
  53.   zn = 'E:\\'+ os.path.basename(en(path)[:-1])+'.zip'
  54.   if len(files):
  55.  
  56.    # jika folder yg dipilih ada file nya, tanya dulu, mau lanjut atau tidak
  57.    if A.query(u'%s\nPack this folder ?' %path, 'query'):
  58.  
  59.     # jika ya, oke lanjut bikin zip nya
  60.     self.__zipf(files,zn)
  61.   else:
  62.    # jika folder kosong
  63.    A.note(u'No files in this directory', 'error')
  64.  
  65.  
  66.  def __zipf(self,files,zipname):
  67.   # backup interface tampilan / listbox awal
  68.   old=(A.app.title,A.app.body,A.app.menu,A.app.exit_key_handler)
  69.  
  70.   # fungsi back
  71.   def back(): # tanpa self
  72.  
  73.    # untuk kembali ke tampilan awal, swith run dan srun jgn lupa dikembalikan
  74.    self.run,self.srun=1,0
  75.  
  76.    # kembalikan ke tampilan semula
  77.    (A.app.title,A.app.body,A.app.menu,A.app.exit_key_handler)=old
  78.  
  79.   # switch, run = untuk listbox utama, srun = untuk listbox proses, matikan dulu run (utama) supaya ga hang karena ada proses 'while' yg bertimpa2.
  80.  
  81.   self.run,self.srun=0,1
  82.   item=[(u'Packing files', u'Please wait...')]
  83.   lb=A.Listbox(item,)
  84.   A.app.title=u'Processing'
  85.   A.app.body=lb
  86.  
  87.   # ambil nafas sebelum import modul
  88.   A.e32.ao_sleep(0)
  89.   import zipfile
  90.  
  91.   arch = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
  92.  
  93.   # jumlah file yg berhasil di pack
  94.   pkg=0
  95.  
  96.   for n in range(len(files)):
  97.    per=float(n)/(len(files)-1)*100
  98.    try:
  99.     arch.write(en(files[n]),en(files[n])[3:])
  100.     # teks di listbox
  101.     item[0]=u'%s' %os.path.basename(en(files[n])), u'Process.. %0.0f%%' %per
  102.  
  103.     # file yg berhasil di pack nambah 1
  104.     pkg+=1
  105.    except:pass
  106.  
  107.    # update teks di listbox
  108.    lb.set_list(item,0)
  109.    A.e32.ao_sleep(1e-04)
  110.  
  111.   arch.close()
  112.   # close file zip baru nya
  113.  
  114.   # tampilkan pesan komplit
  115.   A.app.title=u'Completed'
  116.   item[0]=u'Packing Completed', u'%d of %d file(s) Packed' %(pkg,len(files))
  117.   lb.set_list(item,0)
  118.   A.app.menu=[]
  119.   A.app.exit_key_handler=back
  120.   while self.srun:
  121.    A.e32.ao_sleep(1e-04)
  122.  
  123.  def exit(self):
  124.   self.run=0
  125.  
  126.  def main(self):
  127.   self.run=1
  128.   def cb():
  129.    x=lb.current()
  130.    (self.go,lambda:none)[x]()
  131.   item=[(u'Browse',u'Select folder to pack')]
  132.   lb=A.Listbox(item,cb)
  133.  
  134.   A.app.title=u'ZipME'
  135.   A.app.body=lb
  136.   A.app.menu=[(u'Select', cb)]
  137.   A.app.exit_key_handler=self.exit
  138.   while self.run:
  139.    A.e32.ao_sleep(1e-04)
  140.  
  141.  
  142. en=lambda x:x.encode('u8')
  143. fm=powlite_fm.manager()
  144. zip=ZipME().main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top