refact debug, adding quiet and debug mode

This commit is contained in:
a-sansara 2012-10-10 19:13:21 +02:00
parent 14424f3d3d
commit 632d0e46fb
5 changed files with 271 additions and 194 deletions

View File

@ -34,7 +34,7 @@ import sys, os, platform
import impra.crypt as crypt import impra.crypt as crypt
import impra.util as util import impra.util as util
import impra.core as core import impra.core as core
from impra.util import Clz from impra.util import Clz, mprint
LINE_SEP_LEN = 120 LINE_SEP_LEN = 120
@ -103,8 +103,9 @@ class Cli:
gpConf = OptionGroup(parser, '') gpConf = OptionGroup(parser, '')
# metavar='<ARG1> <ARG2>', nargs=2 # metavar='<ARG1> <ARG2>', 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('-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('-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 ') 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() (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: if not a:
try : try :
if not o.help : if not o.help :
self.parserError(' no commando specified') self.parserError(' no commando specified')
else : else :
core.clear() if util.DEBUG.active : core.clear()
parser.print_help() parser.print_help()
except : except :
self.parserError(' no commando specified') self.parserError(' no commando specified')
@ -149,7 +162,7 @@ class Cli:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ conf CMD ~~ # ~~ conf CMD ~~
if a[0] == 'conf' : 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.load is not None or o.view is not None or o.save is not None :
if o.view is not None : if o.view is not None :
@ -181,7 +194,7 @@ class Cli:
else : colr = Clz.fgB3 else : colr = Clz.fgB3
Clz.print(sep+p, colr, False) Clz.print(sep+p, colr, False)
if sep=='':sep=',' if sep=='':sep=','
print() mprint()
else : Clz.print(' no profiles', Clz.fgB1) else : Clz.print(' no profiles', Clz.fgB1)
else: self.ini.print(o.view) else: self.ini.print(o.view)
@ -225,7 +238,7 @@ class Cli:
self.print_usage('') self.print_usage('')
elif a[0] == 'help': elif a[0] == 'help':
core.clear() if util.DEBUG.active : core.clear()
parser.print_help() parser.print_help()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -236,14 +249,14 @@ class Cli:
if self.check_profile(o.active_profile): if self.check_profile(o.active_profile):
core.clear() if util.DEBUG.active : core.clear()
self.print_header() if util.DEBUG.active: self.print_header()
conf = core.ImpraConf(self.ini,o.active_profile) conf = core.ImpraConf(self.ini,o.active_profile)
impst = None impst = None
try: try:
impst = core.ImpraStorage(conf, False, self.wkpath) impst = core.ImpraStorage(conf, False, self.wkpath)
except crypt.BadKeyException as e : except crypt.BadKeyException as e :
print() mprint()
Clz.print(' it seems that your current profile `' , Clz.fgB1, False) Clz.print(' it seems that your current profile `' , Clz.fgB1, False)
Clz.print(o.active_profile , Clz.fgB3, False) Clz.print(o.active_profile , Clz.fgB3, False)
Clz.print('` (account:`' , Clz.fgB1, False) Clz.print('` (account:`' , Clz.fgB1, False)
@ -266,17 +279,17 @@ class Cli:
resp = input(' remove index ? (yes/no) ') resp = input(' remove index ? (yes/no) ')
if resp.lower()=='yes': if resp.lower()=='yes':
impst = core.ImpraStorage(conf, True, self.wkpath) impst = core.ImpraStorage(conf, True, self.wkpath)
print() mprint()
print(' bye') mprint(' bye')
Clz.print(' ',Clz.OFF) Clz.print(' ',Clz.OFF)
print() mprint()
self.exit(1) self.exit(1)
else : else :
print() mprint()
print(' bye') mprint(' bye')
Clz.print(' ',Clz.OFF) Clz.print(' ',Clz.OFF)
print() mprint()
self.exit(1) self.exit(1)
@ -289,7 +302,7 @@ class Cli:
noData = impst.index.isEmpty() noData = impst.index.isEmpty()
if uid == None or noData : uid = 'EMPTY' if uid == None or noData : uid = 'EMPTY'
if date == None or noData : date = '' if date == None or noData : date = ''
core.clear() if util.DEBUG.active : core.clear()
printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN)
printHeaderTitle(APP_TITLE) printHeaderTitle(APP_TITLE)
printHeaderPart('account',account) printHeaderPart('account',account)
@ -302,7 +315,7 @@ class Cli:
matchIdsUser = None matchIdsUser = None
matchIds = None matchIds = None
if o.category is not None : if o.category is not None :
print(o.category) mprint(o.category)
matchIdsCatg = impst.index.getByCategory(o.category) matchIdsCatg = impst.index.getByCategory(o.category)
if o.user is not None : if o.user is not None :
matchIdsUser = impst.index.getByUser(o.user) matchIdsUser = impst.index.getByUser(o.user)
@ -332,9 +345,9 @@ class Cli:
if o.category is None : o.category = '' if o.category is None : o.category = ''
done = impst.addFile(vfile,label,o.category) done = impst.addFile(vfile,label,o.category)
if done : if done :
print('\n ',end='') mprint('\n ',end='')
Clz.print(' == OK == ', Clz.bg2+Clz.fgb7) Clz.print(' == OK == ', Clz.bg2+Clz.fgb7)
print() mprint()
else : self.error_cmd('`'+a[1]+' is not a file',parser) 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) if not len(a)>1 : self.error_cmd('`'+a[0]+' need an id',parser)
else: else:
if not util.represents_int(a[1]): if not util.represents_int(a[1]):
print('\n ',end='') mprint('\n ',end='')
Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(' == `' , Clz.bg1+Clz.fgB7, False)
Clz.print(a[1] , Clz.bg1+Clz.fgB3, False) Clz.print(a[1] , Clz.bg1+Clz.fgB3, False)
Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7)
print() mprint()
self.exit(1) self.exit(1)
else : else :
vid = a[1] vid = a[1]
@ -355,21 +368,21 @@ class Cli:
done = impst.editFile(key,o.label,o.category) done = impst.editFile(key,o.label,o.category)
if done : if done :
impst.saveIndex() impst.saveIndex()
print('\n ',end='') mprint('\n ',end='')
Clz.print(' == OK == ', Clz.bg2+Clz.fgb7) Clz.print(' == OK == ', Clz.bg2+Clz.fgb7)
print() mprint()
else : else :
print('\n ',end='') mprint('\n ',end='')
Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(' == `' , Clz.bg1+Clz.fgB7, False)
Clz.print(a[1] , Clz.bg1+Clz.fgB3, False) Clz.print(a[1] , Clz.bg1+Clz.fgB3, False)
Clz.print('` has not been modified == ', Clz.bg1+Clz.fgB7) Clz.print('` has not been modified == ', Clz.bg1+Clz.fgB7)
print() mprint()
else: else:
print('\n ',end='') mprint('\n ',end='')
Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(' == `' , Clz.bg1+Clz.fgB7, False)
Clz.print(a[1] , Clz.bg1+Clz.fgB3, False) Clz.print(a[1] , Clz.bg1+Clz.fgB3, False)
Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7)
print() mprint()
elif a[0] == 'get': elif a[0] == 'get':
@ -386,15 +399,15 @@ class Cli:
if key !=None : if key !=None :
done = impst.getFile(key) done = impst.getFile(key)
if done : if done :
print('\n ',end='') mprint('\n ',end='')
Clz.print(' == OK == ', Clz.bg2+Clz.fgb7) Clz.print(' == OK == ', Clz.bg2+Clz.fgb7)
print() mprint()
else: else:
print('\n ',end='') mprint('\n ',end='')
Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(' == `' , Clz.bg1+Clz.fgB7, False)
Clz.print(str(sid) , Clz.bg1+Clz.fgB3, False) Clz.print(str(sid) , Clz.bg1+Clz.fgB3, False)
Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7)
print() mprint()
elif a[0] == 'search': elif a[0] == 'search':
@ -409,7 +422,7 @@ class Cli:
vsearch = a[1] vsearch = a[1]
matchIds = impst.index.getByPattern(vsearch) matchIds = impst.index.getByPattern(vsearch)
core.clear() if util.DEBUG.active : core.clear()
if matchIds is not None: if matchIds is not None:
printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN)
printHeaderTitle(APP_TITLE) printHeaderTitle(APP_TITLE)
@ -429,7 +442,7 @@ class Cli:
matchIdsUser = None matchIdsUser = None
matchIdsCrit = None matchIdsCrit = None
if o.category is not None : if o.category is not None :
print(o.category) mprint(o.category)
matchIdsCatg = impst.index.getByCategory(o.category) matchIdsCatg = impst.index.getByCategory(o.category)
if o.user is not None : if o.user is not None :
matchIdsUser = impst.index.getByUser(o.user) matchIdsUser = impst.index.getByUser(o.user)
@ -450,11 +463,11 @@ class Cli:
order = '-'+o.order_inv order = '-'+o.order_inv
impst.index.print(o.order,matchIds) impst.index.print(o.order,matchIds)
else: else:
print('\n ',end='') mprint('\n ',end='')
Clz.print(' == no match found for pattern `', Clz.bg3+Clz.fgB4, False) Clz.print(' == no match found for pattern `', Clz.bg3+Clz.fgB4, False)
Clz.print(vsearch , Clz.bg3+Clz.fgB1, False) Clz.print(vsearch , Clz.bg3+Clz.fgB1, False)
Clz.print('` == ' , Clz.bg3+Clz.fgB4) Clz.print('` == ' , Clz.bg3+Clz.fgB4)
print() mprint()
elif a[0] == 'remove': elif a[0] == 'remove':
@ -462,11 +475,11 @@ class Cli:
else : else :
if not util.represents_int(a[1]): if not util.represents_int(a[1]):
print('\n ',end='') mprint('\n ',end='')
Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(' == `' , Clz.bg1+Clz.fgB7, False)
Clz.print(a[1] , Clz.bg1+Clz.fgB3, False) Clz.print(a[1] , Clz.bg1+Clz.fgB3, False)
Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7)
print() mprint()
self.exit(1) self.exit(1)
else : else :
vid = a[1] vid = a[1]
@ -474,22 +487,22 @@ class Cli:
if key !=None : if key !=None :
done = impst.removeFile(key) done = impst.removeFile(key)
if done : if done :
print('\n ',end='') mprint('\n ',end='')
Clz.print(' == OK == ', Clz.bg2+Clz.fgb7) Clz.print(' == OK == ', Clz.bg2+Clz.fgb7)
print() mprint()
else: else:
print('\n ',end='') mprint('\n ',end='')
Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(' == `' , Clz.bg1+Clz.fgB7, False)
Clz.print(a[1] , Clz.bg1+Clz.fgB3, False) Clz.print(a[1] , Clz.bg1+Clz.fgB3, False)
Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7)
print() mprint()
else : else :
self.check_profile(o.active_profile, True) self.check_profile(o.active_profile, True)
else : else :
if self.ini.isEmpty() : if self.ini.isEmpty() :
Clz.print(' '*4+'ImpraStorage has no configuration file !!', Clz.fgB1) 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+'# 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+'# type command help for details',Clz.fgn7)
Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) Clz.print(' '*8+'imprastorage ', Clz.fgB7, False)
@ -508,17 +521,17 @@ class Cli:
Clz.print('accountPassword ', Clz.fgB1) Clz.print('accountPassword ', Clz.fgB1)
else : else :
self.error_cmd('unknow command `'+a[0]+'`',parser) self.error_cmd('unknow command `'+a[0]+'`',parser)
print() mprint()
def error_cmd(self,msg, parser): def error_cmd(self,msg, parser):
core.clear() if util.DEBUG.active : core.clear()
self.print_usage('') self.print_usage('')
Clz.print('error : '+msg,Clz.fgB7) Clz.print('error : '+msg,Clz.fgB7)
self.exit(1) self.exit(1)
def parserError(self, msg): def parserError(self, msg):
core.clear() if util.DEBUG.active : core.clear()
self.print_usage('') self.print_usage('')
Clz.print('error : '+msg,Clz.fgB7) Clz.print('error : '+msg,Clz.fgB7)
self.exit(1) self.exit(1)
@ -569,7 +582,7 @@ class Cli:
printHeaderPart('copyright','2012 '+APP_COPY) printHeaderPart('copyright','2012 '+APP_COPY)
Clz.print(' ', Clz.OFF) Clz.print(' ', Clz.OFF)
printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN)
print() mprint()
def print_version(self, data): def print_version(self, data):
self.print_header() self.print_header()
@ -727,7 +740,7 @@ class Cli:
Clz.print(' ]', Clz.fgB3) Clz.print(' ]', Clz.fgB3)
def print_options(self): def print_options(self):
print('\n') mprint('\n')
printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN)
Clz.print(' MAIN OPTIONS :\n' , Clz.fgB3) Clz.print(' MAIN OPTIONS :\n' , Clz.fgB3)
Clz.print(' '*4+'-h, --help' , 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(' '*50+'don\'t print status messages to stdout' , Clz.fgB7)
Clz.print(' '*4+'-d, --debug' , Clz.fgB3) Clz.print(' '*4+'-d, --debug' , Clz.fgB3)
Clz.print(' '*50+'set debug mode' , Clz.fgB7) Clz.print(' '*50+'set debug mode' , Clz.fgB7)
print('\n') mprint('\n')
Clz.print(' COMMANDS OPTIONS :\n' , Clz.fgB3) Clz.print(' COMMANDS OPTIONS :\n' , Clz.fgB3)
Clz.print(' '*4+'-c ' , Clz.fgB3, False) Clz.print(' '*4+'-c ' , Clz.fgB3, False)
@ -769,7 +782,7 @@ class Cli:
Clz.print('COLON'.ljust(10,' ') , Clz.fgB1) Clz.print('COLON'.ljust(10,' ') , Clz.fgB1)
Clz.print(' '*50+'reverse order by specified colon' , Clz.fgB7) Clz.print(' '*50+'reverse order by specified colon' , Clz.fgB7)
print('\n') mprint('\n')
Clz.print(' CONF OPTIONS :\n', Clz.fgB3) Clz.print(' CONF OPTIONS :\n', Clz.fgB3)
Clz.print(' '*4+'-L ' , Clz.fgB3, False) Clz.print(' '*4+'-L ' , Clz.fgB3, False)
Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1, False) Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1, False)
@ -795,7 +808,7 @@ class Cli:
Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1) Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1)
Clz.print(' '*50+'save the specified profile' , Clz.fgB7) Clz.print(' '*50+'save the specified profile' , Clz.fgB7)
print('\n') mprint('\n')
Clz.print(' CONF -S OPTIONS :\n', Clz.fgB3) Clz.print(' CONF -S OPTIONS :\n', Clz.fgB3)
Clz.print(' '*4+'-N ' , Clz.fgB3, False) Clz.print(' '*4+'-N ' , Clz.fgB3, False)
Clz.print('NAME'.ljust(10,' ') , Clz.fgB1, False) Clz.print('NAME'.ljust(10,' ') , Clz.fgB1, False)
@ -851,7 +864,7 @@ class Cli:
Clz.print('USER'.ljust(10,' ') , Clz.fgB1) Clz.print('USER'.ljust(10,' ') , Clz.fgB1)
Clz.print(' '*50+'remove imap multi account' , Clz.fgB7) Clz.print(' '*50+'remove imap multi account' , Clz.fgB7)
print('\n') mprint('\n')
def print_help(self): def print_help(self):
@ -861,7 +874,7 @@ class Cli:
self.print_usage('',True) self.print_usage('',True)
self.print_options() self.print_options()
printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN)
print() mprint()
Clz.print(' EXEMPLES :\n', Clz.fgB3) Clz.print(' EXEMPLES :\n', Clz.fgB3)
CHQ = "'" CHQ = "'"
sep = core.sep sep = core.sep
@ -1115,17 +1128,17 @@ class Cli:
Clz.print('bob23', Clz.fgB1) Clz.print('bob23', Clz.fgB1)
printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN)
print() mprint()
def load_profile(self,o): def load_profile(self,o):
"""""" """"""
if self.check_profile(o.active_profile): if self.check_profile(o.active_profile):
print('',end=' ') mprint('',end=' ')
Clz.print(' == profile `', Clz.bg2+Clz.fgb7, False) Clz.print(' == profile `', Clz.bg2+Clz.fgb7, False)
Clz.print(o.active_profile, Clz.bg2+Clz.fgB3, False) Clz.print(o.active_profile, Clz.bg2+Clz.fgB3, False)
Clz.print('` loaded == ', Clz.bg2+Clz.fgb7) Clz.print('` loaded == ', Clz.bg2+Clz.fgb7)
print() mprint()
self.ini.set('profile', o.active_profile) self.ini.set('profile', o.active_profile)
self.ini.write() self.ini.write()
else : else :

