From 370d010b2c94c2bb5e096b183fe4349c1e1c259f Mon Sep 17 00:00:00 2001 From: a-sansara Date: Wed, 19 Sep 2012 19:26:47 +0200 Subject: [PATCH] ... --- impra/cli.py | 12 ++++++++---- impra/core.py | 25 +++++++++++++++++-------- impra/util.py | 36 ++++++------------------------------ imprastorage.py | 6 ++++-- 4 files changed, 35 insertions(+), 44 deletions(-) diff --git a/impra/cli.py b/impra/cli.py index 6b5aeec..efe1573 100644 --- a/impra/cli.py +++ b/impra/cli.py @@ -30,7 +30,7 @@ # ~~ package cli ~~ from optparse import OptionParser, OptionGroup -import sys +import sys, os, platform import impra.crypt as crypt import impra.util as util import impra.core as core @@ -177,7 +177,6 @@ data command Examples: sys.exit(1) else: - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~ conf CMD ~~ if a[0] == 'conf' : @@ -267,7 +266,9 @@ you can remove index but all presents files on the box %s will be unrecoverable print(mid,status,resp) elif o.add : - impst.addFile(o.add[0],o.add[1],o.user,o.category) + done = impst.addFile(o.add[0],o.add[1],o.user,o.category) + if done : + print('OK') elif o.get : ids = [] @@ -277,7 +278,10 @@ you can remove index but all presents files on the box %s will be unrecoverable else: ids.append(sid) for sid in ids : key = impst.index.getById(str(sid)) - if key !=None : impst.getFile(key) + if key !=None : + done = impst.getFile(key) + if done : + print('OK') else: print('-- `%s` is not a valid id --' % sid) elif o.search : diff --git a/impra/core.py b/impra/core.py index 577951a..6b77f77 100644 --- a/impra/core.py +++ b/impra/core.py @@ -46,7 +46,7 @@ from os.path import abspath, dirname, join, realpath, basename, get from re import split as regsplit, match as regmatch, compile as regcompile, search as regsearch from time import time 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, C +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, C, clear from impra.crypt import Kirmah, ConfigKey, Noiser, Randomiz, hash_sha256, hash_md5_file, BadKeyException LINE_SEP = C.IBLACK+'―'*120+C.OFF @@ -394,6 +394,7 @@ class ImpraIndex: def print(self,header='', matchIds=None): """Print index content as formated bloc""" data = self.toString(matchIds).split(self.SEP_ITEM) + clear() print(header) print(C.ON_IBLACK+' \ %s%s%s' % (C.BYELLOW,'ID' ,' '*1 )+'\ @@ -500,22 +501,22 @@ class ImpraStorage: nid = self.conf.get('nid' ,'index') 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) : # getFromFile if uid != None and file_exists(self.pathInd): # int(self.idx) == int(uid) self.idx = uid encData = get_file_content(self.pathInd) - print('--\nindex in cache') - else : - print('refresh index') + else: refresh = True + else: refresh = True + if refresh : self._getIdIndex() if self.idx : encData = self._getCryptIndex() with open(self.pathInd, mode='w', encoding='utf-8') as o: o.write(encData) self.conf.set('time',str(datetime.now()),'index') - + index = ImpraIndex(self.conf.get('key','keys'),self.conf.get('mark','keys'), encData, self.getIndexDefaultCatg(), int(nid)) rt.stop() return index @@ -526,7 +527,9 @@ class ImpraStorage: if self.idx : self.ih.delete(self.idx, True) self.ih.deleteBin() - self.conf.rem('*','index') + self.conf.rem('*','index') + self.idx = None + remove(self.pathInd) def saveIndex(self): """""" @@ -547,6 +550,7 @@ class ImpraStorage: self.conf.set('time',str(datetime.now()),'index') self.clean() rt.stop() + return True def encryptTextFile(self,path): """""" @@ -563,6 +567,7 @@ class ImpraStorage: def addFile(self, path, label, usr='all', 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) @@ -619,17 +624,19 @@ class ImpraStorage: else : self.index.add(hlst['head'][3],label,hlst['head'][1],ext,ownerHash,catg,md5,bFlag) - self.saveIndex() + done = self.saveIndex() self.conf.set('nid', str(self.index.id),'index') else : print('--\nfile already exist on server as `%s` [id:%i]\n' % (self.index.dic[md5][ImpraIndex.LABEL],self.index.dic[md5][ImpraIndex.UID])) except Exception as e : print(e) rt.stop() + return done def getFile(self,key): """""" from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO + done = False row = self.index.get(key) if row==None : print('--\n%s not on the server' % key) @@ -651,12 +658,14 @@ class ImpraStorage: 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) + done = True else : print('--\n`%s` is private' % row[ImpraIndex.LABEL]) else : print('--\n`%s` invalid count parts %i/%i' %(row[ImpraIndex.LABEL],len(ids),row[ImpraIndex.PARTS])) rt.stop() + return done def clean(self): """""" diff --git a/impra/util.py b/impra/util.py index b7dc771..cc9aa79 100755 --- a/impra/util.py +++ b/impra/util.py @@ -34,12 +34,13 @@ from inspect import stack from errno import EEXIST from hashlib import sha256 from math import log, floor, ceil -from os import urandom, popen, sep, makedirs +from os import urandom, popen, sep, makedirs, system from os.path import dirname, realpath, abspath, join from random import choice from re import split as regsplit from subprocess import PIPE, Popen from sys import stderr, executable as pyexec +import platform #~ from sys.stdout import isatty from time import time @@ -190,35 +191,10 @@ def __CALLER__(args=''): else: val += "'" return val -def hilite(string, color=32, bold=True): - """""" - global COLOR_MODE - if COLOR_MODE and True: - attr = [color] - if bold: - attr.append('1') - - #~ print('\033[1;30mGray like Ghost\033[1;m') - #~ print('\033[1;31mRed like Radish\033[1;m') - #~ print('\033[1;32mGreen like Grass\033[1;m') - #~ print('\033[1;33mYellow like Yolk\033[1;m') - #~ print('\033[1;34mBlue like Blood\033[1;m') - #~ print('\033[1;35mMagenta like Mimosa\033[1;m') - #~ print('\033[1;36mCyan like Caribbean\033[1;m') - #~ print('\033[1;37mWhite like Whipped Cream\033[1;m') - #~ print('\033[1;38mCrimson like Chianti\033[1;m') - #~ print('\033[1;41mHighlighted Red like Radish\033[1;m') - #~ print('\033[1;42mHighlighted Green like Grass\033[1;m') - #~ print('\033[1;43mHighlighted Brown like Bear\033[1;m') - #~ print('\033[1;44mHighlighted Blue like Blood\033[1;m') - #~ print('\033[1;45mHighlighted Magenta like Mimosa\033[1;m') - #~ print('\033[1;46mHighlighted Cyan like Caribbean\033[1;m') - #~ print('\033[1;47mHighlighted Gray like Ghost\033[1;m') - #~ print('\033[1;48mHighlighted Crimson like Chianti\033[1;m') - - string = '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string) - print(string) - return string +if platform.system() == 'Windows' : + clear = lambda: system('cls') +else : clear = lambda: system('clear') + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~ class RuTime ~~ diff --git a/imprastorage.py b/imprastorage.py index 45c02df..8b3fd8e 100755 --- a/imprastorage.py +++ b/imprastorage.py @@ -29,7 +29,7 @@ from impra.core import ImpraConf, ImpraStorage from impra.util import IniFile, RuTime, get_file_path from impra.cli import Cli - +import sys, os if __name__ == '__main__': #~ try : @@ -50,7 +50,9 @@ if __name__ == '__main__': #~ print('\033[1;46mHighlighted Cyan like Caribbean\033[1;m') #~ print('\033[1;47mHighlighted Gray like Ghost\033[1;m') #~ print('\033[1;48mHighlighted Crimson like Chianti\033[1;m') - Cli(get_file_path(__file__ )) + Cli(get_file_path(__file__ )) + #os.system('echo python imprastorage data -l') + #~ except Exception as e : #~ print(e)