This commit is contained in:
a-sansara 2012-09-19 19:26:47 +02:00
parent c3a098d0d6
commit 370d010b2c
4 changed files with 35 additions and 44 deletions

View File

@ -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 :

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 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,15 +501,15 @@ 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()
@ -527,6 +528,8 @@ class ImpraStorage:
self.ih.delete(self.idx, True)
self.ih.deleteBin()
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):
""""""

View File

@ -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')
if platform.system() == 'Windows' :
clear = lambda: system('cls')
else : clear = lambda: system('clear')
#~ 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
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ class RuTime ~~

View File

@ -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 :
@ -51,6 +51,8 @@ if __name__ == '__main__':
#~ 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__ ))
#os.system('echo python imprastorage data -l')
#~ except Exception as e :
#~ print(e)