View File

@ -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 re import split as regsplit, match as regmatch, compile as regcompile, search as regsearch
from time import time, sleep from time import time, sleep
from impra.imap import ImapHelper, ImapConfig 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 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='./'): def __init__(self, ck, wkdir='./'):
"""""" """"""
rt = RuTime(eval(__CALLER__()))
self.ck = ck self.ck = ck
self.wkdir = wkdir self.wkdir = wkdir
self.DIR_CACHE = join(self.wkdir,'.cache')+sep self.DIR_CACHE = join(self.wkdir,'.cache')+sep
self.DIR_INBOX = join(self.wkdir,'inbox')+sep self.DIR_INBOX = join(self.wkdir,'inbox')+sep
self.DIR_OUTBOX = join(self.wkdir,'outbox')+sep self.DIR_OUTBOX = join(self.wkdir,'outbox')+sep
self.DIR_DEPLOY = join(self.wkdir,'deploy')+sep self.DIR_DEPLOY = join(self.wkdir,'deploy')+sep
rt.stop()
def addFile(self, fromPath, label, fixCount = False): def addFile(self, fromPath, label, fixCount = False):
"""""" """"""
@ -87,8 +89,7 @@ class FSplitter :
def _split(self, fromPath, hlst): 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+') f = open(fromPath, 'rb+')
m = mmap(f.fileno(), 0) m = mmap(f.fileno(), 0)
p = 0 p = 0
@ -112,8 +113,8 @@ class FSplitter :
"""""" """"""
rt = RuTime(eval(__CALLER__('mmap,%s,%s,phlist' % (part,size)))) rt = RuTime(eval(__CALLER__('mmap,%s,%s,phlist' % (part,size))))
with open(self.DIR_OUTBOX+phlst[1]+'.ipr', mode='wb') as o: with open(self.DIR_OUTBOX+phlst[1]+'.ipr', mode='wb') as o:
#~ print(self.DIR_OUTBOX+phlst[1]+'.ipr') #~ mprint(self.DIR_OUTBOX+phlst[1]+'.ipr')
#~ print(str(phlst[2])+' - '+str(size)+' - '+str(phlst[3])+' = '+str(phlst[2]+size+phlst[3])) #~ 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])) o.write(self.ck.noiser.getNoise(phlst[2])+mmap.read(size)+self.ck.noiser.getNoise(phlst[3]))
rt.stop() rt.stop()
@ -278,6 +279,7 @@ class ImpraIndex:
initial content of the index encrypted with Kirmah Algorythm initial content of the index encrypted with Kirmah Algorythm
and representing a dic index as json string and representing a dic index as json string
""" """
rt = RuTime(eval(__CALLER__()))
self.km = Kirmah(key, mark) self.km = Kirmah(key, mark)
self.dic = {} self.dic = {}
if encdata =='' : if encdata =='' :
@ -298,15 +300,18 @@ class ImpraIndex:
else : else :
if not self.SEP_KEY_INTERN+k in self.dic: if not self.SEP_KEY_INTERN+k in self.dic:
self.dic[self.SEP_KEY_INTERN+k] = dicCategory[k] 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=''): def add(self,key, label, count, ext='', usr='', cat='', md5='', bFlag='b', size='', account=''):
"""Add an entry to the index """Add an entry to the index
""" """
rt = RuTime(eval(__CALLER__()))
if self.get(md5) == None : if self.get(md5) == None :
self.dic[md5] = (key,label,count,ext,usr,cat,self.id,bFlag,size,account) self.dic[md5] = (key,label,count,ext,usr,cat,self.id,bFlag,size,account)
self.id +=1 self.id +=1
else : else :
print(label+' already exist') mprint(label+' already exist')
rt.stop()
def addUser(self, nameFrom, hashName): def addUser(self, nameFrom, hashName):
"""""" """"""
@ -371,6 +376,7 @@ class ImpraIndex:
"""Get the corresponding key in the index """Get the corresponding key in the index
:Returns: `tuple` row :Returns: `tuple` row
""" """
rt = RuTime(eval(__CALLER__()))
done = False done = False
row = self.dic[key] row = self.dic[key]
r = list(row) r = list(row)
@ -380,6 +386,7 @@ class ImpraIndex:
r[self.CATG] = category r[self.CATG] = category
self.dic[key] = tuple(r) self.dic[key] = tuple(r)
done = row != self.dic[key] done = row != self.dic[key]
rt.stop()
return done return done
def getById(self,sid): def getById(self,sid):
@ -395,14 +402,17 @@ class ImpraIndex:
def fixAccount(self,account): 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)] r = [k for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)]
for k in r: for k in r:
t = list(self.dic[k]) t = list(self.dic[k])
t[self.ACCOUNT] = account t[self.ACCOUNT] = account
self.dic[k] = tuple(t) self.dic[k] = tuple(t)
rt.stop()
def getLightestAccount(self,l): 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)] r = [k for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)]
t = {} t = {}
for k in r: for k in r:
@ -418,6 +428,7 @@ class ImpraIndex:
if account is None : if account is None :
d = sorted(r, reverse=False, key=lambda lst:lst[0]) d = sorted(r, reverse=False, key=lambda lst:lst[0])
account = d[0][1] account = d[0][1]
rt.stop()
return account return account
def fixDuplicateIds(self): 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)] d = [k[0] for k in l if any( k[1] == v for v in l3)]
for k in d: for k in d:
mxid += 1 mxid += 1
print(self.dic[k]) #mprint(self.dic[k])
t = list(self.dic[k]) t = list(self.dic[k])
t[self.UID] = mxid t[self.UID] = mxid
print(t) #mprint(t)
self.dic[k] = tuple(t) self.dic[k] = tuple(t)
self.id = mxid+1 self.id = mxid+1
else: else:
@ -502,14 +513,14 @@ class ImpraIndex:
def encrypt(self): def encrypt(self):
"""""" """"""
#~ print('encrypting index :') #~ mprint('encrypting index :')
jdata = jdumps(self.dic) jdata = jdumps(self.dic)
#~ print(jdata) #~ mprint(jdata)
return self.km.encrypt(jdata,'.index',22) return self.km.encrypt(jdata,'.index',22)
def decrypt(self,data): def decrypt(self,data):
"""""" """"""
#~ print('decrypting index : ') #~ mprint('decrypting index : ')
try : try :
jdata = self.km.decrypt(data,'.index',22) jdata = self.km.decrypt(data,'.index',22)
data = jloads(jdata) data = jloads(jdata)
@ -532,18 +543,20 @@ class ImpraIndex:
if sizeid < 3 : sizeid = 3 if sizeid < 3 : sizeid = 3
sizeid = 3 sizeid = 3
addsize = abs(3 - sizeid); addsize = abs(3 - sizeid);
Clz.print(' ID'+' '*(1+addsize), Clz.BG4+Clz.fgB7, False, False) Clz.print(' ID'+' '*(1+addsize), Clz.BG4+Clz.fgB7, False, False)
print('HASH' +' '*6 , end='') mprint('HASH' +' '*6 , end='')
print('LABEL' +' '*(35), end='') mprint('LABEL' +' '*(35), end='')
print('SIZE' +' '*5 , end='') mprint('SIZE' +' '*5 , end='')
print('PART' +' '*2 , end='') mprint('PART' +' '*2 , end='')
print('TYPE' +' '*2 , end='') mprint('TYPE' +' '*2 , end='')
print('USER' +' '*11, end='') mprint('USER' +' '*11, end='')
print('CATEGORY'+' '*(22-addsize)) mprint('CATEGORY'+' '*(22-addsize))
#print('CATEGORY'+' '*(22-addsize), end='') #mprint('CATEGORY'+' '*(22-addsize), end='')
#Clz.print('ACCOUNT'+' '*(3), Clz.BG4+Clz.fgB7) #Clz.print('ACCOUNT'+' '*(3), Clz.BG4+Clz.fgB7)
printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN)
dbg = DEBUG.active
DEBUG.active = True
a = '' a = ''
tsize = 0 tsize = 0
psize = 0 psize = 0
@ -566,11 +579,12 @@ class ImpraIndex:
#~ if v[self.ACCOUNT] in acc : #~ if v[self.ACCOUNT] in acc :
#~ acc[v[self.ACCOUNT]] += int(v[self.SIZE]) #~ acc[v[self.ACCOUNT]] += int(v[self.SIZE])
#~ else : 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]) psize += int(v[self.SIZE])
tsize += int(v[self.SIZE]) tsize += int(v[self.SIZE])
if len(d)==0: if len(d)==0:
Clz.print(' empty', Clz.fgB1) Clz.print(' empty', Clz.fgB1)
DEBUG.active = dbg
printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN)
c = Clz.fgB2 c = Clz.fgB2
if psize != tsize : c = Clz.fgB7 if psize != tsize : c = Clz.fgB7
@ -579,7 +593,7 @@ class ImpraIndex:
if psize != tsize : if psize != tsize :
Clz.print(' / ', Clz.fgB3, False) Clz.print(' / ', Clz.fgB3, False)
Clz.print(formatBytes(int(tsize)), Clz.fgB2, False) Clz.print(formatBytes(int(tsize)), Clz.fgB2, False)
print() mprint()
printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN)
#~ Clz.print(' '*4+'[', Clz.fgB7, False) #~ Clz.print(' '*4+'[', Clz.fgB7, False)
#~ sep = '' #~ sep = ''
@ -590,7 +604,8 @@ class ImpraIndex:
#~ Clz.print(formatBytes(acc[k]),Clz.fgB2,False) #~ Clz.print(formatBytes(acc[k]),Clz.fgB2,False)
#~ if sep=='':sep = ',' #~ if sep=='':sep = ','
#~ Clz.print(']', Clz.fgB7, False) #~ Clz.print(']', Clz.fgB7, False)
#~ print() #~ mprint()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ class ImpraStorage ~~ # ~~ class ImpraStorage ~~
@ -600,8 +615,7 @@ class ImpraStorage:
def __init__(self, conf, remIndex=False, wkdir=None): 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')) if wkdir == None : wkdir = abspath(join(dirname( __file__ ), '..', 'wk'))
self.wkdir = wkdir self.wkdir = wkdir
self.conf = conf self.conf = conf
@ -625,9 +639,9 @@ class ImpraStorage:
if self.ih is None or self.ih.conf.user != iconf.user : if self.ih is None or self.ih.conf.user != iconf.user :
self.ih = ImapHelper(iconf,self.rootBox) self.ih = ImapHelper(iconf,self.rootBox)
except Exception as e: except Exception as e:
print('Error :') mprint('Error :')
print(e) mprint(e)
print('check your connection or your imap config') mprint('check your connection or your imap config')
def _getIdIndex(self): def _getIdIndex(self):
"""""" """"""
@ -669,8 +683,7 @@ class ImpraStorage:
def getIndex(self, forceRefresh=False): 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 index = None
encData = '' encData = ''
uid = self.conf.get('uid' ,'index') uid = self.conf.get('uid' ,'index')
@ -706,6 +719,7 @@ class ImpraStorage:
def removeIndex(self): def removeIndex(self):
"""""" """"""
rt = RuTime(eval(__CALLER__()))
self._getIdIndex() self._getIdIndex()
if self.idx : if self.idx :
self.ih.delete(self.idx, True) self.ih.delete(self.idx, True)
@ -713,17 +727,17 @@ class ImpraStorage:
self.conf.rem('*','index') self.conf.rem('*','index')
self.idx = None self.idx = None
remove(self.pathInd) remove(self.pathInd)
rt.stop()
def saveIndex(self): 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: try:
if self.idx != None : if self.idx != None :
self.ih.delete(self.idx, True) self.ih.delete(self.idx, True)
except Exception as e : except Exception as e :
print('error : ') mprint('error : ')
print(e) mprint(e)
#~ if len(self.delids) > 0 : #~ if len(self.delids) > 0 :
#~ for i in self.delids : self.ih.delete(i, True, False) #~ for i in self.delids : self.ih.delete(i, True, False)
#~ Clz.print('\n expunge, waiting server...\n', Clz.fgB1) #~ Clz.print('\n expunge, waiting server...\n', Clz.fgB1)
@ -732,7 +746,7 @@ class ImpraStorage:
self.index.fixDuplicateIds() self.index.fixDuplicateIds()
encData = self.index.encrypt() encData = self.index.encrypt()
msgIndex = self.mb.buildIndex(encData) 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) ids = self.ih.send(msgIndex.as_string(), self.rootBox)
date = self.ih.headerField('date', ids[1], True) date = self.ih.headerField('date', ids[1], True)
self.conf.set('uid',ids[1],'index') self.conf.set('uid',ids[1],'index')
@ -746,16 +760,20 @@ class ImpraStorage:
def encryptTextFile(self,path): def encryptTextFile(self,path):
"""""" """"""
rt = RuTime(eval(__CALLER__()))
cdata = self.index.km.subenc(get_file_content(path)) cdata = self.index.km.subenc(get_file_content(path))
with open(self.fsplit.DIR_CACHE+'.~KirmahEnc', mode='w') as o: with open(self.fsplit.DIR_CACHE+'.~KirmahEnc', mode='w') as o:
o.write(cdata) o.write(cdata)
rt.stop()
return self.fsplit.DIR_CACHE+'.~KirmahEnc' return self.fsplit.DIR_CACHE+'.~KirmahEnc'
def decryptTextFile(self,path): def decryptTextFile(self,path):
"""""" """"""
rt = RuTime(eval(__CALLER__()))
data = self.index.km.subdec(get_file_content(path)) data = self.index.km.subdec(get_file_content(path))
with open(path, mode='w') as o: with open(path, mode='w') as o:
o.write(data) o.write(data)
rt.stop()
def checkSendIds(self,sendIds,subject): def checkSendIds(self,sendIds,subject):
"""""" """"""
@ -765,6 +783,7 @@ class ImpraStorage:
def switchFileAccount(self,account=None): def switchFileAccount(self,account=None):
"""""" """"""
rt = RuTime(eval(__CALLER__()))
al = self.conf.get('multi','imap') al = self.conf.get('multi','imap')
if al is not None : if al is not None :
al = eval(al) al = eval(al)
@ -782,15 +801,15 @@ class ImpraStorage:
iconf.pwd = al[account] iconf.pwd = al[account]
self.ih = ImapHelper(iconf,self.rootBox) self.ih = ImapHelper(iconf,self.rootBox)
except Exception as e: except Exception as e:
print('Error : ') mprint('Error : ')
print(e) mprint(e)
print('check your connection or your imap config for account '+iconf.user+' : '+iconf.password) mprint('check your connection or your imap config for account '+iconf.user+' : '+iconf.password)
rt.stop()
def addFile(self, path, label, catg=''): def addFile(self, path, label, catg=''):
"""""" """"""
done = False 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() self.switchFileAccount()
@ -800,14 +819,14 @@ class ImpraStorage:
if size > 0 : if size > 0 :
md5 = hash_sha256_file(path) md5 = hash_sha256_file(path)
account = self.ih.conf.user account = self.ih.conf.user
print() mprint()
Clz.print(' account : ' , Clz.fgn7, False) Clz.print(' account : ' , Clz.fgn7, False)
Clz.print(account , Clz.fgB7) Clz.print(account , Clz.fgB7)
Clz.print(' file : ' , Clz.fgn7, False) Clz.print(' file : ' , Clz.fgn7, False)
Clz.print(path , Clz.fgN1) Clz.print(path , Clz.fgN1)
Clz.print(' hash : ' , Clz.fgn7, False) Clz.print(' hash : ' , Clz.fgn7, False)
Clz.print(md5 , Clz.fgN2) Clz.print(md5 , Clz.fgN2)
print() mprint()
if not self.index.get(md5) : if not self.index.get(md5) :
if catg=='' : catg = self.index.getAutoCatg(ext) if catg=='' : catg = self.index.getAutoCatg(ext)
@ -818,10 +837,10 @@ class ImpraStorage:
path = self.encryptTextFile(path) path = self.encryptTextFile(path)
hlst = self.fsplit.addFile(path,md5) hlst = self.fsplit.addFile(path,md5)
if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : if DEBUG.active and DEBUG.level <= DEBUG.NOTICE :
print(hlst['head']) mprint(hlst['head'])
for v in hlst['data']: for v in hlst['data']:
print(v) mprint(v)
usr = self.conf.get('name','infos') usr = self.conf.get('name','infos')
ownerHash = self.mb.getHashName(usr) ownerHash = self.mb.getHashName(usr)
@ -839,21 +858,21 @@ class ImpraStorage:
status, resp = self.ih.fetch(mid[1],'(UID BODYSTRUCTURE)', True) status, resp = self.ih.fetch(mid[1],'(UID BODYSTRUCTURE)', True)
if status == self.ih.OK: if status == self.ih.OK:
sendIds.append((mid[1],row)) sendIds.append((mid[1],row))
print(' ',end='') mprint(' ',end='')
Clz.print('part ' , Clz.fgn7, False) Clz.print('part ' , Clz.fgn7, False)
Clz.print(str(row[0]) , Clz.fgB2, False) Clz.print(str(row[0]) , Clz.fgB2, False)
Clz.print(' sent as msg ', Clz.fgn7, False) Clz.print(' sent as msg ', Clz.fgn7, False)
Clz.print(str(mid[1]) , Clz.fgB1) Clz.print(str(mid[1]) , Clz.fgB1)
else: 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 : if not cancel :
diff = self.checkSendIds(sendIds,hlst['head'][2]) diff = self.checkSendIds(sendIds,hlst['head'][2])
#~ print('diff') #~ mprint('diff')
#~ for mid in diff : #~ for mid in diff :
#~ if mid > 0: #~ if mid > 0:
#~ print(mid) #~ mprint(mid)
#self.ih.delete(str(mid), True, False) #self.ih.delete(str(mid), True, False)
if len(diff) > 0 : if len(diff) > 0 :
for mid in diff : for mid in diff :
@ -863,14 +882,15 @@ class ImpraStorage:
# bugfix mid would be without +1 # bugfix mid would be without +1
k = [ k for k in sendIds if len(k)>0 and int(k[0]) == int(mid+1)] k = [ k for k in sendIds if len(k)>0 and int(k[0]) == int(mid+1)]
if len(k) > 0 : if len(k) > 0 :
print(k) mprint(k)
row = k[0][1] row = k[0][1]
msg = self.mb.build(usr,'all',hlst['head'][2],self.fsplit.DIR_OUTBOX+row[1]+'.ipr') msg = self.mb.build(usr,'all',hlst['head'][2],self.fsplit.DIR_OUTBOX+row[1]+'.ipr')
if DEBUG.active :
Clz.print(' resending part ' , Clz.fgn7, False) Clz.print(' resending part ' , Clz.fgn7, False)
Clz.print(str(row[0]) , Clz.fgN2) Clz.print(str(row[0]) , Clz.fgN2)
mid = self.ih.send(msg.as_string(), self.rootBox) mid = self.ih.send(msg.as_string(), self.rootBox)
else : else :
print() mprint()
#Clz.print(' index intall files checked\n', Clz.fgB2) #Clz.print(' index intall files checked\n', Clz.fgB2)
self._setIndexImap() self._setIndexImap()
self.index = self.getIndex(True) self.index = self.getIndex(True)
@ -885,21 +905,21 @@ class ImpraStorage:
self.clean() self.clean()
else : else :
print(' ',end='') mprint(' ',end='')
Clz.print(' == file already exist on server as ' , Clz.fgN7+Clz.bg1, False) 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(self.index.dic[md5][ImpraIndex.LABEL] , Clz.bg1+Clz.fgB3, False)
Clz.print(' [id:' , Clz.fgN7+Clz.bg1, 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(str(self.index.dic[md5][ImpraIndex.UID]) , Clz.bg1+Clz.fgB3, False)
Clz.print('] == ' , Clz.fgN7+Clz.bg1) Clz.print('] == ' , Clz.fgN7+Clz.bg1)
print() mprint()
else : else :
print(' ',end='') mprint(' ',end='')
Clz.print(' == files is empty or don\t exists == ' , Clz.bg1+Clz.fgN7) Clz.print(' == files is empty or don\t exists == ' , Clz.bg1+Clz.fgN7)
print() mprint()
#~ except Exception as e : #~ except Exception as e :
#~ print('Erroreuh') #~ mprint('Erroreuh')
#~ print(e) #~ mprint(e)
self._setIndexImap() self._setIndexImap()
rt.stop() rt.stop()
return done return done
@ -907,34 +927,35 @@ class ImpraStorage:
def editFile(self,key,label,category): 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 done = False
row = self.index.get(key) row = self.index.get(key)
if row==None : if row==None :
print() mprint()
Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(' == `' , Clz.bg1+Clz.fgB7, False)
Clz.print(str(key) , Clz.bg1+Clz.fgB3, False) Clz.print(str(key) , Clz.bg1+Clz.fgB3, False)
Clz.print('` not on the server == ', Clz.bg1+Clz.fgB7) Clz.print('` not on the server == ', Clz.bg1+Clz.fgB7)
print() mprint()
else : else :
done = self.index.edit(key,label,category) done = self.index.edit(key,label,category)
rt.stop()
return done return done
def removeFile(self,key): def removeFile(self,key):
"""""" """"""
from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO
done = False done = False
row = self.index.get(key) row = self.index.get(key)
if row==None : if row==None :
print() mprint()
Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(' == `' , Clz.bg1+Clz.fgB7, False)
Clz.print(str(key) , Clz.bg1+Clz.fgB3, False) Clz.print(str(key) , Clz.bg1+Clz.fgB3, False)
Clz.print('` not on the server == ', Clz.bg1+Clz.fgB7) Clz.print('` not on the server == ', Clz.bg1+Clz.fgB7)
print() mprint()
else : 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]) self.switchFileAccount(row[ImpraIndex.ACCOUNT])
account = self.ih.conf.user account = self.ih.conf.user
if DEBUG.active :
Clz.print(' account : ' , Clz.fgn7, False) Clz.print(' account : ' , Clz.fgn7, False)
Clz.print(account , Clz.fgB7) Clz.print(account , Clz.fgB7)
ck = ConfigKey(row[ImpraIndex.HASH]) ck = ConfigKey(row[ImpraIndex.HASH])
@ -955,18 +976,17 @@ class ImpraStorage:
def getFile(self,key): def getFile(self,key):
"""""" """"""
from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO
done = False done = False
row = self.index.get(key) row = self.index.get(key)
if row==None : if row==None :
print() mprint()
Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) Clz.print(' == `' , Clz.bg1+Clz.fgB7, False)
Clz.print(str(key) , Clz.bg1+Clz.fgB3, False) Clz.print(str(key) , Clz.bg1+Clz.fgB3, False)
Clz.print('` not on the server == ', Clz.bg1+Clz.fgB7) Clz.print('` not on the server == ', Clz.bg1+Clz.fgB7)
print() mprint()
else : 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]) self.switchFileAccount(row[ImpraIndex.ACCOUNT])
account = self.ih.conf.user account = self.ih.conf.user
Clz.print(' account : ' , Clz.fgn7, False) Clz.print(' account : ' , Clz.fgn7, False)
@ -978,21 +998,21 @@ class ImpraStorage:
for mid in ids : for mid in ids :
self.ih.downloadAttachment(mid,self.fsplit.DIR_INBOX, True) self.ih.downloadAttachment(mid,self.fsplit.DIR_INBOX, True)
if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : if DEBUG.active and DEBUG.level <= DEBUG.NOTICE :
print(hlst['head']) mprint(hlst['head'])
for v in hlst['data']: 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]) path = self.fsplit.deployFile(hlst, row[ImpraIndex.LABEL], row[ImpraIndex.EXT], row[ImpraIndex.UID], row[ImpraIndex.CATG])
if row[ImpraIndex.BFLAG] == ImpraIndex.FILE_CRYPT: if row[ImpraIndex.BFLAG] == ImpraIndex.FILE_CRYPT:
self.decryptTextFile(path) self.decryptTextFile(path)
print() mprint()
Clz.print(' deploying in ', Clz.fgn7) Clz.print(' deploying in ', Clz.fgn7)
Clz.print(' '+dirname(path), Clz.fgB2) Clz.print(' '+dirname(path), Clz.fgB2)
print() mprint()
done = True done = True
else : else :
print() mprint()
Clz.print(' == `' , Clz.BG3+Clz.fgB1, False) Clz.print(' == `' , Clz.BG3+Clz.fgB1, False)
Clz.print(row[ImpraIndex.LABEL] , Clz.BG3+Clz.fgB4, False) Clz.print(row[ImpraIndex.LABEL] , Clz.BG3+Clz.fgB4, False)
Clz.print('` invalid count parts ' , Clz.BG3+Clz.fgB1, 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('/' , Clz.BG3+Clz.fgB1, False)
Clz.print(str(row[ImpraIndex.PARTS]) , Clz.BG3+Clz.fgB4, False) Clz.print(str(row[ImpraIndex.PARTS]) , Clz.BG3+Clz.fgB4, False)
Clz.print(' == ' , Clz.BG3+Clz.fgB1) Clz.print(' == ' , Clz.BG3+Clz.fgB1)
print() mprint()
self._setIndexImap() self._setIndexImap()
rt.stop() rt.stop()
return done return done

View File

@ -126,6 +126,7 @@ class ConfigKey:
def __init__(self, key=None, salt=None, psize=19710000): def __init__(self, key=None, salt=None, psize=19710000):
"""""" """"""
rt = RuTime(eval(__CALLER__()))
if key : self.key = bytes(key,'utf-8') if key : self.key = bytes(key,'utf-8')
else : self.key = self._build() else : self.key = self._build()
if salt ==None : self.salt = str(self.key[::-10]) if salt ==None : self.salt = str(self.key[::-10])
@ -133,6 +134,7 @@ class ConfigKey:
self.psize = psize self.psize = psize
self.noiser = Noiser(self.key) self.noiser = Noiser(self.key)
self.rdmz = Randomiz(1) self.rdmz = Randomiz(1)
rt.stop()
def getHashList(self,name,count,noSorted=False): def getHashList(self,name,count,noSorted=False):
"""""" """"""
@ -154,8 +156,11 @@ class ConfigKey:
def _build(self,l=48): def _build(self,l=48):
"""""" """"""
rt = RuTime(eval(__CALLER__()))
kg = KeyGen(l) 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): def getKey(self):
"""""" """"""
@ -170,25 +175,31 @@ class Kirmah:
def __init__(self, key, mark): def __init__(self, key, mark):
"""""" """"""
rt = RuTime(eval(__CALLER__()))
self.key = bytes(key,'utf-8') self.key = bytes(key,'utf-8')
self.mark = mark self.mark = mark
self.ck = ConfigKey(mark) self.ck = ConfigKey(mark)
rt.stop()
def enchr(self,o,cs,ce): 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: if not self.key[cs] > self.key[ce] or not o - 2*self.key[cs] - self.key[ce] > 0:
o += self.key[ce] o += self.key[ce]
else: else:
o -= self.key[cs] o -= self.key[cs]
if o == 10: o+=30 if o == 10: o+=30
#~ rt.stop()
return o return o
def dechr(self,o,cs,ce): 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 : if not self.key[cs] > self.key[ce] or self.enchr(o - self.key[ce],cs,ce)==o :
o -= self.key[ce] o -= self.key[ce]
else : else :
o += self.key[cs] o += self.key[cs]
#~ rt.stop()
return o return o
def subenc(self,data): def subenc(self,data):
@ -229,14 +240,20 @@ class Kirmah:
def sign(self,data): 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): def unsign(self,data):
"""""" """"""
rt = RuTime(eval(__CALLER__()))
d = data[64:] d = data[64:]
if not data[:64] == hash_sha256(self.mark + hash_sha256(d)): if not data[:64] == hash_sha256(self.mark + hash_sha256(d)):
raise BadKeyException() raise BadKeyException()
else: return d else:
rt.stop()
return d
def encrypt(self, odata, label, cpart): def encrypt(self, odata, label, cpart):
"""""" """"""
@ -291,19 +308,25 @@ class Randomiz:
def __init__(self,count,chl=None): def __init__(self,count,chl=None):
"""""" """"""
#~ rt = RuTime(eval(__CALLER__()))
if chl ==None : self.lst = list(range(0,count)) if chl ==None : self.lst = list(range(0,count))
else: self.lst = chl else: self.lst = chl
self.count = len(self.lst) self.count = len(self.lst)
#~ rt.stop()
def new(self,count=None, chl=None): def new(self,count=None, chl=None):
"""""" """"""
#~ rt = RuTime(eval(__CALLER__()))
if count : self.count = count if count : self.count = count
self.__init__(self.count,chl) self.__init__(self.count,chl)
#~ rt.stop()
def get(self,single=True): def get(self,single=True):
"""""" """"""
#~ rt = RuTime(eval(__CALLER__()))
pos = choice(self.lst) pos = choice(self.lst)
if single: del self.lst[self.lst.index(pos)] if single: del self.lst[self.lst.index(pos)]
#~ rt.stop()
return pos return pos
@ -315,23 +338,29 @@ class Noiser:
def __init__(self, key, part=0): def __init__(self, key, part=0):
"""""" """"""
#~ rt = RuTime(eval(__CALLER__()))
self.key = key self.key = key
self.build(part) self.build(part)
#~ rt.stop()
def build(self, part): def build(self, part):
"""""" """"""
#~ rt = RuTime(eval(__CALLER__()))
if not part < len(self.key)-1 : raise Exception('part exceed limit') if not part < len(self.key)-1 : raise Exception('part exceed limit')
else : else :
self.part, v = part, 0 self.part, v = part, 0
v = int(ceil((self.key[22]+v)/4.20583)) v = int(ceil((self.key[22]+v)/4.20583))
self.lns = abs(int(ceil(v/2))-self.key[self.part]+self.key[7]) 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)) 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): def getNoise(self, l, noBytes=False):
"""""" """"""
#~ rt = RuTime(eval(__CALLER__()))
n = urandom(l) n = urandom(l)
if noBytes: if noBytes:
n = str(urlsafe_b64encode(n),'utf-8')[0:l] n = str(urlsafe_b64encode(n),'utf-8')[0:l]
#~ rt.stop()
return n return n

