From 34b8ef3aabf5cdc5c972c2dacdbcd09a33c3ea92 Mon Sep 17 00:00:00 2001 From: a-sansara Date: Mon, 24 Sep 2012 19:11:20 +0200 Subject: [PATCH] cli coloriz help, readjust all options --- impra/cli.py | 972 +++++++++++++++++++++++++++++++++------------- impra/core.py | 113 +++--- impra/w32color.py | 42 +- 3 files changed, 775 insertions(+), 352 deletions(-) diff --git a/impra/cli.py b/impra/cli.py index 08ebae7..f97f81f 100644 --- a/impra/cli.py +++ b/impra/cli.py @@ -46,21 +46,17 @@ APP_AUTHOR = 'a-Sansara' APP_COPY = 'pluie.org' APP_LICENSE = 'GNU GPLv3' APP_DESC = """ -ImpraStorage provided a private imap access to store large files. -Each file stored on the server is split in severals random parts. -Each part also contains random noise data (lenght depends on a crypt key) -to ensure privacy and exclude easy merge without the corresponding key. + ImpraStorage provided a private imap access to store large files. Each file stored on the server is split + in severals random parts. Each part also contains random noise data (lenght depends on a crypt key) to + ensure privacy and exclude easy merge without the corresponding key. -An index of files stored is encrypt (with the symmetric-key algorithm -Kirmah) and regularly updated. Once decrypt, it permit to perform search -on the server and download each part. + An index of files stored is encrypt (with the symmetric-key algorithm Kirmah) and regularly updated. Once + decrypt, it permit to perform search on the server and download each part. -transfert process is transparent. Just vizualize locally the index of -stored files and simply select files to download or upload. -ImpraStorage automatically launch the parts to download, then merge parts -in the appropriate way to rebuild the original file. Inversely, a file -to upload is split (in several parts with addition of noise data), and -ImpraStorage randomly upload each parts then update the index. + Transfert process is transparent. Just vizualize locally the index of stored files and simply select files + to download or upload. ImpraStorage automatically launch the parts to download, then merge parts in the + appropriate way to rebuild the original file. Inversely, a file to upload is split (in several parts with + addition of noise data), and ImpraStorage randomly upload each parts then update the index. """ @@ -99,61 +95,10 @@ class Cli: def __init__(self,path): self.ini = util.IniFile(path+'impra.ini') - parser = _OptionParser(prog='imprastorage', usage='\n\n\ -------------------------------------------------------------------------------\n\ --- %s --\n\ -------------------------------------------------------------------------------\n\ --- version : %s copyright : %s --\n\ --- author : %s license : %s --\n\ -------------------------------------------------------------------------------\n\ -\n\ -%s conf [-D|-L| -K,-H {host},-U {user},-X {password},-P {port},\n\ - -B {boxname}] [-A profileName]\n\ -%s data [-l |-a {file, label} |-g {id} |-s {pattern} | -r {id}]\n\ - [-c {catg}, -u {owner}]' - % (APP_TITLE,APP_VERSION,APP_COPY,APP_AUTHOR,APP_LICENSE,APP_TITLE.lower(),APP_TITLE.lower()) , epilog=""" - -conf command Examples: - - Initialize program and set config on default profile with keys generation : - imprastorage conf -K -H imap.gmail.com -P 993 -U login -X password - - Set config on a new profile with same keys from previous active profile: - imprastorage conf -H myimapserver.net -P 993 -U login -X password -B boxname -A profile1 - - Load config from a profile (wich become active) : - imprastorage conf -DA profile2 - - List config from profile : - imprastorage conf -LA profile1 - - -data command Examples: - - List index - imprastorage data -l - - Add file - imprastorage data -a /path/tofile 'my video' - - Add file with category (category is also a directory structure recreate - when downloading files) - imprastorage data -a /path/tofile '2009 - en la playa' -c videos/perso/2009 - - Get file by id - imprastorage data -g 22 - - Remove from server a file by id - imprastorage data -g 22 - - Search files matching pattern : - imprastorage data -s 'holydays' - - Search files upload by a particular user on a category : - imprastorage data -s * -c films -u myfriend - -""",description=APP_DESC) - + parser = _OptionParser() + parser.print_help = self.print_help + parser.print_usage = self.print_usage + gpData = OptionGroup(parser, '\n------------------------------------\ndata related Options (command data)') gpConf = OptionGroup(parser, '\n------------------------------------\nconf related Options (command conf)') @@ -162,21 +107,17 @@ data command Examples: parser.add_option('-v', '--version' , help='show program\'s version number and exit' , action='store_true' , default=False) parser.add_option('-q', '--quiet' , help='don\'t print status messages to stdout' , action='store_false', default=True) parser.add_option('-d', '--debug' , help='set debug mode' , action='store_true' , default=False) - - gpData.add_option('-l', '--list' , help='list index on imap server' , action='store_true' ) - gpData.add_option('-a', '--add' , help='add file FILE with specified LABEL on server' , action='store', metavar='FILE LABEL ', nargs=2) - gpData.add_option('-g', '--get' , help='get file with specified ID from server' , action='store', metavar='ID ') - gpData.add_option('-s', '--search' , help='search file with specified PATTERN' , action='store', metavar='PATTERN ') - gpData.add_option('-r', '--remove' , help='remove FILE with specified ID from server' , action='store', metavar='ID ') - gpData.add_option('-c', '--category' , help='set specified CATEGORY (crit. for opt. -l,-a or -s)' , action='store', metavar='CATG ' , default='') - gpData.add_option('-u', '--user' , help='set specified USER (crit. for opt. -l,-a or -s)' , action='store', metavar='OWNER ' , default='all') + + 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('-o', '--order' , help='set colon ORDER (crit. for opt. -l and -s)' , action='store', metavar='ORDER ' , default='ID') gpData.add_option('-O', '--order-inv' , help='set colon ORDER_INVERSE (crit. for opt. -l and -s)' , action='store', metavar='ORDER_INVERSE') #gpData.add_option('-o', '--output-dir' , help='set specified OUTPUT DIR (for opt. -l,-a,-d or -g)' , action='store', metavar='DIR ') parser.add_option_group(gpData) - gpConf.add_option('-L', '--list-conf' , help='list configuration' , action='store_true', default=False) - gpConf.add_option('-D', '--load-conf' , help='load configuration' , action='store_true', default=False) + gpConf.add_option('-V', '--view' , help='view configuration' , action='store' ) + gpConf.add_option('-L', '--load' , help='load configuration' , action='store' ) + gpConf.add_option('-S', '--save' , help='save configuration' , action='store' ) gpConf.add_option('-H', '--set-host' , help='set imap host server' , action='store', metavar='HOST ') gpConf.add_option('-U', '--set-user' , help='set imap user login' , action='store', metavar='USER ') gpConf.add_option('-X', '--set-pass' , help='set imap user password' , action='store', metavar='PASS ') @@ -184,229 +125,183 @@ data command Examples: gpConf.add_option('-N', '--set-name' , help='set user name' , action='store', metavar='NAME ') gpConf.add_option('-B', '--set-boxn' , help='set boxName on imap server (default:[%default])' , action='store', metavar='BOXNAME ') gpConf.add_option('-K', '--gen-keys' , help='generate new key' , action='store_true', default=False) - gpConf.add_option('-A', '--active-profile', help='set active profile' , action='store', metavar='PROFILE ') parser.add_option_group(gpConf) (o, a) = parser.parse_args() - -# USAGE : -# -# imprastorage add {filePath} [{name} -c {category} -u {owner}] -# imprastorage list [-o {colon}, -c {category} -u {owner}] -# imprastorage get {id|ids} | -n {name} -# imprastorage remove {id} | -n {name} -# imprastorage search {pattern} [-c {category} -u {owner} -o {colon}] -# imprastorage conf [-A profileName] -L | -V | -# -S [-K, -H {host}, -U {user}, -X {password}, -P {port}, -B {box}, -N {name}] -# -# EXEMPLES : -# -# command add : -# imprastorage add /home/Share/2009-mountains.avi -# imprastorage add /home/Share/2009-mountains.avi 'summer 2009 - in mountains' -# imprastorage add /home/Share/2009-mountains.avi -u family -# imprastorage add /home/Share/2009-mountains.avi -c videos/perso/2009 -# imprastorage add /home/Share/2009-mountains.avi 'summer 2009 - in mountains' -c videos/perso/2009 -u family -# -# command list : -# imprastorage list -# imprastorage list -o LABEL -# imprastorage list -c videos/perso -# imprastorage list -u family -# imprastorage list -o SIZE -c videos/perso -u family -# -# command get : -# imprastorage get 15 -# imprastorage get 15-19 -# imprastorage get 22,29,30 -# imprastorage get 22,29,30-35,48 -# imprastorage get -n 'summer 2009 - in mountains' -# -# command remove : -# imprastorage remove 15 -# imprastorage remove -n 'summer 2009 - in mountains' -# -# command search : -# imprastorage search 'mountains' -# imprastorage search 'mountains' -c videos/perso -# imprastorage search 'mountains' -u family -# imprastorage search 'mountains' -o LABEL -# imprastorage search 'mountains' -c videos/perso -u family -o LABEL -# imprastorage search '^mountains' -# imprastorage search 'mountains$' -# -# command conf : -# imprastorage conf -L -# imprastorage conf -LA bobgmail -# imprastorage conf -V -# imprastorage conf -VA bobgmail -# imprastorage conf -S -K -# imprastorage conf -SA bobgmail -K -# imprastorage conf -SA bobgmail -K -H imap.gmail.com -U bob22 -X mypassword -N bob -# + print() if not a: - parser.print_help() - print() - parser.error(' no commando specified') - sys.exit(1) + + try : + if o.version : + self.print_header() + sys.exit(0) + except Exception as ex: + pass + + try : + if not o.help : + parser.error(' no commando specified') + sys.exit(1) + else : + parser.print_help() + except : + parser.error(' no commando specified') + sys.exit(1) else: # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~ conf CMD ~~ if a[0] == 'conf' : - if o.active_profile==None: - if self.ini.has('profile') : o.active_profile = self.ini.get('profile') - else : o.active_profile = 'default' - - if o.load_conf : self.load_profile(o) + if o.load is not None or o.view is not None or o.save is not None : + + if o.view is not None : + o.active_profile = o.view + if o.load is not None : + o.active_profile = o.load + if o.save is not None : + o.active_profile = o.save + + if o.active_profile==None: + if self.ini.has('profile') : o.active_profile = self.ini.get('profile') + else : o.active_profile = 'default' + + if o.load : self.load_profile(o) - elif o.list_conf : print(self.ini.toString(o.active_profile)) + elif o.view : self.ini.print(o.active_profile) + elif o.save : + if not o.set_host and not o.set_user and not o.set_pass and not o.set_port and not o.set_boxn and not o.set_name and not o.gen_keys: + parser.error(' no options specified') + else : + if o.set_port and not util.represents_int(o.set_port): + parser.error(' port must be a number') + sys.exit(1) + if o.set_boxn: self.ini.set('box' , o.set_boxn,o.active_profile+'.imap') + if o.set_host: self.ini.set('host', o.set_host,o.active_profile+'.imap') + if o.set_user: self.ini.set('user', o.set_user,o.active_profile+'.imap') + if o.set_pass: self.ini.set('pass', o.set_pass,o.active_profile+'.imap') + if o.set_port: self.ini.set('port', o.set_port,o.active_profile+'.imap') + if o.set_name: self.ini.set('name', o.set_name,o.active_profile+'.infos') + if o.gen_keys: + kg = crypt.KeyGen(256) + self.ini.set('key' ,kg.key,o.active_profile+'.keys') + self.ini.set('mark',kg.mark,o.active_profile+'.keys') + self.ini.set('salt','-¤-ImpraStorage-¤-',o.active_profile+'.keys') + if self.check_profile(o.active_profile): + self.ini.set('profile', o.active_profile) + self.ini.write() + self.ini.print(o.active_profile) else : - if not o.set_host and not o.set_user and not o.set_pass and not o.set_port and not o.set_boxn and not o.set_name and not o.gen_keys: - parser.error(' no options specified') - else : - if o.set_port and not util.represents_int(o.set_port): - parser.error(' port must be a number') - sys.exit(1) - if o.set_boxn: self.ini.set('box' , o.set_boxn,o.active_profile+'.imap') - if o.set_host: self.ini.set('host', o.set_host,o.active_profile+'.imap') - if o.set_user: self.ini.set('user', o.set_user,o.active_profile+'.imap') - if o.set_pass: self.ini.set('pass', o.set_pass,o.active_profile+'.imap') - if o.set_port: self.ini.set('port', o.set_port,o.active_profile+'.imap') - if o.set_name: self.ini.set('name', o.set_name,o.active_profile+'.infos') - if o.gen_keys: - kg = crypt.KeyGen(256) - self.ini.set('key' ,kg.key,o.active_profile+'.keys') - self.ini.set('mark',kg.mark,o.active_profile+'.keys') - self.ini.set('salt','-¤-ImpraStorage-¤-',o.active_profile+'.keys') - if self.check_profile(o.active_profile): - self.ini.set('profile', o.active_profile) - self.ini.write() - self.ini.print(o.active_profile) + self.print_usage('') # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~ data CMD ~~ - elif a[0] == 'data' : + elif a[0] == 'list' or a[0] == 'add' or a[0] == 'get' or a[0] == 'remove' or a[0] == 'search' : o.active_profile = self.ini.get('profile') - if not o.list and not o.add and not o.get and not o.search and not o.remove : - parser.error(' no options specified') - else : - if self.check_profile(o.active_profile): - conf = core.ImpraConf(self.ini,o.active_profile) - impst = None - try: - impst = core.ImpraStorage(conf) - except crypt.BadKeyException as e : + if self.check_profile(o.active_profile): + conf = core.ImpraConf(self.ini,o.active_profile) + impst = None + try: + impst = core.ImpraStorage(conf) + except crypt.BadKeyException as e : + print() + Clz.print(' it seems that your current profile `' , Clz.fgB1, False) + Clz.print(o.active_profile , Clz.fgB3, False) + Clz.print('` has a wrong key to decrypt index on server.' , Clz.fgB1) + Clz.print(' you can remove index but all presents files on the box `', Clz.fgB1, False) + Clz.print(conf.get('box','imap') , Clz.fgB3, False) + Clz.print('` will be unrecoverable\n' , Clz.fgB1, True, False) + + remIndex = input(' remove index ? (yes/no) ') + if remIndex.lower()=='yes': + Clz.print(' ',Clz.OFF) + impst = core.ImpraStorage(conf, True) + + else : print() - Clz.print(' it seems that your current profile `' , Clz.fgB1, False) - Clz.print(o.active_profile , Clz.fgB3, False) - Clz.print('` has a wrong key to decrypt index on server.' , Clz.fgB1) - Clz.print(' you can remove index but all presents files on the box `', Clz.fgB1, False) - Clz.print(conf.get('box','imap') , Clz.fgB3, False) - Clz.print('` will be unrecoverable\n' , Clz.fgB1, True, False) + print(' bye') + print() + sys.exit(1) - remIndex = input(' remove index ? (yes/no) ') - if remIndex.lower()=='yes': - Clz.print(' ',Clz.OFF) - impst = core.ImpraStorage(conf, True) - - else : - print() - print(' bye') - print() - sys.exit(1) - - if o.list : - uid = conf.get('uid' ,'index') - date = conf.get('date','index') - account = conf.get('user','imap') - if impst.index != None: - noData = impst.index.isEmpty() - if uid == None or noData : uid = 'EMPTY' - if date == None or noData : date = '' - core.clear() - printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) - printHeaderTitle(APP_TITLE) - printHeaderPart('account',account) - printHeaderPart('index',uid) - printHeaderPart('box',impst.rootBox) - Clz.print(date, Clz.fgB7, True, True) - printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) - order = o.order - if o.order_inv is not None: - order = '-'+o.order_inv - impst.index.print(order) + if a[0]=='list': - elif o.add : - done = impst.addFile(o.add[0],o.add[1],o.user,o.category) + uid = conf.get('uid' ,'index') + date = conf.get('date','index') + account = conf.get('user','imap') + if impst.index != None: + noData = impst.index.isEmpty() + if uid == None or noData : uid = 'EMPTY' + if date == None or noData : date = '' + core.clear() + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + printHeaderTitle(APP_TITLE) + printHeaderPart('account',account) + printHeaderPart('index',uid) + printHeaderPart('box',impst.rootBox) + Clz.print(date, Clz.fgB7, True, True) + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + + matchIdsCatg = None + matchIdsUser = None + matchIds = None + if o.category is not None : + print(o.category) + matchIdsCatg = impst.index.getByCategory(o.category) + if o.user is not None : + matchIdsUser = impst.index.getByUser(o.user) + + if o.category is not None : + if o.user is not None : + matchIds = impst.index.getIntersection(matchIdsCatg,matchIdsUser) + else : matchIds = matchIdsCatg + + elif o.user is not None : + matchIds = matchIdsUser + + order = o.order + if o.order_inv is not None: + order = '-'+o.order_inv + impst.index.print(order,matchIds) + + + elif a[0] == 'add': + if not len(a)>1 : self.error_cmd('`'+a[0]+' need at least one argument',parser) + vfile = a[1] + if util.file_exists(vfile) : + if not len(a)>2 : + label, ext = core.splitext(core.basename(vfile)) + else: label = a[2] + if o.category is None : o.category = '' + done = impst.addFile(vfile,label,o.category) if done : print('\n ',end='') Clz.print(' == OK == ', Clz.bg2+Clz.fgb7) print() + + else : self.error_cmd('`'+a[1]+' is not a file',parser) - elif o.get : - ids = [] - for sid in o.get.split(',') : - seq = sid.split('-') - if len(seq)==2 : ids.extend(range(int(seq[0]),int(seq[1])+1)) - else: ids.append(sid) - for sid in ids : - key = impst.index.getById(str(sid)) - if key !=None : - done = impst.getFile(key) - if done : - print('\n ',end='') - Clz.print(' == OK == ', Clz.bg2+Clz.fgb7) - print() - else: - print('\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() - elif o.search : - uid = conf.get('uid' ,'index') - date = conf.get('date','index') - account = conf.get('user','imap') - matchIds = impst.index.getByPattern(o.search) - core.clear() - if matchIds is not None: - printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) - printHeaderTitle(APP_TITLE) - printHeaderPart('account',account) - printHeaderPart('index',uid) - printHeaderPart('box',impst.rootBox) - Clz.print(date, Clz.fgB7, True, True) - printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) - Clz.print(' searching --' , Clz.fgB3, False) - Clz.print(' `'+o.search+'`' , Clz.fgB7, False) - Clz.print(' -- found ' , Clz.fgB3, False) - Clz.print(str(len(matchIds)), Clz.fgB1, False) - Clz.print(' results --' , Clz.fgB3) - printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) - order = o.order - if o.order_inv is not None: - order = '-'+o.order_inv - impst.index.print(o.order,matchIds) - else: - print('\n ',end='') - Clz.print(' == no match found for pattern `', Clz.bg3+Clz.fgB4, False) - Clz.print(o.search , Clz.bg3+Clz.fgB1, False) - Clz.print('` == ' , Clz.bg3+Clz.fgB4) - print() - elif o.remove : - key = impst.index.getById(o.remove) + elif a[0] == 'get': + + if not len(a)>1 : self.error_cmd('`'+a[0]+' need one argument',parser) + vid = a[1] + + ids = [] + for sid in vid.split(',') : + seq = sid.split('-') + if len(seq)==2 : ids.extend(range(int(seq[0]),int(seq[1])+1)) + else: ids.append(sid) + for sid in ids : + key = impst.index.getById(str(sid)) if key !=None : - done = impst.removeFile(key) + done = impst.getFile(key) if done : print('\n ',end='') Clz.print(' == OK == ', Clz.bg2+Clz.fgb7) @@ -414,18 +309,553 @@ data command Examples: else: print('\n ',end='') Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) - Clz.print(str(o.remove) , 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) print() - else : parser.print_help() - print() + elif a[0] == 'search': + + uid = conf.get('uid' ,'index') + date = conf.get('date','index') + account = conf.get('user','imap') + + if not len(a)>1 : self.error_cmd('`'+a[0]+' need one argument',parser) + vsearch = a[1] + + matchIds = impst.index.getByPattern(vsearch) + core.clear() + if matchIds is not None: + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + printHeaderTitle(APP_TITLE) + printHeaderPart('account',account) + printHeaderPart('index',uid) + printHeaderPart('box',impst.rootBox) + Clz.print(date, Clz.fgB7, True, True) + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + Clz.print(' searching --' , Clz.fgB3, False) + Clz.print(' `'+vsearch+'`' , Clz.fgB7, False) + Clz.print(' -- found ' , Clz.fgB3, False) + Clz.print(str(len(matchIds)), Clz.fgB1, False) + Clz.print(' results --' , Clz.fgB3) + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + + matchIdsCatg = None + matchIdsUser = None + matchIdsCrit = None + if o.category is not None : + print(o.category) + matchIdsCatg = impst.index.getByCategory(o.category) + if o.user is not None : + matchIdsUser = impst.index.getByUser(o.user) + + if o.category is not None : + if o.user is not None : + matchIdsCrit = impst.index.getIntersection(matchIdsCatg,matchIdsUser) + else : matchIdsCrit = matchIdsCatg + + elif o.user is not None : + matchIdsCrit = matchIdsUser + + if matchIdsCrit is not None : + matchIds = impst.index.getIntersection(matchIds,matchIdsCrit) + + order = o.order + if o.order_inv is not None: + order = '-'+o.order_inv + impst.index.print(o.order,matchIds) + else: + print('\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() + + elif a[0] == 'remove': + + if not len(a)>1 : self.error_cmd('`'+a[0]+' need one argument',parser) + vid = a[1] + key = impst.index.getById(vid) + if key !=None : + done = impst.removeFile(key) + if done : + print('\n ',end='') + Clz.print(' == OK == ', Clz.bg2+Clz.fgb7) + print() + else: + print('\n ',end='') + Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) + Clz.print(str(o.remove) , Clz.bg1+Clz.fgB3, False) + Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) + print() + + else : + self.error_cmd('unknow command `'+a[0]+'`',parser) + print() + + + def error_cmd(self,msg, parser): + parser.error(msg) + sys.exit(1) def check_profile(self,profile): """""" return self.ini.hasSection(profile+'.keys') and self.ini.has('host',profile+'.imap') and self.ini.has('user',profile+'.imap') and self.ini.has('pass',profile+'.imap') and self.ini.has('port',profile+'.imap') + def print_header(self): + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + printHeaderTitle(APP_TITLE) + printHeaderPart('version',APP_VERSION) + printHeaderPart('author',APP_AUTHOR) + printHeaderPart('license',APP_LICENSE) + printHeaderPart('copyright','2012 '+APP_COPY) + Clz.print(' ', Clz.OFF) + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + print() + + def print_version(self, data): + self.print_header() + + def print_usage(self, data, withoutHeader=False): + if not withoutHeader : self.print_header() + + Clz.print(' USAGE :\n', Clz.fgB3) + Clz.print(' imprastorage ', Clz.fgb7, False) + Clz.print('--help ', Clz.fgB3) + + Clz.print(' imprastorage ', Clz.fgb7, False) + Clz.print('add ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('filePath', Clz.fgb1, False) + Clz.print('} ', Clz.fgB1, False) + Clz.print('[', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('name', Clz.fgb1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(' -c ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('category', Clz.fgb1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(']', Clz.fgB3) + + Clz.print(' imprastorage ', Clz.fgb7, False) + Clz.print('get ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('id|ids', Clz.fgb1, False) + Clz.print('}', Clz.fgB1) + + Clz.print(' imprastorage ', Clz.fgb7, False) + Clz.print('list ', Clz.fgB3, False) + Clz.print('[', Clz.fgB3, False) + Clz.print(' -c ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('category', Clz.fgb1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(' -u ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('user', Clz.fgb1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(' -o', Clz.fgB3, False) + Clz.print('|', Clz.fgB1, False) + Clz.print('-O ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('colon', Clz.fgb1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(']', Clz.fgB3) + + Clz.print(' imprastorage ', Clz.fgb7, False) + Clz.print('remove ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('id', Clz.fgb1, False) + Clz.print('}', Clz.fgB1) + + Clz.print(' imprastorage ', Clz.fgb7, False) + Clz.print('search ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('pattern', Clz.fgb1, False) + Clz.print('} ', Clz.fgB1, False) + Clz.print('[', Clz.fgB3, False) + Clz.print(' -c ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('category', Clz.fgb1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(' -u ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('user', Clz.fgb1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(' -o', Clz.fgB3, False) + Clz.print('|', Clz.fgB1, False) + Clz.print('-O ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('colon', Clz.fgb1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(']', Clz.fgB3) + + Clz.print(' imprastorage ', Clz.fgb7, False) + Clz.print('conf', Clz.fgB3, False) + Clz.print(' -L', Clz.fgB3, False) + Clz.print('|', Clz.fgB1, False) + Clz.print('-V ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('profile', Clz.fgb1, False) + Clz.print('}', Clz.fgB1) + + Clz.print(' imprastorage ', Clz.fgb7, False) + Clz.print('conf', Clz.fgB3, False) + Clz.print(' -S ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('profile', Clz.fgb1, False) + Clz.print('} ', Clz.fgB1, False) + Clz.print('[', Clz.fgB3, False) + Clz.print(' -K ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('length', Clz.fgB1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(', -H ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('host', Clz.fgB1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(', -U ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('user', Clz.fgB1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(', -X ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('password', Clz.fgB1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(', -P ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('port', Clz.fgB1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(', -B ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('box', Clz.fgB1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(', -N ', Clz.fgB3, False) + Clz.print('{', Clz.fgB1, False) + Clz.print('name', Clz.fgB1, False) + Clz.print('}', Clz.fgB1, False) + Clz.print(' ]', Clz.fgB3) + + def print_options(self): + print('\n') + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + Clz.print(' MAIN OPTIONS :\n' , Clz.fgB3) + Clz.print(' '*4+'-h, --help' , Clz.fgB3) + Clz.print(' '*50+'display help message' , Clz.fgB7) + Clz.print(' '*4+'-q, --quiet' , Clz.fgB3) + 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) + Clz.print(' '*4+'-v, --version' , Clz.fgB3) + Clz.print(' '*50+'show program\'s version number' , Clz.fgB7) + print('\n') + + Clz.print(' COMMANDS OPTIONS :\n' , Clz.fgB3) + Clz.print(' '*4+'-c ' , Clz.fgB3, False) + Clz.print('CATEGORY'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --category '.ljust(18,' ') , Clz.fgB3, False) + Clz.print('CATEGORY'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'set a category' , Clz.fgB7) + + Clz.print(' '*4+'-u ' , Clz.fgB3, False) + Clz.print('USER'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --user'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('USER'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'set a user' , Clz.fgB7) + + Clz.print(' '*4+'-o ' , Clz.fgB3, False) + Clz.print('COLON'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --order'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('COLON'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'order by specified colon' , Clz.fgB7) + + Clz.print(' '*4+'-O ' , Clz.fgB3, False) + Clz.print('COLON'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --order-rev'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('COLON'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'reverse order by specified colon' , Clz.fgB7) + + print('\n') + Clz.print(' CONF OPTIONS :\n', Clz.fgB3) + Clz.print(' '*4+'-L ' , Clz.fgB3, False) + Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --load'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'load the specified profile' , Clz.fgB7) + + Clz.print(' '*4+'-V ' , Clz.fgB3, False) + Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --view'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'view the specified profile (or current)' , Clz.fgB7) + + Clz.print(' '*4+'-S ' , Clz.fgB3, False) + Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --order'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'save the specified profile' , Clz.fgB7) + + print('\n') + Clz.print(' CONF -S OPTIONS :\n', Clz.fgB3) + Clz.print(' '*4+'-N ' , Clz.fgB3, False) + Clz.print('NAME'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --set-name'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('NAME'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'set imprastorage username' , Clz.fgB7) + + Clz.print(' '*4+'-K ' , Clz.fgB3, False) + Clz.print('LENGTH'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --gen-key'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('LENGTH'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'generate a new key of optionnal LENGTH length (default:256)', Clz.fgB7) + + Clz.print(' '*4+'-H ' , Clz.fgB3, False) + Clz.print('HOST'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --set-host'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('HOST'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'set imap host' , Clz.fgB7) + + Clz.print(' '*4+'-U ' , Clz.fgB3, False) + Clz.print('USER'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --set-user'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('USER'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'set imap user' , Clz.fgB7) + + Clz.print(' '*4+'-X ' , Clz.fgB3, False) + Clz.print('PASSWORD'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --set-password'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('USER'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'set imap password' , Clz.fgB7) + + Clz.print(' '*4+'-P ' , Clz.fgB3, False) + Clz.print('PORT'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --set-port'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('PORT'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'set imap port' , Clz.fgB7) + + Clz.print(' '*4+'-B ' , Clz.fgB3, False) + Clz.print('BOXNAME'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(', --set-box'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('BOXNAME'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'set imap boxname (default:__impra__)' , Clz.fgB7) + + print('\n') + + + def print_help(self): + """""" + self.print_header() + Clz.print(APP_DESC, Clz.fgN1) + self.print_usage('', True) + self.print_options() + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + print() + Clz.print(' EXEMPLES :\n', Clz.fgB3) + + + Clz.print(' '*4+'command add :', Clz.fgB3) + + Clz.print(' '*8+'# add (upload) a file',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('add ', Clz.fgB3, False) + Clz.print('/home/Share/2009-mountains.avi', Clz.fgB1) + + Clz.print(' '*8+'# add a file with a label (label will be the filename on downloading)',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('add ', Clz.fgB3, False) + Clz.print('/home/Share/2009-mountains.avi \'summer 2009 - in mountains\'', Clz.fgB1) + + Clz.print(' '*8+'# add a file on a category (category will be the dir structure on downloading)',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('add ', Clz.fgB3, False) + Clz.print('/home/Share/2009-mountains.avi', Clz.fgB1, False) + Clz.print(' -c ', Clz.fgB3, False) + Clz.print('videos/perso/2009', Clz.fgB1) + + Clz.print(' '*8+'# add a file with a label on a category',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('add ', Clz.fgB3, False) + Clz.print('/home/Share/2009-mountains.avi \'summer 2009 - in mountains\'', Clz.fgB1, False) + Clz.print(' -c ', Clz.fgB3, False) + Clz.print('videos/perso/2009', Clz.fgB1) + + + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + Clz.print('\n'+' '*4+'command get :', Clz.fgB3) + + Clz.print(' '*8+'# get file with id 15',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('get ', Clz.fgB3, False) + Clz.print('15', Clz.fgB1) + + Clz.print(' '*8+'# get files with id 15,16,17,18,19',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('get ', Clz.fgB3, False) + Clz.print('15-19', Clz.fgB1) + + Clz.print(' '*8+'# get files with id 22,29,30',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('get ', Clz.fgB3, False) + Clz.print('22,29,30', Clz.fgB1) + + Clz.print(' '*8+'# get files with id 22,29,30,31,32,33,34,35,48',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('get ', Clz.fgB3, False) + Clz.print('22,29-35,48', Clz.fgB1) + + + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + Clz.print('\n'+' '*4+'command list :', Clz.fgB3) + + Clz.print(' '*8+'# list all files',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('list', Clz.fgB3) + + Clz.print(' '*8+'# list all files (sorted by LABEL)',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('list', Clz.fgB3, False) + Clz.print(' -o ' , Clz.fgB3, False) + Clz.print('LABEL', Clz.fgB1) + + Clz.print(' '*8+'# list all files on category `videos/perso` ',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('list', Clz.fgB3, False) + Clz.print(' -c ' , Clz.fgB3, False) + Clz.print('videos/perso', Clz.fgB1) + + Clz.print(' '*8+'# list all files sent by `bob`',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('list', Clz.fgB3, False) + Clz.print(' -u ' , Clz.fgB3, False) + Clz.print('bob', Clz.fgB1) + + Clz.print(' '*8+'# list all files sent by `bob` on category `videos/perso` (reverse sorted by SIZE)',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('list', Clz.fgB3, False) + Clz.print(' -O ' , Clz.fgB3, False) + Clz.print('SIZE', Clz.fgB1, False) + Clz.print(' -c ' , Clz.fgB3, False) + Clz.print('videos/perso', Clz.fgB1, False) + Clz.print(' -u ' , Clz.fgB3, False) + Clz.print('bob', Clz.fgB1) + + + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + Clz.print('\n'+' '*4+'command remove :', Clz.fgB3) + + Clz.print(' '*8+'# remove file with id 15 (removing command only take a single id)',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('remove ', Clz.fgB3, False) + Clz.print('15', Clz.fgB1) + + + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + Clz.print('\n'+' '*4+'command search :', Clz.fgB3) + + Clz.print(' '*8+'# search all files wich contains `mountains`',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('search ', Clz.fgB3, False) + Clz.print('mountains', Clz.fgB1) + + Clz.print(' '*8+'# search all files wich contains `old mountain` on category `videos/perso`',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('search ', Clz.fgB3, False) + Clz.print('\'old mountain\'', Clz.fgB1, False) + Clz.print(' -c ' , Clz.fgB3, False) + Clz.print('videos/perso', Clz.fgB1) + + Clz.print(' '*8+'# search all files wich contains `old mountain` sent by user `bob`',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('search ', Clz.fgB3, False) + Clz.print('\'old mountain\'', Clz.fgB1, False) + Clz.print(' -u ' , Clz.fgB3, False) + Clz.print('bob', Clz.fgB1) + + Clz.print(' '*8+'# search all files wich contains `old mountain` (reverse sorted by SIZE)',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('search ', Clz.fgB3, False) + Clz.print('\'old mountain\'', Clz.fgB1, False) + Clz.print(' -O ' , Clz.fgB3, False) + Clz.print('SIZE', Clz.fgB1) + + Clz.print(' '*8+'# search all files wich contains `old mountain` sent by user `bob` and on category `videos/perso` (reverse',Clz.fgn7) + Clz.print(' '*8+'# sorted by LABEL)',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('search ', Clz.fgB3, False) + Clz.print('\'old mountain\'', Clz.fgB1, False) + Clz.print(' -c ' , Clz.fgB3, False) + Clz.print('videos/perso', Clz.fgB1, False) + Clz.print(' -u ' , Clz.fgB3, False) + Clz.print('bob', Clz.fgB1, False) + Clz.print(' -O ' , Clz.fgB3, False) + Clz.print('LABEL' , Clz.fgB1) + + Clz.print(' '*8+'# search all files starting by `old mountain`',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('search ', Clz.fgB3, False) + Clz.print('\'^old mountain\'', Clz.fgB1) + + Clz.print(' '*8+'# search all files ending by `old mountain`',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('search ', Clz.fgB3, False) + Clz.print('\'old mountain$\'', Clz.fgB1) + + + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + Clz.print('\n'+' '*4+'command conf :', Clz.fgB3) + + Clz.print(' '*8+'# this command is tipycally a profile creation (or rewrite if profile exists)',Clz.fgn7) + Clz.print(' '*8+'# set a userName, generate a new Key and set imap account with host,port,user,password for profile bobgmail',Clz.fgn7) + Clz.print(' '*8+'# then set it as current profile',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('conf ', Clz.fgB3, False) + Clz.print('-S ', Clz.fgB3, False) + Clz.print('bobgmail ', Clz.fgB1, False) + Clz.print('-N ', Clz.fgB3, False) + Clz.print('bob ', Clz.fgB1, False) + Clz.print('-K -H ', Clz.fgB3, False) + Clz.print('imap.gmail.com ', Clz.fgB1, False) + Clz.print('-P ', Clz.fgB3, False) + Clz.print('993 ', Clz.fgB1, False) + Clz.print('-U ', Clz.fgB3, False) + Clz.print('bob22 ', Clz.fgB1, False) + Clz.print('-X ', Clz.fgB3, False) + Clz.print('mypassword ', Clz.fgB1) + + Clz.print(' '*8+'# load config profile bobimap and set it as current profile',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('conf ', Clz.fgB3, False) + Clz.print('-L ', Clz.fgB3, False) + Clz.print('bobimap ', Clz.fgB1) + + Clz.print(' '*8+'# view config current profile',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('conf ', Clz.fgB3, False) + Clz.print('-V', Clz.fgB3) + + Clz.print(' '*8+'# view config profile bobgmail (current profile doesn\'t change)',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('conf ', Clz.fgB3, False) + Clz.print('-V ', Clz.fgB3, False) + Clz.print('bobgmail ', Clz.fgB1) + + Clz.print(' '*8+'# generate a new Key for current profile (carreful with this command if your account has no empty index - ',Clz.fgn7) + Clz.print(' '*8+'# all files will be unrecoverable without the appropriate key)',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('conf ', Clz.fgB3, False) + Clz.print('-S -K', Clz.fgB3) + + Clz.print(' '*8+'# generate a new Key for profile bobgmail and set it as current profile (carreful with this command ',Clz.fgn7) + Clz.print(' '*8+'# if your account has no empty index - all files will be unrecoverable without the appropriate key)',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('conf ', Clz.fgB3, False) + Clz.print('-S ', Clz.fgB3, False) + Clz.print('bobgmail ', Clz.fgB1, False) + Clz.print('-K', Clz.fgB3) + + printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) + print() def load_profile(self,o): diff --git a/impra/core.py b/impra/core.py index 1cb8e67..854210e 100644 --- a/impra/core.py +++ b/impra/core.py @@ -44,7 +44,7 @@ from mmap import mmap from os import remove, urandom, sep from os.path import abspath, dirname, join, realpath, basename, getsize, splitext from re import split as regsplit, match as regmatch, compile as regcompile, search as regsearch -from time import time +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.crypt import Kirmah, ConfigKey, Noiser, Randomiz, hash_sha256, hash_md5_file, BadKeyException @@ -246,7 +246,7 @@ class ImpraIndex: """""" EXT = 3 """""" - OWNER = 4 + USER = 4 """""" CATG = 5 """""" @@ -260,7 +260,7 @@ class ImpraIndex: """""" FILE_CRYPT = 'c' """""" - COLS = ('HASH','LABEL','PART','TYPE','OWNER','CATEGORY','ID','BLFAG','SIZE') + COLS = ('HASH','LABEL','PART','TYPE','USER','CATEGORY','ID','BLFAG','SIZE') """""" def __init__(self, key, mark, encdata='', dicCategory={}, id=0): @@ -384,6 +384,37 @@ class ImpraIndex: l = [self.dic[k][self.UID] for k in r] rt.stop() return l + + def getByCategory(self,category): + """Get ids corresponding to category + :Returns: `[uid]`|None matchIds + """ + rt = RuTime(eval(__CALLER__(category))) + l = None + r = [ k for i,k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN) and regsearch(category,self.dic[k][self.CATG]) is not None ] + l = [self.dic[k][self.UID] for k in r] + rt.stop() + return l + + def getByUser(self,user): + """Get ids corresponding to category + :Returns: `[uid]`|None matchIds + """ + rt = RuTime(eval(__CALLER__(user))) + l = None + r = [ k for i,k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN) and regsearch(user,self.getUser(self.dic[k][self.USER])) is not None ] + l = [self.dic[k][self.UID] for k in r] + rt.stop() + return l + + def getIntersection(self,list1, list2): + """Get ids intercept list1 and list2 + :Returns: `[uid]`|None matchIds + """ + rt = RuTime(eval(__CALLER__())) + l = [ i for i in set(list1).intersection(set(list2))] + rt.stop() + return l def encrypt(self): """""" @@ -417,7 +448,7 @@ class ImpraIndex: print('SIZE' +' '*5 , end='') print('PART' +' '*2 , end='') print('TYPE' +' '*2 , end='') - print('OWNER' +' '*10, end='') + print('USER ' +' '*10, end='') Clz.print('CATEGORY'+' '*22, Clz.BG4+Clz.fgB7) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) @@ -427,14 +458,14 @@ class ImpraIndex: if matchIds==None or v[self.UID] in matchIds: a = '' Clz.print(str(v[self.UID]).rjust(1+ceil(len(str(v[self.UID]))/10),' ')+' ', Clz.bg1+Clz.fgB7, False) - Clz.print(' '+str(k)[0:6]+'... ' ,Clz.fgN2, False) + Clz.print(' '+str(k)[0:6]+'... ' , Clz.fgN2, False) if len(v[self.LABEL])>36 : a = '...' - Clz.print(str(v[self.LABEL][:36]+a).ljust(40,' ') ,Clz.fgN7, False) - Clz.print(formatBytes(int(v[self.SIZE]))[:8].ljust(10,' '),Clz.fgN5, False) - Clz.print(str(v[self.PARTS]).rjust(2 ,'0') +' '*2 ,Clz.fgN1, False) - Clz.print(str(v[self.EXT][:5]).ljust(7,' ') ,Clz.fgn3, False) - Clz.print(self.getUser(str(v[self.OWNER])).ljust(15,' ') ,Clz.fgn7, False) - Clz.print(str(v[self.CATG]) +' '*2 ,Clz.fgN3) + Clz.print(str(v[self.LABEL][:36]+a).ljust(40,' ') , Clz.fgN7, False) + Clz.print(formatBytes(int(v[self.SIZE]))[:8].rjust(8,' ')+' '*2 , Clz.fgN5, False) + Clz.print(str(v[self.PARTS]).rjust(2 ,'0') +' '*2 , Clz.fgN1, False) + Clz.print(str(v[self.EXT][:5]).ljust(7,' ') , Clz.fgn3, False) + Clz.print(self.getUser(str(v[self.USER])).ljust(16 ,' ') , Clz.fgn7, False) + Clz.print(str(v[self.CATG]) +' '*2 , Clz.fgN3) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) @@ -509,7 +540,7 @@ class ImpraStorage: tstamp = self.conf.get('time' ,'index') if nid is None : nid = 0 refresh = False - if tstamp is not None and (datetime.now() - datetime.strptime(tstamp[:-7], '%Y-%m-%d %H:%M:%S')) < timedelta(minutes = 1) : + if tstamp is not None and (datetime.now() - datetime.strptime(tstamp[:-7], '%Y-%m-%d %H:%M:%S')) < timedelta(minutes = 3) : # getFromFile if uid != None and file_exists(self.pathInd): # int(self.idx) == int(uid) self.idx = uid @@ -584,11 +615,11 @@ class ImpraStorage: lsrv = self.ih.searchBySubject(subject,True) return [ i for i in set(lloc).difference(set(lsrv))] - def addFile(self, path, label, usr='all', catg=''): + 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__('"%s","%s","%s"' % (path[:13]+'...',label,usr))),DEBUG_INFO) + rt = RuTime(eval(__CALLER__('"%s","%s","%s"' % (path[:13]+'...',label,catg))),DEBUG_INFO) _, ext = splitext(path) @@ -598,9 +629,9 @@ class ImpraStorage: md5 = hash_md5_file(path) print() Clz.print(' file : ' , Clz.fgn7, False) - Clz.print(path , Clz.fgN1) + Clz.print(path , Clz.fgN1) Clz.print(' md5sum : ' , Clz.fgn7, False) - Clz.print(md5 , Clz.fgN2) + Clz.print(md5 , Clz.fgN2) print() if not self.index.get(md5) : @@ -617,19 +648,20 @@ class ImpraStorage: for v in hlst['data']: print(v) + usr = self.conf.get('name','infos') ownerHash = self.mb.getHashName(usr) self.index.addUser(usr,ownerHash) - + cancel = False sendIds = [] test = True for row in hlst['data'] : - msg = self.mb.build(self.conf.get('name','infos'),usr,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') mid = self.ih.send(msg.as_string(), self.rootBox) if mid is not None : print(' ',end='') - Clz.print(' part ' , Clz.fgn7, False) - Clz.print(str(row[0]) , Clz.fgB1, False) + 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) sendIds.append((mid[1],row)) @@ -653,7 +685,7 @@ class ImpraStorage: Clz.print(' error when sending, missing parts :', Clz.fgB1) print(diff) for mid, row in sendIds : - msg = self.mb.build(self.conf.get('name','infos'),usr,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') Clz.print(' resending part ' , Clz.fgn7, False) Clz.print(str(row[0]) , Clz.fgN2, False) mid = self.ih.send(msg.as_string(), self.rootBox) @@ -731,29 +763,22 @@ class ImpraStorage: hlst = ck.getHashList(key,row[ImpraIndex.PARTS],True) ids = self._getIdsBySubject(hlst['head'][2]) if len(ids) >= row[ImpraIndex.PARTS]: - status, resp = self.ih.srv.fetch(ids[0],'(BODY[HEADER.FIELDS (TO)])') - to = bstr(resp[0][1][4:-4]) - if to == self.mb.getHashName('all')+'@'+self.mb.DOMAIN_NAME or to == self.mb.getHashName(self.conf.ini.get('name',self.conf.profile+'.infos'))+'@'+self.mb.DOMAIN_NAME : - for mid in ids : - self.ih.downloadAttachment(mid,self.fsplit.DIR_INBOX) - if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : - print(hlst['head']) - for v in hlst['data']: - print(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() - Clz.print(' deploying in ', Clz.fgn7) - Clz.print(dirname(path), Clz.fgB2) - print() - done = True - else : - print() - Clz.print(' == `' , Clz.bg3+Clz.fgB4, False) - Clz.print(str(row[ImpraIndex.LABEL]) , Clz.bg3+Clz.fgB1, False) - Clz.print('` is private == ' , Clz.bg3+Clz.fgB4) - print() + + for mid in ids : + self.ih.downloadAttachment(mid,self.fsplit.DIR_INBOX) + if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : + print(hlst['head']) + for v in hlst['data']: + print(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() + Clz.print(' deploying in ', Clz.fgn7) + Clz.print(' '+dirname(path), Clz.fgB2) + print() + done = True + else : print() Clz.print(' == `' , Clz.bg3+Clz.fgB4, False) diff --git a/impra/w32color.py b/impra/w32color.py index 474e8b5..1c65155 100644 --- a/impra/w32color.py +++ b/impra/w32color.py @@ -10,52 +10,20 @@ from ctypes import windll, Structure, c_short as SHORT, c_ushort as WORD, byref class COORD(Structure): """struct in wincon.h.""" - _fields_ = [ - ("X", SHORT), - ("Y", SHORT)] + _fields_ = [("X", SHORT),("Y", SHORT)] class SMALL_RECT(Structure): """struct in wincon.h.""" - _fields_ = [ - ("Left", SHORT), - ("Top", SHORT), - ("Right", SHORT), - ("Bottom", SHORT)] + _fields_ = [("Left", SHORT),("Top", SHORT),("Right", SHORT),("Bottom", SHORT)] class CONSOLE_SCREEN_BUFFER_INFO(Structure): """struct in wincon.h.""" - _fields_ = [ - ("dwSize", COORD), - ("dwCursorPosition", COORD), - ("wAttributes", WORD), - ("srWindow", SMALL_RECT), - ("dwMaximumWindowSize", COORD)] + _fields_ = [("dwSize", COORD),("dwCursorPosition", COORD),("wAttributes", WORD),("srWindow", SMALL_RECT),("dwMaximumWindowSize", COORD)] # winbase.h -STD_INPUT_HANDLE = -10 +STD_INPUT_HANDLE = -10 STD_OUTPUT_HANDLE = -11 -STD_ERROR_HANDLE = -12 - -# wincon.h -FOREGROUND_BLACK = 0x0000 -FOREGROUND_BLUE = 0x0001 -FOREGROUND_GREEN = 0x0002 -FOREGROUND_CYAN = 0x0003 -FOREGROUND_RED = 0x0004 -FOREGROUND_MAGENTA = 0x0005 -FOREGROUND_YELLOW = 0x0006 -FOREGROUND_GREY = 0x0007 -FOREGROUND_INTENSITY = 0x0008 # foreground color is intensified. - -BACKGROUND_BLACK = 0x0000 -BACKGROUND_BLUE = 0x0010 -BACKGROUND_GREEN = 0x0020 -BACKGROUND_CYAN = 0x0030 -BACKGROUND_RED = 0x0040 -BACKGROUND_MAGENTA = 0x0050 -BACKGROUND_YELLOW = 0x0060 -BACKGROUND_GREY = 0x0070 -BACKGROUND_INTENSITY = 0x0080 # background color is intensified. +STD_ERROR_HANDLE = -12 stdout_handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute