From 632d0e46fb1eea82b3656cd5d9b2ec3e088c6913 Mon Sep 17 00:00:00 2001 From: a-sansara Date: Wed, 10 Oct 2012 19:13:21 +0200 Subject: [PATCH] refact debug, adding quiet and debug mode --- impra/cli.py | 125 +++++++++++++++++--------------- impra/core.py | 188 +++++++++++++++++++++++++++---------------------- impra/crypt.py | 37 ++++++++-- impra/imap.py | 37 +++++----- impra/util.py | 78 +++++++++++--------- 5 files changed, 271 insertions(+), 194 deletions(-) diff --git a/impra/cli.py b/impra/cli.py index 9b9cb1b..01aaf59 100644 --- a/impra/cli.py +++ b/impra/cli.py @@ -34,7 +34,7 @@ import sys, os, platform import impra.crypt as crypt import impra.util as util import impra.core as core -from impra.util import Clz +from impra.util import Clz, mprint LINE_SEP_LEN = 120 @@ -103,8 +103,9 @@ class Cli: gpConf = OptionGroup(parser, '') # metavar=' ', nargs=2 - parser.add_option('-q', '--quiet' , help='don\'t print status messages to stdout' , action='store_false', default=True) + parser.add_option('-q', '--quiet' , help='don\'t print status messages to stdout' , action='store_true', default=False) parser.add_option('-d', '--debug' , help='set debug mode' , action='store_true' , default=False) + parser.add_option('--no-color' , help='disable color mode' , action='store_true' , default=False) gpData.add_option('-c', '--category' , help='set specified CATEGORY (crit. for opt. -l,-a or -s)' , action='store', metavar='CATG ') gpData.add_option('-u', '--user' , help='set specified USER (crit. for opt. -l,-a or -s)' , action='store', metavar='OWNER ') @@ -132,14 +133,26 @@ class Cli: (o, a) = parser.parse_args() - print() + if o.no_color : + util.Clz.active = False + + if o.quiet : + util.DEBUG.active = False + else: + util.DEBUG.active = True + if o.debug : + util.DEBUG.level = util.DEBUG.ALL + else : + util.DEBUG.level = util.DEBUG.INFO + + mprint() if not a: try : if not o.help : self.parserError(' no commando specified') else : - core.clear() + if util.DEBUG.active : core.clear() parser.print_help() except : self.parserError(' no commando specified') @@ -149,7 +162,7 @@ class Cli: # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~ conf CMD ~~ if a[0] == 'conf' : - core.clear() + if util.DEBUG.active : core.clear() if o.load is not None or o.view is not None or o.save is not None : if o.view is not None : @@ -181,7 +194,7 @@ class Cli: else : colr = Clz.fgB3 Clz.print(sep+p, colr, False) if sep=='':sep=',' - print() + mprint() else : Clz.print(' no profiles', Clz.fgB1) else: self.ini.print(o.view) @@ -225,7 +238,7 @@ class Cli: self.print_usage('') elif a[0] == 'help': - core.clear() + if util.DEBUG.active : core.clear() parser.print_help() # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -236,14 +249,14 @@ class Cli: if self.check_profile(o.active_profile): - core.clear() - self.print_header() + if util.DEBUG.active : core.clear() + if util.DEBUG.active: self.print_header() conf = core.ImpraConf(self.ini,o.active_profile) impst = None try: impst = core.ImpraStorage(conf, False, self.wkpath) except crypt.BadKeyException as e : - print() + mprint() Clz.print(' it seems that your current profile `' , Clz.fgB1, False) Clz.print(o.active_profile , Clz.fgB3, False) Clz.print('` (account:`' , Clz.fgB1, False) @@ -266,17 +279,17 @@ class Cli: resp = input(' remove index ? (yes/no) ') if resp.lower()=='yes': impst = core.ImpraStorage(conf, True, self.wkpath) - print() - print(' bye') + mprint() + mprint(' bye') Clz.print(' ',Clz.OFF) - print() + mprint() self.exit(1) else : - print() - print(' bye') + mprint() + mprint(' bye') Clz.print(' ',Clz.OFF) - print() + mprint() self.exit(1) @@ -289,7 +302,7 @@ class Cli: noData = impst.index.isEmpty() if uid == None or noData : uid = 'EMPTY' if date == None or noData : date = '' - core.clear() + if util.DEBUG.active : core.clear() printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printHeaderTitle(APP_TITLE) printHeaderPart('account',account) @@ -302,7 +315,7 @@ class Cli: matchIdsUser = None matchIds = None if o.category is not None : - print(o.category) + mprint(o.category) matchIdsCatg = impst.index.getByCategory(o.category) if o.user is not None : matchIdsUser = impst.index.getByUser(o.user) @@ -332,9 +345,9 @@ class Cli: if o.category is None : o.category = '' done = impst.addFile(vfile,label,o.category) if done : - print('\n ',end='') + mprint('\n ',end='') Clz.print(' == OK == ', Clz.bg2+Clz.fgb7) - print() + mprint() else : self.error_cmd('`'+a[1]+' is not a file',parser) @@ -342,11 +355,11 @@ class Cli: if not len(a)>1 : self.error_cmd('`'+a[0]+' need an id',parser) else: if not util.represents_int(a[1]): - print('\n ',end='') + mprint('\n ',end='') Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(a[1] , Clz.bg1+Clz.fgB3, False) Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) - print() + mprint() self.exit(1) else : vid = a[1] @@ -355,21 +368,21 @@ class Cli: done = impst.editFile(key,o.label,o.category) if done : impst.saveIndex() - print('\n ',end='') + mprint('\n ',end='') Clz.print(' == OK == ', Clz.bg2+Clz.fgb7) - print() + mprint() else : - print('\n ',end='') + mprint('\n ',end='') Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(a[1] , Clz.bg1+Clz.fgB3, False) Clz.print('` has not been modified == ', Clz.bg1+Clz.fgB7) - print() + mprint() else: - print('\n ',end='') + mprint('\n ',end='') Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(a[1] , Clz.bg1+Clz.fgB3, False) Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) - print() + mprint() elif a[0] == 'get': @@ -386,15 +399,15 @@ class Cli: if key !=None : done = impst.getFile(key) if done : - print('\n ',end='') + mprint('\n ',end='') Clz.print(' == OK == ', Clz.bg2+Clz.fgb7) - print() + mprint() else: - print('\n ',end='') + mprint('\n ',end='') Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(str(sid) , Clz.bg1+Clz.fgB3, False) Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) - print() + mprint() elif a[0] == 'search': @@ -409,7 +422,7 @@ class Cli: vsearch = a[1] matchIds = impst.index.getByPattern(vsearch) - core.clear() + if util.DEBUG.active : core.clear() if matchIds is not None: printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printHeaderTitle(APP_TITLE) @@ -429,7 +442,7 @@ class Cli: matchIdsUser = None matchIdsCrit = None if o.category is not None : - print(o.category) + mprint(o.category) matchIdsCatg = impst.index.getByCategory(o.category) if o.user is not None : matchIdsUser = impst.index.getByUser(o.user) @@ -450,11 +463,11 @@ class Cli: order = '-'+o.order_inv impst.index.print(o.order,matchIds) else: - print('\n ',end='') + mprint('\n ',end='') Clz.print(' == no match found for pattern `', Clz.bg3+Clz.fgB4, False) Clz.print(vsearch , Clz.bg3+Clz.fgB1, False) Clz.print('` == ' , Clz.bg3+Clz.fgB4) - print() + mprint() elif a[0] == 'remove': @@ -462,11 +475,11 @@ class Cli: else : if not util.represents_int(a[1]): - print('\n ',end='') + mprint('\n ',end='') Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(a[1] , Clz.bg1+Clz.fgB3, False) Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) - print() + mprint() self.exit(1) else : vid = a[1] @@ -474,22 +487,22 @@ class Cli: if key !=None : done = impst.removeFile(key) if done : - print('\n ',end='') + mprint('\n ',end='') Clz.print(' == OK == ', Clz.bg2+Clz.fgb7) - print() + mprint() else: - print('\n ',end='') + mprint('\n ',end='') Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(a[1] , Clz.bg1+Clz.fgB3, False) Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) - print() + mprint() else : self.check_profile(o.active_profile, True) else : if self.ini.isEmpty() : Clz.print(' '*4+'ImpraStorage has no configuration file !!', Clz.fgB1) - print() + mprint() Clz.print(' '*8+'# to create the config file you must use this command with appropriate values :',Clz.fgn7) Clz.print(' '*8+'# type command help for details',Clz.fgn7) Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) @@ -508,17 +521,17 @@ class Cli: Clz.print('accountPassword ', Clz.fgB1) else : self.error_cmd('unknow command `'+a[0]+'`',parser) - print() + mprint() def error_cmd(self,msg, parser): - core.clear() + if util.DEBUG.active : core.clear() self.print_usage('') Clz.print('error : '+msg,Clz.fgB7) self.exit(1) def parserError(self, msg): - core.clear() + if util.DEBUG.active : core.clear() self.print_usage('') Clz.print('error : '+msg,Clz.fgB7) self.exit(1) @@ -569,7 +582,7 @@ class Cli: printHeaderPart('copyright','2012 '+APP_COPY) Clz.print(' ', Clz.OFF) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) - print() + mprint() def print_version(self, data): self.print_header() @@ -727,7 +740,7 @@ class Cli: Clz.print(' ]', Clz.fgB3) def print_options(self): - print('\n') + mprint('\n') printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) Clz.print(' MAIN OPTIONS :\n' , Clz.fgB3) Clz.print(' '*4+'-h, --help' , Clz.fgB3) @@ -736,7 +749,7 @@ class Cli: Clz.print(' '*50+'don\'t print status messages to stdout' , Clz.fgB7) Clz.print(' '*4+'-d, --debug' , Clz.fgB3) Clz.print(' '*50+'set debug mode' , Clz.fgB7) - print('\n') + mprint('\n') Clz.print(' COMMANDS OPTIONS :\n' , Clz.fgB3) Clz.print(' '*4+'-c ' , Clz.fgB3, False) @@ -769,7 +782,7 @@ class Cli: Clz.print('COLON'.ljust(10,' ') , Clz.fgB1) Clz.print(' '*50+'reverse order by specified colon' , Clz.fgB7) - print('\n') + mprint('\n') Clz.print(' CONF OPTIONS :\n', Clz.fgB3) Clz.print(' '*4+'-L ' , Clz.fgB3, False) Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1, False) @@ -795,7 +808,7 @@ class Cli: Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1) Clz.print(' '*50+'save the specified profile' , Clz.fgB7) - print('\n') + mprint('\n') Clz.print(' CONF -S OPTIONS :\n', Clz.fgB3) Clz.print(' '*4+'-N ' , Clz.fgB3, False) Clz.print('NAME'.ljust(10,' ') , Clz.fgB1, False) @@ -851,7 +864,7 @@ class Cli: Clz.print('USER'.ljust(10,' ') , Clz.fgB1) Clz.print(' '*50+'remove imap multi account' , Clz.fgB7) - print('\n') + mprint('\n') def print_help(self): @@ -861,7 +874,7 @@ class Cli: self.print_usage('',True) self.print_options() printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) - print() + mprint() Clz.print(' EXEMPLES :\n', Clz.fgB3) CHQ = "'" sep = core.sep @@ -1115,17 +1128,17 @@ class Cli: Clz.print('bob23', Clz.fgB1) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) - print() + mprint() def load_profile(self,o): """""" if self.check_profile(o.active_profile): - print('',end=' ') + mprint('',end=' ') Clz.print(' == profile `', Clz.bg2+Clz.fgb7, False) Clz.print(o.active_profile, Clz.bg2+Clz.fgB3, False) Clz.print('` loaded == ', Clz.bg2+Clz.fgb7) - print() + mprint() self.ini.set('profile', o.active_profile) self.ini.write() else : diff --git a/impra/core.py b/impra/core.py index eee1f43..2a8f1d5 100644 --- a/impra/core.py +++ b/impra/core.py @@ -46,7 +46,7 @@ from os.path import abspath, dirname, join, realpath, basename, get from re import split as regsplit, match as regmatch, compile as regcompile, search as regsearch from time import time, sleep from impra.imap import ImapHelper, ImapConfig -from impra.util import __CALLER__, RuTime, formatBytes, randomFrom, bstr, quote_escape, stack, run, file_exists, get_file_content, DEBUG, DEBUG_ALL, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, mkdir_p, is_binary, clear, Clz +from impra.util import __CALLER__, RuTime, formatBytes, randomFrom, bstr, quote_escape, stack, run, file_exists, get_file_content, DEBUG, mkdir_p, is_binary, clear, Clz, mprint from impra.crypt import Kirmah, ConfigKey, Noiser, Randomiz, hash_sha256, hash_md5_file, BadKeyException, hash_sha256_file @@ -58,12 +58,14 @@ class FSplitter : def __init__(self, ck, wkdir='./'): """""" + rt = RuTime(eval(__CALLER__())) self.ck = ck self.wkdir = wkdir self.DIR_CACHE = join(self.wkdir,'.cache')+sep self.DIR_INBOX = join(self.wkdir,'inbox')+sep self.DIR_OUTBOX = join(self.wkdir,'outbox')+sep self.DIR_DEPLOY = join(self.wkdir,'deploy')+sep + rt.stop() def addFile(self, fromPath, label, fixCount = False): """""" @@ -87,8 +89,7 @@ class FSplitter : def _split(self, fromPath, hlst): """""" - from impra.util import DEBUG_NOTICE, DEBUG, DEBUG_LEVEL, DEBUG_INFO - rt = RuTime(eval(__CALLER__()),DEBUG_INFO) + rt = RuTime(eval(__CALLER__()),DEBUG.INFO) f = open(fromPath, 'rb+') m = mmap(f.fileno(), 0) p = 0 @@ -112,8 +113,8 @@ class FSplitter : """""" rt = RuTime(eval(__CALLER__('mmap,%s,%s,phlist' % (part,size)))) with open(self.DIR_OUTBOX+phlst[1]+'.ipr', mode='wb') as o: - #~ print(self.DIR_OUTBOX+phlst[1]+'.ipr') - #~ print(str(phlst[2])+' - '+str(size)+' - '+str(phlst[3])+' = '+str(phlst[2]+size+phlst[3])) + #~ mprint(self.DIR_OUTBOX+phlst[1]+'.ipr') + #~ mprint(str(phlst[2])+' - '+str(size)+' - '+str(phlst[3])+' = '+str(phlst[2]+size+phlst[3])) o.write(self.ck.noiser.getNoise(phlst[2])+mmap.read(size)+self.ck.noiser.getNoise(phlst[3])) rt.stop() @@ -130,7 +131,7 @@ class FSplitter : else: dirPath = self.DIR_DEPLOY filePath = dirPath+fileName - if file_exists(filePath+ext): + if file_exists(filePath+ext): Clz.print('\n name already exist, deploying file as :' , Clz.fgB1) Clz.print(' '+basename(filePath) , Clz.fgB2, False) Clz.print('-'+str(uid) , Clz.fgB1, False) @@ -278,6 +279,7 @@ class ImpraIndex: initial content of the index encrypted with Kirmah Algorythm and representing a dic index as json string """ + rt = RuTime(eval(__CALLER__())) self.km = Kirmah(key, mark) self.dic = {} if encdata =='' : @@ -298,15 +300,18 @@ class ImpraIndex: else : if not self.SEP_KEY_INTERN+k in self.dic: self.dic[self.SEP_KEY_INTERN+k] = dicCategory[k] + rt.stop() def add(self,key, label, count, ext='', usr='', cat='', md5='', bFlag='b', size='', account=''): """Add an entry to the index """ + rt = RuTime(eval(__CALLER__())) if self.get(md5) == None : self.dic[md5] = (key,label,count,ext,usr,cat,self.id,bFlag,size,account) self.id +=1 else : - print(label+' already exist') + mprint(label+' already exist') + rt.stop() def addUser(self, nameFrom, hashName): """""" @@ -371,6 +376,7 @@ class ImpraIndex: """Get the corresponding key in the index :Returns: `tuple` row """ + rt = RuTime(eval(__CALLER__())) done = False row = self.dic[key] r = list(row) @@ -380,6 +386,7 @@ class ImpraIndex: r[self.CATG] = category self.dic[key] = tuple(r) done = row != self.dic[key] + rt.stop() return done def getById(self,sid): @@ -395,14 +402,17 @@ class ImpraIndex: def fixAccount(self,account): """""" + rt = RuTime(eval(__CALLER__())) r = [k for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)] for k in r: t = list(self.dic[k]) t[self.ACCOUNT] = account self.dic[k] = tuple(t) + rt.stop() def getLightestAccount(self,l): """""" + rt = RuTime(eval(__CALLER__())) r = [k for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)] t = {} for k in r: @@ -418,6 +428,7 @@ class ImpraIndex: if account is None : d = sorted(r, reverse=False, key=lambda lst:lst[0]) account = d[0][1] + rt.stop() return account def fixDuplicateIds(self): @@ -435,10 +446,10 @@ class ImpraIndex: d = [k[0] for k in l if any( k[1] == v for v in l3)] for k in d: mxid += 1 - print(self.dic[k]) + #mprint(self.dic[k]) t = list(self.dic[k]) t[self.UID] = mxid - print(t) + #mprint(t) self.dic[k] = tuple(t) self.id = mxid+1 else: @@ -502,14 +513,14 @@ class ImpraIndex: def encrypt(self): """""" - #~ print('encrypting index :') + #~ mprint('encrypting index :') jdata = jdumps(self.dic) - #~ print(jdata) + #~ mprint(jdata) return self.km.encrypt(jdata,'.index',22) def decrypt(self,data): """""" - #~ print('decrypting index : ') + #~ mprint('decrypting index : ') try : jdata = self.km.decrypt(data,'.index',22) data = jloads(jdata) @@ -532,18 +543,20 @@ class ImpraIndex: if sizeid < 3 : sizeid = 3 sizeid = 3 addsize = abs(3 - sizeid); + Clz.print(' ID'+' '*(1+addsize), Clz.BG4+Clz.fgB7, False, False) - print('HASH' +' '*6 , end='') - print('LABEL' +' '*(35), end='') - print('SIZE' +' '*5 , end='') - print('PART' +' '*2 , end='') - print('TYPE' +' '*2 , end='') - print('USER' +' '*11, end='') - print('CATEGORY'+' '*(22-addsize)) - #print('CATEGORY'+' '*(22-addsize), end='') + mprint('HASH' +' '*6 , end='') + mprint('LABEL' +' '*(35), end='') + mprint('SIZE' +' '*5 , end='') + mprint('PART' +' '*2 , end='') + mprint('TYPE' +' '*2 , end='') + mprint('USER' +' '*11, end='') + mprint('CATEGORY'+' '*(22-addsize)) + #mprint('CATEGORY'+' '*(22-addsize), end='') #Clz.print('ACCOUNT'+' '*(3), Clz.BG4+Clz.fgB7) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) - + dbg = DEBUG.active + DEBUG.active = True a = '' tsize = 0 psize = 0 @@ -566,11 +579,12 @@ class ImpraIndex: #~ if v[self.ACCOUNT] in acc : #~ acc[v[self.ACCOUNT]] += int(v[self.SIZE]) #~ else : acc[v[self.ACCOUNT]] = int(v[self.SIZE]) - #~ else: print() + #~ else: mprint() psize += int(v[self.SIZE]) tsize += int(v[self.SIZE]) if len(d)==0: Clz.print(' empty', Clz.fgB1) + DEBUG.active = dbg printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) c = Clz.fgB2 if psize != tsize : c = Clz.fgB7 @@ -579,7 +593,7 @@ class ImpraIndex: if psize != tsize : Clz.print(' / ', Clz.fgB3, False) Clz.print(formatBytes(int(tsize)), Clz.fgB2, False) - print() + mprint() printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) #~ Clz.print(' '*4+'[', Clz.fgB7, False) #~ sep = '' @@ -590,7 +604,8 @@ class ImpraIndex: #~ Clz.print(formatBytes(acc[k]),Clz.fgB2,False) #~ if sep=='':sep = ',' #~ Clz.print(']', Clz.fgB7, False) - #~ print() + #~ mprint() + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~ class ImpraStorage ~~ @@ -600,8 +615,7 @@ class ImpraStorage: def __init__(self, conf, remIndex=False, wkdir=None): """""" - from impra.util import DEBUG_INFO - rt = RuTime(eval(__CALLER__()),DEBUG_INFO) + rt = RuTime(eval(__CALLER__()),DEBUG.INFO) if wkdir == None : wkdir = abspath(join(dirname( __file__ ), '..', 'wk')) self.wkdir = wkdir self.conf = conf @@ -625,9 +639,9 @@ class ImpraStorage: if self.ih is None or self.ih.conf.user != iconf.user : self.ih = ImapHelper(iconf,self.rootBox) except Exception as e: - print('Error :') - print(e) - print('check your connection or your imap config') + mprint('Error :') + mprint(e) + mprint('check your connection or your imap config') def _getIdIndex(self): """""" @@ -669,8 +683,7 @@ class ImpraStorage: def getIndex(self, forceRefresh=False): """""" - from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_WARN, DEBUG_INFO - rt = RuTime(eval(__CALLER__()),DEBUG_INFO) + rt = RuTime(eval(__CALLER__()),DEBUG.INFO) index = None encData = '' uid = self.conf.get('uid' ,'index') @@ -706,6 +719,7 @@ class ImpraStorage: def removeIndex(self): """""" + rt = RuTime(eval(__CALLER__())) self._getIdIndex() if self.idx : self.ih.delete(self.idx, True) @@ -713,17 +727,17 @@ class ImpraStorage: self.conf.rem('*','index') self.idx = None remove(self.pathInd) + rt.stop() def saveIndex(self): """""" - from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO - rt = RuTime(eval(__CALLER__()),DEBUG_INFO) + rt = RuTime(eval(__CALLER__()),DEBUG.INFO) try: if self.idx != None : self.ih.delete(self.idx, True) except Exception as e : - print('error : ') - print(e) + mprint('error : ') + mprint(e) #~ if len(self.delids) > 0 : #~ for i in self.delids : self.ih.delete(i, True, False) #~ Clz.print('\n expunge, waiting server...\n', Clz.fgB1) @@ -732,7 +746,7 @@ class ImpraStorage: self.index.fixDuplicateIds() encData = self.index.encrypt() msgIndex = self.mb.buildIndex(encData) - if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : print(msgIndex.as_string()) + if DEBUG.level <= DEBUG.NOTICE : mprint(msgIndex.as_string()) ids = self.ih.send(msgIndex.as_string(), self.rootBox) date = self.ih.headerField('date', ids[1], True) self.conf.set('uid',ids[1],'index') @@ -746,16 +760,20 @@ class ImpraStorage: def encryptTextFile(self,path): """""" + rt = RuTime(eval(__CALLER__())) cdata = self.index.km.subenc(get_file_content(path)) with open(self.fsplit.DIR_CACHE+'.~KirmahEnc', mode='w') as o: o.write(cdata) + rt.stop() return self.fsplit.DIR_CACHE+'.~KirmahEnc' def decryptTextFile(self,path): """""" + rt = RuTime(eval(__CALLER__())) data = self.index.km.subdec(get_file_content(path)) with open(path, mode='w') as o: - o.write(data) + o.write(data) + rt.stop() def checkSendIds(self,sendIds,subject): """""" @@ -765,6 +783,7 @@ class ImpraStorage: def switchFileAccount(self,account=None): """""" + rt = RuTime(eval(__CALLER__())) al = self.conf.get('multi','imap') if al is not None : al = eval(al) @@ -782,15 +801,15 @@ class ImpraStorage: iconf.pwd = al[account] self.ih = ImapHelper(iconf,self.rootBox) except Exception as e: - print('Error : ') - print(e) - print('check your connection or your imap config for account '+iconf.user+' : '+iconf.password) + mprint('Error : ') + mprint(e) + mprint('check your connection or your imap config for account '+iconf.user+' : '+iconf.password) + rt.stop() def addFile(self, path, label, catg=''): """""" done = False - from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO - rt = RuTime(eval(__CALLER__()),DEBUG_INFO) + rt = RuTime(eval(__CALLER__()),DEBUG.INFO) self.switchFileAccount() @@ -800,14 +819,14 @@ class ImpraStorage: if size > 0 : md5 = hash_sha256_file(path) account = self.ih.conf.user - print() + mprint() Clz.print(' account : ' , Clz.fgn7, False) Clz.print(account , Clz.fgB7) Clz.print(' file : ' , Clz.fgn7, False) Clz.print(path , Clz.fgN1) Clz.print(' hash : ' , Clz.fgn7, False) Clz.print(md5 , Clz.fgN2) - print() + mprint() if not self.index.get(md5) : if catg=='' : catg = self.index.getAutoCatg(ext) @@ -818,10 +837,10 @@ class ImpraStorage: path = self.encryptTextFile(path) hlst = self.fsplit.addFile(path,md5) - if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : - print(hlst['head']) + if DEBUG.active and DEBUG.level <= DEBUG.NOTICE : + mprint(hlst['head']) for v in hlst['data']: - print(v) + mprint(v) usr = self.conf.get('name','infos') ownerHash = self.mb.getHashName(usr) @@ -839,21 +858,21 @@ class ImpraStorage: status, resp = self.ih.fetch(mid[1],'(UID BODYSTRUCTURE)', True) if status == self.ih.OK: sendIds.append((mid[1],row)) - print(' ',end='') + mprint(' ',end='') Clz.print('part ' , Clz.fgn7, False) Clz.print(str(row[0]) , Clz.fgB2, False) Clz.print(' sent as msg ', Clz.fgn7, False) Clz.print(str(mid[1]) , Clz.fgB1) else: - print('\n-- error occured when sending part : %s\n-- retrying' % row[0]) + mprint('\n-- error occured when sending part : %s\n-- retrying' % row[0]) if not cancel : diff = self.checkSendIds(sendIds,hlst['head'][2]) - #~ print('diff') + #~ mprint('diff') #~ for mid in diff : #~ if mid > 0: - #~ print(mid) + #~ mprint(mid) #self.ih.delete(str(mid), True, False) if len(diff) > 0 : for mid in diff : @@ -863,14 +882,15 @@ class ImpraStorage: # bugfix mid would be without +1 k = [ k for k in sendIds if len(k)>0 and int(k[0]) == int(mid+1)] if len(k) > 0 : - print(k) + mprint(k) row = k[0][1] msg = self.mb.build(usr,'all',hlst['head'][2],self.fsplit.DIR_OUTBOX+row[1]+'.ipr') - Clz.print(' resending part ' , Clz.fgn7, False) - Clz.print(str(row[0]) , Clz.fgN2) + if DEBUG.active : + Clz.print(' resending part ' , Clz.fgn7, False) + Clz.print(str(row[0]) , Clz.fgN2) mid = self.ih.send(msg.as_string(), self.rootBox) else : - print() + mprint() #Clz.print(' index intall files checked\n', Clz.fgB2) self._setIndexImap() self.index = self.getIndex(True) @@ -885,21 +905,21 @@ class ImpraStorage: self.clean() else : - print(' ',end='') + mprint(' ',end='') Clz.print(' == file already exist on server as ' , Clz.fgN7+Clz.bg1, False) Clz.print(self.index.dic[md5][ImpraIndex.LABEL] , Clz.bg1+Clz.fgB3, False) Clz.print(' [id:' , Clz.fgN7+Clz.bg1, False) Clz.print(str(self.index.dic[md5][ImpraIndex.UID]) , Clz.bg1+Clz.fgB3, False) Clz.print('] == ' , Clz.fgN7+Clz.bg1) - print() + mprint() else : - print(' ',end='') + mprint(' ',end='') Clz.print(' == files is empty or don\t exists == ' , Clz.bg1+Clz.fgN7) - print() + mprint() #~ except Exception as e : - #~ print('Erroreuh') - #~ print(e) + #~ mprint('Erroreuh') + #~ mprint(e) self._setIndexImap() rt.stop() return done @@ -907,36 +927,37 @@ class ImpraStorage: def editFile(self,key,label,category): """""" - from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO + rt = RuTime(eval(__CALLER__())) done = False row = self.index.get(key) if row==None : - print() + mprint() Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(str(key) , Clz.bg1+Clz.fgB3, False) Clz.print('` not on the server == ', Clz.bg1+Clz.fgB7) - print() + mprint() else : done = self.index.edit(key,label,category) + rt.stop() return done def removeFile(self,key): """""" - from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO done = False row = self.index.get(key) if row==None : - print() + mprint() Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(str(key) , Clz.bg1+Clz.fgB3, False) Clz.print('` not on the server == ', Clz.bg1+Clz.fgB7) - print() + mprint() else : - rt = RuTime(eval(__CALLER__('"[%i] %s"' % (row[ImpraIndex.UID],row[ImpraIndex.LABEL]))),DEBUG_INFO) + rt = RuTime(eval(__CALLER__('"[%i] %s"' % (row[ImpraIndex.UID],row[ImpraIndex.LABEL]))),DEBUG.INFO) self.switchFileAccount(row[ImpraIndex.ACCOUNT]) account = self.ih.conf.user - Clz.print(' account : ' , Clz.fgn7, False) - Clz.print(account , Clz.fgB7) + if DEBUG.active : + Clz.print(' account : ' , Clz.fgn7, False) + Clz.print(account , Clz.fgB7) ck = ConfigKey(row[ImpraIndex.HASH]) hlst = ck.getHashList(key,row[ImpraIndex.PARTS],True) Clz.print(' get file list from server', Clz.fgn7) @@ -955,18 +976,17 @@ class ImpraStorage: def getFile(self,key): """""" - from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO done = False row = self.index.get(key) if row==None : - print() - Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) - Clz.print(str(key) , Clz.bg1+Clz.fgB3, False) - Clz.print('` not on the server == ', Clz.bg1+Clz.fgB7) - print() + mprint() + Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) + Clz.print(str(key) , Clz.bg1+Clz.fgB3, False) + Clz.print('` not on the server == ', Clz.bg1+Clz.fgB7) + mprint() else : - rt = RuTime(eval(__CALLER__('"[%i] %s"' % (row[ImpraIndex.UID],row[ImpraIndex.LABEL]))),DEBUG_INFO) + rt = RuTime(eval(__CALLER__('"[%i] %s"' % (row[ImpraIndex.UID],row[ImpraIndex.LABEL]))),DEBUG.INFO) self.switchFileAccount(row[ImpraIndex.ACCOUNT]) account = self.ih.conf.user Clz.print(' account : ' , Clz.fgn7, False) @@ -978,21 +998,21 @@ class ImpraStorage: for mid in ids : self.ih.downloadAttachment(mid,self.fsplit.DIR_INBOX, True) - if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : - print(hlst['head']) + if DEBUG.active and DEBUG.level <= DEBUG.NOTICE : + mprint(hlst['head']) for v in hlst['data']: - print(v) + mprint(v) path = self.fsplit.deployFile(hlst, row[ImpraIndex.LABEL], row[ImpraIndex.EXT], row[ImpraIndex.UID], row[ImpraIndex.CATG]) if row[ImpraIndex.BFLAG] == ImpraIndex.FILE_CRYPT: self.decryptTextFile(path) - print() + mprint() Clz.print(' deploying in ', Clz.fgn7) Clz.print(' '+dirname(path), Clz.fgB2) - print() + mprint() done = True else : - print() + mprint() Clz.print(' == `' , Clz.BG3+Clz.fgB1, False) Clz.print(row[ImpraIndex.LABEL] , Clz.BG3+Clz.fgB4, False) Clz.print('` invalid count parts ' , Clz.BG3+Clz.fgB1, False) @@ -1000,7 +1020,7 @@ class ImpraStorage: Clz.print('/' , Clz.BG3+Clz.fgB1, False) Clz.print(str(row[ImpraIndex.PARTS]) , Clz.BG3+Clz.fgB4, False) Clz.print(' == ' , Clz.BG3+Clz.fgB1) - print() + mprint() self._setIndexImap() rt.stop() return done diff --git a/impra/crypt.py b/impra/crypt.py index 7e1da77..20c3be9 100644 --- a/impra/crypt.py +++ b/impra/crypt.py @@ -126,6 +126,7 @@ class ConfigKey: def __init__(self, key=None, salt=None, psize=19710000): """""" + rt = RuTime(eval(__CALLER__())) if key : self.key = bytes(key,'utf-8') else : self.key = self._build() if salt ==None : self.salt = str(self.key[::-10]) @@ -133,6 +134,7 @@ class ConfigKey: self.psize = psize self.noiser = Noiser(self.key) self.rdmz = Randomiz(1) + rt.stop() def getHashList(self,name,count,noSorted=False): """""" @@ -154,8 +156,11 @@ class ConfigKey: def _build(self,l=48): """""" + rt = RuTime(eval(__CALLER__())) kg = KeyGen(l) - return urlsafe_b64encode(bytes(kg.key,'utf-8')) + k = urlsafe_b64encode(bytes(kg.key,'utf-8')) + rt.stop() + return k def getKey(self): """""" @@ -170,29 +175,35 @@ class Kirmah: def __init__(self, key, mark): """""" + rt = RuTime(eval(__CALLER__())) self.key = bytes(key,'utf-8') self.mark = mark self.ck = ConfigKey(mark) + rt.stop() def enchr(self,o,cs,ce): """""" + #~ rt = RuTime(eval(__CALLER__())) if not self.key[cs] > self.key[ce] or not o - 2*self.key[cs] - self.key[ce] > 0: o += self.key[ce] else: o -= self.key[cs] if o == 10: o+=30 + #~ rt.stop() return o def dechr(self,o,cs,ce): """""" + #~ rt = RuTime(eval(__CALLER__())) if not self.key[cs] > self.key[ce] or self.enchr(o - self.key[ce],cs,ce)==o : o -= self.key[ce] else : o += self.key[cs] + #~ rt.stop() return o def subenc(self,data): - """""" + """""" rt = RuTime(eval(__CALLER__())) s, i, lkey, ld = '', 0, len(self.key), len(data) @@ -229,14 +240,20 @@ class Kirmah: def sign(self,data): """""" - return hash_sha256(self.mark + hash_sha256(data)) + data + rt = RuTime(eval(__CALLER__())) + sd = hash_sha256(self.mark + hash_sha256(data)) + data + rt.stop() + return sd def unsign(self,data): """""" + rt = RuTime(eval(__CALLER__())) d = data[64:] if not data[:64] == hash_sha256(self.mark + hash_sha256(d)): raise BadKeyException() - else: return d + else: + rt.stop() + return d def encrypt(self, odata, label, cpart): """""" @@ -291,19 +308,25 @@ class Randomiz: def __init__(self,count,chl=None): """""" + #~ rt = RuTime(eval(__CALLER__())) if chl ==None : self.lst = list(range(0,count)) else: self.lst = chl self.count = len(self.lst) + #~ rt.stop() def new(self,count=None, chl=None): """""" + #~ rt = RuTime(eval(__CALLER__())) if count : self.count = count self.__init__(self.count,chl) + #~ rt.stop() def get(self,single=True): """""" + #~ rt = RuTime(eval(__CALLER__())) pos = choice(self.lst) if single: del self.lst[self.lst.index(pos)] + #~ rt.stop() return pos @@ -315,23 +338,29 @@ class Noiser: def __init__(self, key, part=0): """""" + #~ rt = RuTime(eval(__CALLER__())) self.key = key self.build(part) + #~ rt.stop() def build(self, part): """""" + #~ rt = RuTime(eval(__CALLER__())) if not part < len(self.key)-1 : raise Exception('part exceed limit') else : self.part, v = part, 0 v = int(ceil((self.key[22]+v)/4.20583)) self.lns = abs(int(ceil(v/2))-self.key[self.part]+self.key[7]) self.lne = abs(int(v-self.lns-self.key[self.part+2]-self.key[44]/2.1934)) + #~ rt.stop() def getNoise(self, l, noBytes=False): """""" + #~ rt = RuTime(eval(__CALLER__())) n = urandom(l) if noBytes: n = str(urlsafe_b64encode(n),'utf-8')[0:l] + #~ rt.stop() return n diff --git a/impra/imap.py b/impra/imap.py index db0ad54..658c41e 100644 --- a/impra/imap.py +++ b/impra/imap.py @@ -37,7 +37,7 @@ from os.path import join from re import search, split from time import time, sleep -from impra.util import __CALLER__, RuTime, bstr, stack, Clz +from impra.util import __CALLER__, RuTime, bstr, stack, Clz, DEBUG, mprint # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -170,6 +170,9 @@ class ImapHelper: self.srv = IMAP4_SSL(conf.host,conf.port) self.conf = conf status, resp = self.srv.login(conf.user,conf.pwd) + if DEBUG.level <= DEBUG.ALL : + mprint(status) + mprint(resp) self.rootBox = box if boxBin is None : if search('yahoo.com',conf.host) is not None : @@ -305,27 +308,26 @@ class ImapHelper: def deleteBin(self): """""" - from impra.util import DEBUG_NOTICE, DEBUG, DEBUG_LEVEL, DEBUG_INFO - rt = RuTime(eval(__CALLER__()),DEBUG_INFO) + rt = RuTime(eval(__CALLER__()),DEBUG.INFO) self.srv.select(self.BOX_BIN) ids = self.search('ALL',True) if len(ids) > 0 and ids[0]!='' and ids[0]!=None: - print() + mprint() #print(str(ids[0],'utf-8').split()) for mid in ids: #~ uid = bytes(mid) - #~ print(type(mid)) - #~ print(mid) + #~ mprint(type(mid)) + #~ mprint(mid) #status, resp = self.srv.store(mid, '+FLAGS', '\\Deleted') status, resp = self.srv.uid('store', mid, '+FLAGS (\\Deleted)' ) - print(' ',end='') + mprint(' ',end='') Clz.print(' deleting msg ',Clz.fgN7+Clz.bg1, False) Clz.print(str(int(mid)) ,Clz.bg1+Clz.fgB3) - if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE: - print(status) - print(resp) + if DEBUG.level <= DEBUG.NOTICE: + mprint(status) + mprint(resp) self.srv.expunge() - print() + mprint() self.srv.select(self.rootBox) rt.stop() @@ -338,6 +340,7 @@ class ImapHelper: status, resp = self.srv.uid( 'store', mid, '+FLAGS (\\Deleted)' ) else : status, resp = self.srv.store(mid, '+FLAGS', 'Deleted') + Clz.print(' flag msg ' , Clz.fgn7, False) Clz.print(str(int(mid)), Clz.fgB1, False) Clz.print(' as deleted', Clz.fgn7) @@ -352,13 +355,12 @@ class ImapHelper: def downloadAttachment(self, msg, toDir='./', byUid=False): """""" - from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_INFO - rt = RuTime(eval(__CALLER__('%i' % int(msg))),DEBUG_INFO) + rt = RuTime(eval(__CALLER__('%i' % int(msg))),DEBUG.INFO) if not isinstance(msg, Message) : msg = self.email(msg,byUid) for part in msg.walk(): filename = part.get_filename() - if filename != None and DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : print(filename) + if filename != None and DEBUG.level <= DEBUG.NOTICE : mprint(filename) if part.get_content_maintype() == 'multipart' or not filename : continue fp = open(join(toDir, filename), 'wb') #print(part.get_payload(decode=True)[::-1]) @@ -368,15 +370,14 @@ class ImapHelper: def send(self, msg, box='INBOX'): """""" - from impra.util import DEBUG_INFO, DEBUG_LEVEL, DEBUG, DEBUG_NOTICE rt = RuTime(eval(__CALLER__())) mid = None date = Time2Internaldate(time()) status, resp = self.srv.append(box, '\Draft', date, bytes(msg,'utf-8')) if status==self.OK: - if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE: - print(status) - print(resp) + if DEBUG.level <= DEBUG.NOTICE: + mprint(status) + mprint(resp) m = search(b']', resp[0]) mid = str(resp[0],'utf-8')[11:-(len(resp[0])-m.start())].split(' ') rt.stop() diff --git a/impra/util.py b/impra/util.py index 8ab3347..170c44a 100644 --- a/impra/util.py +++ b/impra/util.py @@ -44,14 +44,22 @@ import platform #~ from sys.stdout import isatty from time import time -DEBUG_ALL = 0 -DEBUG_WARN = 1 -DEBUG_NOTICE = 2 -DEBUG_INFO = 3 +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# ~~ class Debug ~~ +class Debug: + + ALL = 0 + WARN = 1 + NOTICE = 2 + INFO = 3 + + def __init__(self, active=True, level=3): + """""" + self.active = active + self.level = level -DEBUG = True -DEBUG_LEVEL = DEBUG_INFO -COLOR_MODE = True +COLOR_MODE = True +DEBUG = Debug(True,Debug.INFO) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~ methods ~~ @@ -169,7 +177,7 @@ def run(cmdline): cmdout, cmderr = p.communicate() rcode = p.wait() if rcode < 0: - print((stderr,"Child was terminated by signal",rcode)) + mprint((stderr,"Child was terminated by signal",rcode)) else: return (rcode,cmdout,cmderr) except OSError as e : @@ -184,16 +192,22 @@ def __CALLER__(args=''): :Returns: `str` """ - global DEBUG_LEVEL, DEBUG, DEBUG_WARN + #global DEBUG_LEVEL, DEBUG, DEBUG_WARN val = "self.__class__.__name__+'.%s' % stack()[1][3]+'("+quote_escape(args)+") " - if DEBUG and DEBUG_LEVEL<=DEBUG_WARN : val += "l:'+str(stack()[1][2])+' ' " + if DEBUG.active and DEBUG.level<=DEBUG.WARN : val += "l:'+str(stack()[1][2])+' ' " else: val += "'" return val -if platform.system() == 'Windows' : - clear = lambda: system('cls') -else : clear = lambda: system('clear') +def mprint(d='',end='\n'): + if DEBUG.active : print(d,end=end) +if platform.system() == 'Windows' : + SYS_CLEAR = 'cls' +else : + SYS_CLEAR = 'clear' +clear = lambda: system(SYS_CLEAR) + + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~ class RuTime ~~ @@ -201,11 +215,10 @@ else : clear = lambda: system('clear') class RuTime: """Give basics time stats""" - def __init__(self,label,lvl=DEBUG_NOTICE): + def __init__(self,label,lvl=DEBUG.NOTICE): """Initialize duration with appropriate label""" - from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_INFO - self.debug = DEBUG and DEBUG_LEVEL <= lvl - self.debugStart = self.debug and lvl < DEBUG_INFO + self.debug = DEBUG.active and DEBUG.level <= lvl + self.debugStart = self.debug and lvl < DEBUG.INFO self.lvl = lvl self.label = label self._start() @@ -334,7 +347,7 @@ class IniFile: if s!='main': #~ if section=='*': content += '\n['+s+']\n' #~ else : content += '\n['+s[len(section)+1:]+']\n' - print() + mprint() if not withoutSectionName : Clz.print('['+s+']', Clz.fgB3) else: Clz.print('['+s.split('.')[1]+']', Clz.fgB3) @@ -483,19 +496,20 @@ class Coloriz: def print(self,data,colors,endLF=True,endClz=True): """""" - if not endLF : ev = '' - else: ev = self._LF - if self.active : - tokens = [c.lstrip(self._MARKER[0]).rstrip(self._SEP) for c in colors.split(self._MARKER) if c is not ''] - if self.isUnix : - if endClz : data += self._uOFF - print(eval('self._u'+'+self._u'.join(tokens))+data,end=ev) - else : - self.setColor(eval('self._w'+'|self._w'.join(tokens))) - print(data,end=ev) - stdout.flush() - if endClz : self.setColor(self._wOFF) - else: - print(data,end=ev) + if DEBUG.active: + if not endLF : ev = '' + else: ev = self._LF + if self.active : + tokens = [c.lstrip(self._MARKER[0]).rstrip(self._SEP) for c in colors.split(self._MARKER) if c is not ''] + if self.isUnix : + if endClz : data += self._uOFF + mprint(eval('self._u'+'+self._u'.join(tokens))+data,end=ev) + else : + self.setColor(eval('self._w'+'|self._w'.join(tokens))) + mprint(data,end=ev) + stdout.flush() + if endClz : self.setColor(self._wOFF) + else: + mprint(data,end=ev) Clz = Coloriz()