View File

@ -37,7 +37,7 @@ from os.path import join
from re import search, split from re import search, split
from time import time, sleep 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.srv = IMAP4_SSL(conf.host,conf.port)
self.conf = conf self.conf = conf
status, resp = self.srv.login(conf.user,conf.pwd) status, resp = self.srv.login(conf.user,conf.pwd)
if DEBUG.level <= DEBUG.ALL :
mprint(status)
mprint(resp)
self.rootBox = box self.rootBox = box
if boxBin is None : if boxBin is None :
if search('yahoo.com',conf.host) is not None : if search('yahoo.com',conf.host) is not None :
@ -305,27 +308,26 @@ class ImapHelper:
def deleteBin(self): 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) self.srv.select(self.BOX_BIN)
ids = self.search('ALL',True) ids = self.search('ALL',True)
if len(ids) > 0 and ids[0]!='' and ids[0]!=None: if len(ids) > 0 and ids[0]!='' and ids[0]!=None:
print() mprint()
#print(str(ids[0],'utf-8').split()) #print(str(ids[0],'utf-8').split())
for mid in ids: for mid in ids:
#~ uid = bytes(mid) #~ uid = bytes(mid)
#~ print(type(mid)) #~ mprint(type(mid))
#~ print(mid) #~ mprint(mid)
#status, resp = self.srv.store(mid, '+FLAGS', '\\Deleted') #status, resp = self.srv.store(mid, '+FLAGS', '\\Deleted')
status, resp = self.srv.uid('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(' deleting msg ',Clz.fgN7+Clz.bg1, False)
Clz.print(str(int(mid)) ,Clz.bg1+Clz.fgB3) Clz.print(str(int(mid)) ,Clz.bg1+Clz.fgB3)
if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE: if DEBUG.level <= DEBUG.NOTICE:
print(status) mprint(status)
print(resp) mprint(resp)
self.srv.expunge() self.srv.expunge()
print() mprint()
self.srv.select(self.rootBox) self.srv.select(self.rootBox)
rt.stop() rt.stop()
@ -338,6 +340,7 @@ class ImapHelper:
status, resp = self.srv.uid( 'store', mid, '+FLAGS (\\Deleted)' ) status, resp = self.srv.uid( 'store', mid, '+FLAGS (\\Deleted)' )
else : else :
status, resp = self.srv.store(mid, '+FLAGS', 'Deleted') status, resp = self.srv.store(mid, '+FLAGS', 'Deleted')
Clz.print(' flag msg ' , Clz.fgn7, False) Clz.print(' flag msg ' , Clz.fgn7, False)
Clz.print(str(int(mid)), Clz.fgB1, False) Clz.print(str(int(mid)), Clz.fgB1, False)
Clz.print(' as deleted', Clz.fgn7) Clz.print(' as deleted', Clz.fgn7)
@ -352,13 +355,12 @@ class ImapHelper:
def downloadAttachment(self, msg, toDir='./', byUid=False): 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) : if not isinstance(msg, Message) :
msg = self.email(msg,byUid) msg = self.email(msg,byUid)
for part in msg.walk(): for part in msg.walk():
filename = part.get_filename() 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 if part.get_content_maintype() == 'multipart' or not filename : continue
fp = open(join(toDir, filename), 'wb') fp = open(join(toDir, filename), 'wb')
#print(part.get_payload(decode=True)[::-1]) #print(part.get_payload(decode=True)[::-1])
@ -368,15 +370,14 @@ class ImapHelper:
def send(self, msg, box='INBOX'): def send(self, msg, box='INBOX'):
"""""" """"""
from impra.util import DEBUG_INFO, DEBUG_LEVEL, DEBUG, DEBUG_NOTICE
rt = RuTime(eval(__CALLER__())) rt = RuTime(eval(__CALLER__()))
mid = None mid = None
date = Time2Internaldate(time()) date = Time2Internaldate(time())
status, resp = self.srv.append(box, '\Draft', date, bytes(msg,'utf-8')) status, resp = self.srv.append(box, '\Draft', date, bytes(msg,'utf-8'))
if status==self.OK: if status==self.OK:
if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE: if DEBUG.level <= DEBUG.NOTICE:
print(status) mprint(status)
print(resp) mprint(resp)
m = search(b']', resp[0]) m = search(b']', resp[0])
mid = str(resp[0],'utf-8')[11:-(len(resp[0])-m.start())].split(' ') mid = str(resp[0],'utf-8')[11:-(len(resp[0])-m.start())].split(' ')
rt.stop() rt.stop()

