From c367db484cc304539c42fa0d4b2a390e37db34a7 Mon Sep 17 00:00:00 2001 From: a-Sansara Date: Wed, 15 May 2013 20:47:12 +0200 Subject: [PATCH] adding progress step + bugfix cli --- kirmah-cli.py | 1 + kirmah.py | 1 + kirmah/cli.py | 32 +++-- kirmah/cliapp.py | 2 +- kirmah/crypt.py | 310 +++++++++++++++++++++++++---------------------- kirmah/gui.py | 19 ++- kirmah/ui.py | 14 ++- psr/cli.py | 17 ++- psr/sys.py | 1 + 9 files changed, 212 insertions(+), 185 deletions(-) diff --git a/kirmah-cli.py b/kirmah-cli.py index 905da4c..bb7fe71 100644 --- a/kirmah-cli.py +++ b/kirmah-cli.py @@ -36,6 +36,7 @@ def main(): c = 0 Cli('.'+Sys.sep) except Exception as e : + raise e Sys.pwarn((('main : ',(str(e),Sys.CLZ_ERROR_PARAM), ' !'),), True) c = 1 return c diff --git a/kirmah.py b/kirmah.py index 5265d1b..6fdffcb 100644 --- a/kirmah.py +++ b/kirmah.py @@ -36,6 +36,7 @@ def main(): c = 0 AppGui() except Exception as e: + raise e Sys.pwarn((('main : ',(str(e),Sys.CLZ_ERROR_PARAM), ' !'),), True) c = 1 return c diff --git a/kirmah/cli.py b/kirmah/cli.py index 280da10..b82b7d0 100755 --- a/kirmah/cli.py +++ b/kirmah/cli.py @@ -46,7 +46,7 @@ class Cli(AbstractCli): def __init__(self, path, remote=False, rwargs=None, thread=None, loglvl=Const.LOG_DEFAULT): """""" - AbstractCli.__init__(self, conf) + AbstractCli.__init__(self, conf, self) Cli.HOME = conf.DEFVAL_USER_PATH @@ -84,21 +84,22 @@ class Cli(AbstractCli): Sys.g.GUI = thread is not None init(conf.PRG_NAME, o.debug, remote, not o.no_color, loglvl) - - - if not a: + + if not a: try : - if not o.help : - Cli.error_cmd(('no command specified',)) + if not o.help or not o.version: + self.parser.error_cmd(('no command specified',), True) else : Sys.clear() Cli.print_help() except : - Cli.error_cmd(('no command specified',)) + if not o.version : + self.parser.error_cmd(('no command specified',), True) + else : + Cli.print_header() else: - if a[0] == 'help': Sys.clear() Cli.print_help() @@ -111,9 +112,9 @@ class Cli(AbstractCli): app.onCommandKey() else : if not len(a)>1 : - Cli.error_cmd((('an ',('inputFile',Sys.Clz.fgb3),' is required !'),)) + self.parser.error_cmd((('an ',('inputFile',Sys.Clz.fgb3),' is required !'),), True) elif not Io.file_exists(a[1]): - Cli.error_cmd((('the file ',(a[1], Sys.Clz.fgb3), ' doesn\'t exists !'),)) + self.parser.error_cmd((('the file ',(a[1], Sys.Clz.fgb3), ' doesn\'t exists !'),), True) elif a[0]=='enc' : app.onCommandEnc() elif a[0]=='dec' : app.onCommandDec() @@ -125,10 +126,19 @@ class Cli(AbstractCli): Sys.g.LOG_QUEUE.put(Sys.g.SIGNAL_STOP) else : - Cli.error_cmd((('unknow command ',(a[0],Sys.Clz.fgb3)),)) + self.parser.error_cmd((('unknow command ',(a[0],Sys.Clz.fgb3)),)) if not o.quiet : Sys.dprint() +#~ + #~ @staticmethod + #~ def error_cmd(data): + #~ """""" + #~ Cli.print_usage('') + #~ Sys.dprint() + #~ Sys.pwarn(data, True) + #~ Cli.exit(1) + @staticmethod def print_usage(data, withoutHeader=False): diff --git a/kirmah/cliapp.py b/kirmah/cliapp.py index 7851747..0cc33bd 100755 --- a/kirmah/cliapp.py +++ b/kirmah/cliapp.py @@ -288,7 +288,7 @@ class CliApp: Sys.pwarn((str(e),)) if not Sys.g.QUIET : - self.onend_cmd('Merging file', self.stime, done, toPath) + self.onend_cmd('Kirmah Merge', self.stime, done, toPath) @Log(Const.LOG_ALL) diff --git a/kirmah/crypt.py b/kirmah/crypt.py index 4b907ed..3421403 100755 --- a/kirmah/crypt.py +++ b/kirmah/crypt.py @@ -363,39 +363,29 @@ class Kirmah: @Log() - def compress_start(self, fromPath, toPath, compress=True, lvl=9): + def compress_start(self, fromPath, toPath, compress=True, lvl=9, emit=True): """""" - d = Sys.datetime.now() - c = not Sys.is_cli_cancel() - if c : + if not Sys.is_cli_cancel(): with Io.rfile(fromPath) as fi : with Io.wfile(toPath) as fo : data = fi.read() if not compress else Io.gzcompress(fi.read(), lvl) fo.write(b2a_base64(data)) - Sys.wlog(Sys.dprint()) - Sys.pstep('Compression', d, c) @Log() - def uncompress_start(self, fromPath, toPath, decompress=True): + def uncompress_start(self, fromPath, toPath, decompress=True, emit=True): """""" - d = Sys.datetime.now() - c = not Sys.is_cli_cancel() - if c : + if not Sys.is_cli_cancel(): with Io.rfile(fromPath) as fi : with Io.wfile(toPath) as fo : data = a2b_base64(fi.read()) fo.write(data if not decompress else Io.gzdecompress(data)) - Sys.wlog(Sys.dprint()) - Sys.pstep('Compression mode', d, c) @Log() - def compress_end(self, fromPath, toPath, compress=True, lvl=9): + def compress_end(self, fromPath, toPath, compress=True, lvl=9, emit=True): """""" - d = Sys.datetime.now() - c = not Sys.is_cli_cancel() - if c: + if not Sys.is_cli_cancel(): with Io.rfile(fromPath) as fi : with Io.wfile(toPath) as fo : data = fi.read() @@ -403,25 +393,19 @@ class Kirmah: header = self.kh.buildHeader(len(data)) fo.write(header) fo.write(data) - Sys.wlog(Sys.dprint()) - Sys.pstep('Compression mode', d, c) @Log() - def uncompress_end(self, fromPath, toPath, decompress=True): + def uncompress_end(self, fromPath, toPath, decompress=True, emit=True): """""" - d = Sys.datetime.now() - c = not Sys.is_cli_cancel() - if c: + if not Sys.is_cli_cancel(): with Io.rfile(fromPath) as fi : with Io.wfile(toPath) as fo : fi.seek(self.kh.POS_END) fo.write(fi.read() if not decompress else Io.gzdecompress(fi.read())) - Sys.wlog(Sys.dprint()) - Sys.pstep('Compression mode', d, c) @Log(Const.LOG_ALL) - def encryptStr(self, data): + def encryptStr(self, data, emit=True): """""" if not Sys.is_cli_cancel(): s, lk, i = [], len(self.key), 0 @@ -435,7 +419,7 @@ class Kirmah: @Log() - def encryptToFile(self, fromPath, toPath, i=0, event=None): + def encryptToFile(self, fromPath, toPath, i=0, event=None, emit=True): """""" if not Sys.is_cli_cancel(): with Io.ufile(fromPath) as fi : @@ -452,7 +436,7 @@ class Kirmah: @Log() - def decryptToFile(self, fromPath, toPath, i=0, event=None): + def decryptToFile(self, fromPath, toPath, i=0, event=None, emit=True): """""" if not Sys.is_cli_cancel(): with Io.ufile(fromPath) as fi : @@ -478,7 +462,7 @@ class Kirmah: @Log() - def randomFileContent(self, fromPath, toPath): + def randomFileContent(self, fromPath, toPath, emit=True): """""" d = Sys.datetime.now() c = not Sys.is_cli_cancel() @@ -491,57 +475,12 @@ class Kirmah: for piece, i in Io.read_in_chunks(fi, chsize): fo.seek(lst[i]*chsize-(rest if lst[i] > lst[size-1] else 0)) fo.write(piece[::-1]) - Sys.wlog(Sys.dprint()) + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) Sys.pstep('Random mode', d, c) @Log() - def mpRandomFileContent(self, fromPath, toPath, nproc): - """""" - if not Sys.is_cli_cancel(): - Io.copy(fromPath, fromPath+'.ori') - fsize, chsize, size = Kirmah.getSizes(fromPath) - mpchsize = (fsize//nproc)+1 - if fsize % mpchsize == 0 : mpchsize -= 1 - #~ print('fsize : '+str(fsize)) - #~ print('chsize : '+str(chsize)) - #~ print('mpchsize : '+str(mpchsize)) - hlstPaths = [] - with Io.rfile(fromPath) as fi : - s, piece, fo = 0, 0, Io.wfile('proc_0') - hlstPaths.append('proc_0') - for pdata, _ in Io.read_in_chunks(fi, chsize): - s += chsize - if s > mpchsize : - piece += 1 - s = 0 - try : - fo.close() - except : - pass - fo = Io.wfile('proc_'+str(piece)) - hlstPaths.append('proc_'+str(piece)) - fo.write(pdata) - - self.mpMergeFiles(hlstPaths, fromPath+'.copy', True) - self.randomFileContent(fromPath, toPath) - - - @Log() - def mpMergeFiles(self,hlstPaths, toPath, noRemove=False): - """""" - d = Sys.datetime.now() - c = not Sys.is_cli_cancel() - if c: - with Io.wfile(toPath) as fo: - for fromPath in hlstPaths : - with Io.rfile(fromPath) as fi : - fo.write(fi.read()) - if not noRemove : Sys.removeFile(fromPath) - - - @Log() - def unRandomFileContent(self, fromPath, toPath): + def unRandomFileContent(self, fromPath, toPath, emit=True): """""" d = Sys.datetime.now() c = not Sys.is_cli_cancel() @@ -558,16 +497,16 @@ class Kirmah: if i == size-1 and rest > 0 : piece = piece[:-rest] if lst[i]==0 else piece[rest:] fo.write(piece[::-1]) - Sys.wlog(Sys.dprint()) - Sys.pstep('Random mode', d, c) + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.pstep('Random mode - inv', d, c) @Log() - def mixdata(self, fromPath, toPath, encryptNoise=False, label='kirmah', cpart=22): + def mixdata(self, fromPath, toPath, encryptNoise=False, label='kirmah', cpart=22, emit=True): """""" d = Sys.datetime.now() c = not Sys.is_cli_cancel() - if c: + if c: hlst = self.ck.getHashList(label, cpart, False) hlst['data'] = sorted(hlst['data'], key=lambda hlst: hlst[0]) size = Sys.getsize(fromPath) @@ -585,7 +524,7 @@ class Kirmah: fi.seek(psize*row[5]) fo.write(bdata[:row[2]] + fi.read(psize) + adata[:row[3]]) cp += 1 - Sys.wlog(Sys.dprint()) + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) Sys.pstep('Mix mode', d, c) @@ -608,8 +547,8 @@ class Kirmah: return lst - @Log() - def unmixdata(self, fromPath, toPath, label='kirmah', cpart=22): + @Log() + def unmixdata(self, fromPath, toPath, label='kirmah', cpart=22, emit=True): """""" d = Sys.datetime.now() c = not Sys.is_cli_cancel() @@ -637,12 +576,12 @@ class Kirmah: fo.write(dp[:rest]) break fo.write(dp) - Sys.wlog(Sys.dprint()) - Sys.pstep('Mix mode', d, c) + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.pstep('Mix mode - inv', d, c) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # - # # SPLIT # # + # # SPLIT # # @Log() def splitFile(self, fromPath, hlst, nproc=1): @@ -660,10 +599,10 @@ class Kirmah: @Log() def kcfEnc(self, hlst, nproc=1): if not Sys.is_cli_cancel(): - d = Sys.datetime.now() + d = Sys.datetime.now() theStr = {'name': hlst['head'][0], 'count': hlst['head'][1] } Io.set_data(self.DIR_DEPLOY+hlst['head'][2]+'.tmp', str(theStr)) - self.encrypt(self.DIR_DEPLOY+hlst['head'][2]+'.tmp', self.DIR_DEPLOY+hlst['head'][2]+'.kcf', nproc, KirmahHeader(self.VERSION, Io.bytes(self.mark), KirmahHeader.COMP_NONE, True, True)) + self.encrypt(self.DIR_DEPLOY+hlst['head'][2]+'.tmp', self.DIR_DEPLOY+hlst['head'][2]+'.kcf', nproc, KirmahHeader(self.VERSION, Io.bytes(self.mark), KirmahHeader.COMP_NONE, True, True), False) Sys.removeFile(self.DIR_DEPLOY+hlst['head'][2]+'.tmp') if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) Sys.pstep('Encrypting Kirmah configuration file', d, True) @@ -671,7 +610,7 @@ class Kirmah: return self.DIR_DEPLOY+hlst['head'][2]+'.kcf' - @Log() + @Log() def split(self, fromPath, hlst): """""" if not Sys.is_cli_cancel(): @@ -697,7 +636,7 @@ class Kirmah: Sys.cli_emit_progress(perc) p += 1 m.close() - + # ensure random order hlst['data'] = sorted(hlst['data'], key=lambda lst: lst[4]) hlst['head'].append(psize) @@ -717,7 +656,7 @@ class Kirmah: # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # - # # MERGE # # + # # MERGE # # @Log() def mergeFile(self, fromPath, toPath=None, uid=''): @@ -787,10 +726,23 @@ class Kirmah: # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # - # # ENCRYPT # # + # # ENCRYPT # # @Log() - def encrypt_sp_start(self, fromPath, toPath, header=None): + def mpMergeFiles(self,hlstPaths, toPath, noRemove=False, emit=True): + """""" + if not Sys.is_cli_cancel(): + with Io.wfile(toPath) as fo: + for fromPath in hlstPaths : + with Io.rfile(fromPath) as fi : + fo.write(fi.read()) + if not noRemove : Sys.removeFile(fromPath) + #~ if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + #~ Sys.pstep('Encrypt Data (multiprocessing)', d, c) + + + @Log() + def encrypt_sp_start(self, fromPath, toPath, header=None, emit=True): """""" if not Sys.is_cli_cancel(): if header is not None : @@ -803,41 +755,61 @@ class Kirmah: self.tmpPath2 = self.DIR_TEMP + Sys.basename(fromPath) + '.tmp2' compend, compstart = not decHeader['cmode']== KirmahHeader.COMP_NONE, decHeader['cmode']== KirmahHeader.COMP_ALL fp, tp = fromPath, self.tmpPath1 - Sys.wlog(Sys.dprint()) - Sys.ptask('Compressing data') - self.compress_start(fp, tp, compstart) + if emit : Sys.cli_emit_progress(2) + d = Sys.datetime.now() + if compstart : + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.ptask('Compressing data') + self.compress_start(fp, tp, compstart, emit=emit) + if compstart : + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.pstep('Compression mode', d, True) fp, tp = tp, self.tmpPath2 if tp == self.tmpPath1 else self.tmpPath1 + if emit : Sys.cli_emit_progress(5) return fp, tp, decHeader['rmode'], decHeader['mmode'], compend @Log() - def encrypt_sp_end(self, fp, tp, toPath, rmode, mmode, compend): + def encrypt_sp_end(self, fp, tp, toPath, rmode, mmode, compend, emit=True): """""" if not Sys.is_cli_cancel(): if rmode : #~ self.mpRandomFileContent(fp, tp, 4) - Sys.wlog(Sys.dprint()) + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) Sys.ptask('Randomizing data') - self.randomFileContent(fp, tp) + self.randomFileContent(fp, tp, emit=emit) fp, tp = tp, self.tmpPath2 if tp == self.tmpPath1 else self.tmpPath1 + if emit : Sys.cli_emit_progress(75) if mmode : - Sys.wlog(Sys.dprint()) + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) Sys.ptask('Mixing data') - self.mixdata(fp, tp, True) + self.mixdata(fp, tp, True, emit=emit) + fp, tp = tp, self.tmpPath2 if tp == self.tmpPath1 else self.tmpPath1 - Sys.wlog(Sys.dprint()) - Sys.ptask('Compressing data') - self.compress_end(fp, toPath, compend) + if emit : Sys.cli_emit_progress(85) + + if compend : + d = Sys.datetime.now() + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.ptask('Compressing data') + self.compress_end(fp, toPath, compend, emit=emit) + if emit : Sys.cli_emit_progress(95) + + if compend : + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.pstep('Compression mode', d, True) # clean tmp files - Sys.wlog(Sys.dprint()) + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) Sys.ptask('Cleaning') + try : Sys.removeFile(self.tmpPath1) Sys.removeFile(self.tmpPath2) except: pass + if emit : Sys.cli_emit_progress(97) @Log() @@ -859,49 +831,53 @@ class Kirmah: @Log() - def mproc_encode_part(self, id, event=None): + def mproc_encode_part(self, id, event=None, emit=True): """""" if not Sys.is_cli_cancel(): mpfile, mpfilenc = self.KMP_FILE+'_'+str(Sys.g.MAIN_PROC)+'_'+str(id), self.KMP_FILE+'enc_'+str(Sys.g.MAIN_PROC)+'_'+str(id) - self.encryptToFile(mpfile, mpfilenc, self.getSubStartIndice(id), event) + self.encryptToFile(mpfile, mpfilenc, self.getSubStartIndice(id), event, emit=emit) Sys.removeFile(mpfile) @Log() - def encrypt_mproc(self, fp, tp, nproc=1): + def encrypt_mproc(self, fp, tp, nproc=1, emit=True): """""" - Sys.wlog(Sys.dprint()) + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) Sys.ptask('Encrypting data') d = Sys.datetime.now() c = not Sys.is_cli_cancel() if c: if nproc == 1 : self.encryptToFile(fp, tp) + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.pstep('Encrypt data', d, c) else : hlstPaths = self.prepare_mproc_encode(fp, nproc) mg = Manager(self.mproc_encode_part, nproc, None, Sys.g.MPEVENT) mg.run() - self.mpMergeFiles(hlstPaths, tp) - Sys.wlog(Sys.dprint()) - Sys.pstep('Encrypt data', d, c) + self.mpMergeFiles(hlstPaths, tp, emit=emit) + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.pstep('Encrypt data (multiproc)', d, c) + if emit : Sys.cli_emit_progress(70) + @Log() - def encrypt(self, fromPath, toPath, nproc=1, header=None): - """""" + def encrypt(self, fromPath, toPath, nproc=1, header=None, emit=True): + """""" + if emit : Sys.cli_emit_progress(0) if not Sys.is_cli_cancel(): - fp, tp, rmode, mmode, compend = self.encrypt_sp_start(fromPath, toPath, header) - - self.encrypt_mproc(fp, tp, nproc) + fp, tp, rmode, mmode, compend = self.encrypt_sp_start(fromPath, toPath, header, emit=True) + self.encrypt_mproc(fp, tp, nproc, emit=True) fp, tp = tp, self.tmpPath2 if tp == self.tmpPath1 else self.tmpPath1 - - self.encrypt_sp_end(fp, tp, toPath, rmode, mmode, compend) + self.encrypt_sp_end(fp, tp, toPath, rmode, mmode, compend, emit=True) + if emit : Sys.cli_emit_progress(100) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # - # # DECRYPT # # - + # # DECRYPT # # + @Log() - def decrypt_sp_start(self, fromPath, toPath): + def decrypt_sp_start(self, fromPath, toPath, emit=True): """""" if not Sys.is_cli_cancel(): if Sys.getsize(fromPath) > 0 : @@ -910,67 +886,101 @@ class Kirmah: fsize = Sys.getsize(fromPath) fsize -= self.kh.POS_END with Io.rfile(fromPath) as f : + d = Sys.datetime.now() + if emit : Sys.cli_emit_progress(1) decHeader = self.kh.readHeader(f.read(self.kh.POS_END)) - print(decHeader) + if emit : Sys.cli_emit_progress(2) + #~ print(decHeader) if len(decHeader) > 0 : + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) if decHeader['smode'] == self.mark[fsize%len(self.mark)] : + Sys.pstep('Reading Header', d, True) #~ print('ok valid') """""" - else : raise BadKeyException() + else : + Sys.pstep('Reading Header', d, False) + raise BadKeyException() + compend, compstart = not decHeader['cmode']== KirmahHeader.COMP_NONE, decHeader['cmode']== KirmahHeader.COMP_ALL fp, tp = fromPath, self.tmpPath1 - Sys.wlog(Sys.dprint()) - Sys.ptask('Uncompressing data') - self.uncompress_end(fp, tp, compend) + if emit : Sys.cli_emit_progress(3) + if compend : + d = Sys.datetime.now() + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.ptask('Uncompressing data') + self.uncompress_end(fp, tp, compend, emit=emit) + if emit : Sys.cli_emit_progress(10) + if compend : + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.pstep('Compression mode', d, True) fp, tp = tp, self.tmpPath2 if tp == self.tmpPath1 else self.tmpPath1 + if decHeader['mmode'] : - Sys.wlog(Sys.dprint()) + d = Sys.datetime.now() + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) Sys.ptask('Sorting data') - self.unmixdata(fp, tp) + self.unmixdata(fp, tp, emit=emit) fp, tp = tp, self.tmpPath2 if tp == self.tmpPath1 else self.tmpPath1 + if emit : Sys.cli_emit_progress(20) if decHeader['rmode'] : - Sys.wlog(Sys.dprint()) + d = Sys.datetime.now() + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) Sys.ptask('Reordering data') - self.unRandomFileContent(fp, tp) + self.unRandomFileContent(fp, tp, emit=emit) fp, tp = tp, self.tmpPath2 if tp == self.tmpPath1 else self.tmpPath1 + if emit : Sys.cli_emit_progress(25) return fp, tp, compstart @Log() - def decrypt_sp_end(self, fromPath, toPath, compstart): + def decrypt_sp_end(self, fromPath, toPath, compstart, emit=True): """""" if not Sys.is_cli_cancel(): - Sys.wlog(Sys.dprint()) - Sys.ptask('Uncompressing data') - self.uncompress_start(fromPath, toPath, compstart) - Sys.wlog(Sys.dprint()) + d = Sys.datetime.now() + if emit : Sys.cli_emit_progress(80) + if compstart : + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.ptask('Uncompressing data') + self.uncompress_start(fromPath, toPath, compstart, emit=emit) + if emit : Sys.cli_emit_progress(90) + if compstart: + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.pstep('Compression mode', d, True) + + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) Sys.ptask('Cleaning') + if emit : Sys.cli_emit_progress(95) try : Sys.removeFile(self.tmpPath1) Sys.removeFile(self.tmpPath2) except: pass + if emit : Sys.cli_emit_progress(97) @Log() - def decrypt_mproc(self, fromPath, toPath, nproc=1): + def decrypt_mproc(self, fromPath, toPath, nproc=1, emit=True): """""" - Sys.wlog(Sys.dprint()) - Sys.ptask('Decrypting data') + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.ptask('Decrypting data') d = Sys.datetime.now() c = not Sys.is_cli_cancel() if c: + if emit : Sys.cli_emit_progress(30) if nproc == 1 : self.decryptToFile(fromPath, toPath) + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.pstep('Decrypt data', d, True) else : hlstPaths = self.prepare_mproc_decode(fromPath, nproc) - mg = Manager(self.mproc_decode_part, nproc, None, Sys.g.MPEVENT) + mg = Manager(self.mproc_decode_part, nproc, None, Sys.g.MPEVENT, emit=True) mg.run() - self.mpMergeFiles(hlstPaths, toPath) - Sys.wlog(Sys.dprint()) - Sys.pstep('Decrypt data', d, c) + self.mpMergeFiles(hlstPaths, toPath, emit=emit) + if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) + Sys.pstep('Decrypt data (multiproc)', d, True) + @Log() @@ -995,21 +1005,25 @@ class Kirmah: @Log() - def mproc_decode_part(self, id, event=None): + def mproc_decode_part(self, id, event=None, emit=True): """""" + if emit : Sys.cli_emit_progress(-1) if not Sys.is_cli_cancel(): mpfile, mpfiledec = self.KMP_FILE+'_'+str(Sys.g.MAIN_PROC)+'_'+str(id), self.KMP_FILE+'dec_'+str(Sys.g.MAIN_PROC)+'_'+str(id) - self.decryptToFile(mpfile, mpfiledec, self.getSubStartIndice(id), event) + self.decryptToFile(mpfile, mpfiledec, self.getSubStartIndice(id), event, emit=emit) Sys.removeFile(mpfile) @Log() - def decrypt(self, fromPath, toPath, nproc=1): + def decrypt(self, fromPath, toPath, nproc=1, emit=True): """""" + Sys.cli_emit_progress(0) if not Sys.is_cli_cancel(): - fp, tp, compstart = self.decrypt_sp_start(fromPath, toPath) - self.decrypt_mproc(fp, tp, nproc) - self.decrypt_sp_end(tp, toPath, compstart) + fp, tp, compstart = self.decrypt_sp_start(fromPath, toPath, emit=emit) + self.decrypt_mproc(fp, tp, nproc, emit=emit) + self.decrypt_sp_end(tp, toPath, compstart, emit=emit) + Sys.cli_emit_progress(100) + @Log(Const.LOG_DEBUG) diff --git a/kirmah/gui.py b/kirmah/gui.py index 832e922..e070db6 100644 --- a/kirmah/gui.py +++ b/kirmah/gui.py @@ -101,27 +101,27 @@ class AppGui(Gui): self.get('entry1').set_text(mark) Sys.g.UI_AUTO_SCROLL = True - self.textview = self.get('textview1') + self.textview = self.get('textview1') self.textview.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0, 0, 0, 1.0)) self.textview.modify_font(Pango.font_description_from_string ('DejaVu Sans Mono Book 11')) self.textbuffer = self.textview.get_buffer() self.tags = self.buildTxtTags(self.textbuffer) self.progressbar = self.get('progressbar1') cbt = self.get('comboboxtext1') - cbt.connect("changed", self.on_compression_changed) + cbt.connect("changed", self.on_compression_changed) tree_iter = cbt.get_model().get_iter_first() print(cbt.get_model().get_string_from_iter(tree_iter)) tree_iter = cbt.get_model().get_iter_from_string('3') cbt.set_active_iter(tree_iter) cbt = self.get('comboboxtext2') - cbt.connect("changed", self.on_logging_changed) + cbt.connect("changed", self.on_logging_changed) tree_iter = cbt.get_model().get_iter_first() tree_iter = cbt.get_model().get_iter_from_string('4') cbt.set_active_iter(tree_iter) Sys.clear() Sys.dprint('INIT UI') self.start = True - self.thkmh = None + self.thkmh = None @Log(Const.LOG_UI) @@ -359,7 +359,6 @@ class AppGui(Gui): def on_proceed(self, btn): """""" if btn.get_label() == conf.GUI_LABEL_OK : - btn = self.get('button1') btn.set_label(conf.GUI_LABEL_PROCEED) self.PROCEED = False self.pb.hide() @@ -367,10 +366,10 @@ class AppGui(Gui): else : if not self.PROCEED : - self.list_threads() self.PROCEED = True self.STOPPED = False btn.set_sensitive(False) + self.on_new_file_dest(self.get('filechooserbutton3')) self.pb = self.get('progressbar1') self.pb.set_fraction(0) self.pb.show() @@ -388,7 +387,7 @@ class AppGui(Gui): @Log(Const.LOG_UI) - def halt_thread(self, *args): + def halt_thread(self, *args): Sys.wlog(Sys.dprint()) Sys.pwarn(('thread interrupt',), False) self.get('button1').set_sensitive(False) @@ -404,11 +403,11 @@ class AppGui(Gui): try : btn = self.get('button1') btn.set_label('Proceed') - btn.set_sensitive(True) + btn.set_sensitive(True) self.PROCEED = False - btn.set_label(conf.GUI_LABEL_OK) + if not abort : btn.set_label(conf.GUI_LABEL_OK) self.get('checkbutton3').hide() - + except Exception as e: Sys.pwarn((('on_proceed_end : ',(str(e),Sys.CLZ_WARN_PARAM), ' !'),), False) pass diff --git a/kirmah/ui.py b/kirmah/ui.py index 4497bb8..3b2cd44 100644 --- a/kirmah/ui.py +++ b/kirmah/ui.py @@ -56,7 +56,7 @@ class Gui(): threads_init() self.wname = wname self.builder = Builder() - self.builder.add_from_file(conf.PRG_GLADE_PATH) + self.builder.add_from_file(conf.PRG_GLADE_PATH) self.builder.connect_signals(self) self.win = self.get(wname) self.win.connect('destroy', self.onDeleteWindow) @@ -222,7 +222,7 @@ class Gui(): self.textbuffer.insert_at_cursor(item[0]) ei = self.textbuffer.get_end_iter() oi = self.textbuffer.get_iter_at_offset(offs) - tagName = item[1] + tagName = item[1] self.textbuffer.apply_tag(self.tags[tagName], oi, ei) self.textbuffer.insert_at_cursor('\n') self.scroll_end() @@ -230,11 +230,11 @@ class Gui(): Sys.dprint('STOP') thread.cancel() self.update_progress(progress) - + @Log(Const.LOG_NEVER) def update_progress(self, progress, lvl=50): - if progress > 0 : + if progress > 0 : self.progressbar.set_text(str(progress)) lp = self.progressbar.get_fraction() diff = (progress/100.0 - lp) @@ -243,6 +243,8 @@ class Gui(): if nf < progress/100.0 : self.progressbar.set_fraction(nf) self.progressbar.set_fraction(progress/100.0) + else : + self.progressbar.set_fraction(self.progressbar.get_fraction()+0.01) @Log(Const.LOG_NEVER) @@ -355,11 +357,11 @@ class CliThread(Thread, IdleObject): def stop(self): """""" if self.isAlive(): - self.cancel() + self.cancel() if current_thread() .getName()==self.getName(): try: self.emit("interrupted") - Sys.thread_exit() + Sys.thread_exit() except RuntimeError as e : print(str(self.getName()) + ' COULD NOT BE TERMINATED') raise e diff --git a/psr/cli.py b/psr/cli.py index e91b37c..db53796 100644 --- a/psr/cli.py +++ b/psr/cli.py @@ -54,7 +54,7 @@ class TinyParser(OptionParser): def error(self, errMsg, errData=None): """""" self.print_usage('') - AbstractCli.error_cmd((errMsg,)) + self.error_cmd((errMsg,)) @@ -88,8 +88,9 @@ class AbstractCli(): if not Sys.isUnix : Const.LINE_SEP_CHAR = '-' AbstractCli.conf = prgconf self.parser = TinyParser() - self.parser.print_help = AbstractCli.print_help - self.parser.print_usage = AbstractCli.print_usage + self.parser.print_help = self.print_help + self.parser.print_usage = self.print_usage + self.parser.error_cmd = self.error_cmd self.parser.add_option('-v', '--version' , action='store_true', default=False) self.parser.add_option('-d', '--debug' , action='store_true', default=False) @@ -99,15 +100,13 @@ class AbstractCli(): self.parser.add_option('--no-color' , action='store_true' , default=False) - @staticmethod - def error_cmd(data): + def error_cmd(self, data, pusage=False): """""" - AbstractCli.print_usage('') + if pusage : self.print_usage('') Sys.dprint() Sys.pwarn(data, True) AbstractCli.exit(1) - @staticmethod def exit(code): """""" @@ -197,8 +196,8 @@ class AbstractCli(): Sys.print(' '*50+'description option b' , Sys.CLZ_HELP_ARG_INFO) - @staticmethod - def print_usage(data, withoutHeader=False): + + def print_usage(self, data, withoutHeader=False): """""" if not withoutHeader : AbstractCli.print_header() diff --git a/psr/sys.py b/psr/sys.py index e089a96..dc7fad2 100644 --- a/psr/sys.py +++ b/psr/sys.py @@ -77,6 +77,7 @@ class Sys: g.GUI = False g.GUI_PRINT_STDOUT = True g.MPEVENT = Event() + g.LOG_QUEUE = None @staticmethod