View File

@ -44,14 +44,22 @@ import platform
#~ from sys.stdout import isatty #~ from sys.stdout import isatty
from time import time from time import time
DEBUG_ALL = 0 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DEBUG_WARN = 1 # ~~ class Debug ~~
DEBUG_NOTICE = 2 class Debug:
DEBUG_INFO = 3
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 ~~ # ~~ methods ~~
@ -169,7 +177,7 @@ def run(cmdline):
cmdout, cmderr = p.communicate() cmdout, cmderr = p.communicate()
rcode = p.wait() rcode = p.wait()
if rcode < 0: if rcode < 0:
print((stderr,"Child was terminated by signal",rcode)) mprint((stderr,"Child was terminated by signal",rcode))
else: else:
return (rcode,cmdout,cmderr) return (rcode,cmdout,cmderr)
except OSError as e : except OSError as e :
@ -184,15 +192,21 @@ def __CALLER__(args=''):
:Returns: `str` :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)+") " 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 += "'" else: val += "'"
return val return val
def mprint(d='',end='\n'):
if DEBUG.active : print(d,end=end)
if platform.system() == 'Windows' : if platform.system() == 'Windows' :
clear = lambda: system('cls') SYS_CLEAR = 'cls'
else : clear = lambda: system('clear') else :
SYS_CLEAR = 'clear'
clear = lambda: system(SYS_CLEAR)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -201,11 +215,10 @@ else : clear = lambda: system('clear')
class RuTime: class RuTime:
"""Give basics time stats""" """Give basics time stats"""
def __init__(self,label,lvl=DEBUG_NOTICE): def __init__(self,label,lvl=DEBUG.NOTICE):
"""Initialize duration with appropriate label""" """Initialize duration with appropriate label"""
from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_INFO self.debug = DEBUG.active and DEBUG.level <= lvl
self.debug = DEBUG and DEBUG_LEVEL <= lvl self.debugStart = self.debug and lvl < DEBUG.INFO
self.debugStart = self.debug and lvl < DEBUG_INFO
self.lvl = lvl self.lvl = lvl
self.label = label self.label = label
self._start() self._start()
@ -334,7 +347,7 @@ class IniFile:
if s!='main': if s!='main':
#~ if section=='*': content += '\n['+s+']\n' #~ if section=='*': content += '\n['+s+']\n'
#~ else : content += '\n['+s[len(section)+1:]+']\n' #~ else : content += '\n['+s[len(section)+1:]+']\n'
print() mprint()
if not withoutSectionName : if not withoutSectionName :
Clz.print('['+s+']', Clz.fgB3) Clz.print('['+s+']', Clz.fgB3)
else: Clz.print('['+s.split('.')[1]+']', 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): def print(self,data,colors,endLF=True,endClz=True):
"""""" """"""
if DEBUG.active:
if not endLF : ev = '' if not endLF : ev = ''
else: ev = self._LF else: ev = self._LF
if self.active : if self.active :
tokens = [c.lstrip(self._MARKER[0]).rstrip(self._SEP) for c in colors.split(self._MARKER) if c is not ''] tokens = [c.lstrip(self._MARKER[0]).rstrip(self._SEP) for c in colors.split(self._MARKER) if c is not '']
if self.isUnix : if self.isUnix :
if endClz : data += self._uOFF if endClz : data += self._uOFF
print(eval('self._u'+'+self._u'.join(tokens))+data,end=ev) mprint(eval('self._u'+'+self._u'.join(tokens))+data,end=ev)
else : else :
self.setColor(eval('self._w'+'|self._w'.join(tokens))) self.setColor(eval('self._w'+'|self._w'.join(tokens)))
print(data,end=ev) mprint(data,end=ev)
stdout.flush() stdout.flush()
if endClz : self.setColor(self._wOFF) if endClz : self.setColor(self._wOFF)
else: else:
print(data,end=ev) mprint(data,end=ev)
Clz = Coloriz() Clz = Coloriz()