printing finesse

This commit is contained in:
Ville Rantanen
2016-12-22 09:36:53 +02:00
parent bf2f587a2b
commit 2b1b45601e

View File

@@ -19,6 +19,7 @@ MIME=magic.open(magic.MAGIC_NONE)
#MIME=magic.open(magic.MAGIC_MIME)
MIME.load()
ANIM=['.','·',"'","'",'·','.','_']
DEFAULT_CHUNK=1024*1024*50
def setup_options():
parser=ArgumentParser(description="Maintains the list of images sqlite file")
@@ -70,7 +71,10 @@ def add_recurse(options):
conn=sqlite3.connect(options.sqlfile)
conn.text_factory=str
db=conn.cursor()
prev_path_len=0
for path,dirs,files in os.walk(options.startpath,followlinks=options.symlinks):
sys.stdout.write("\r%s%s"%(filename_join(path,".",options),(prev_path_len-len(path))*' '))
prev_path_len=len(path)
dirs=clean_dirs(dirs)
dirs.sort()
files.sort()
@@ -94,7 +98,7 @@ def add_recurse(options):
#file content changed
add_single(conn,filename,change=True,fullfile=options.fullfile)
conn.commit()
sys.stdout.write("\n")
return
def add_single(conn,filename,change=False,hash=None,minsize=0,fullfile=False):
@@ -103,7 +107,7 @@ def add_single(conn,filename,change=False,hash=None,minsize=0,fullfile=False):
hsize=humanize_size(fsize)
except IOError:
hsize=""
print("%s (%s)"%(filename,hsize))
print("\r%s (%s)"%(filename,hsize))
db=conn.cursor()
try:
if hash==None:
@@ -123,6 +127,7 @@ def add_single(conn,filename,change=False,hash=None,minsize=0,fullfile=False):
else:
db.execute("INSERT INTO list(file,date,hash,size,mime)\
VALUES(?,?,?,?,?)",(filename,ftime,hash,fsize,mime))
sys.stdout.write('\r')
return
def checkdb(options):
@@ -140,6 +145,7 @@ def checkdb(options):
OK_count=0
for row in db:
status='OK'
sys.stdout.write("\r%s"%(row[0],))
if os.path.exists(row[0]):
md5f=get_md5(row[0],options.fullfile)
if row[1]!=md5f:
@@ -148,7 +154,7 @@ def checkdb(options):
else:
status='Not-found'
missing.append(row)
print("%s %s"%(row[0],status))
sys.stdout.write("\r%s %s\n"%(row[0],status))
if status=='OK':
OK_count+=1
if len(differing)>0:
@@ -311,22 +317,25 @@ def get_folder_contents(db,path):
files.append(base)
return files
def get_md5(filename,fullfile=False):
''' returns content based hash, only first 50Mb is read, unless user wants the whole file '''
if fullfile:
fsize=os.path.getsize(filename)
if fullfile and fsize>DEFAULT_CHUNK:
anim_i=0
anim_len=len(ANIM)
filename_len=len(filename)
block_size=2**24
chars_per_block=filename_len/(float(fsize)/block_size)
md5 = hashlib.md5()
with open(filename,'rb') as f:
for chunk in iter(lambda: f.read(block_size), b''):
sys.stdout.write('\r'+ANIM[anim_i])
sys.stdout.write('\r'+(int(anim_i*chars_per_block)*'_')+ANIM[anim_i%anim_len])
sys.stdout.flush()
anim_i=(anim_i+1)%len(ANIM)
anim_i+=1
md5.update(chunk)
sys.stdout.write('\r')
sys.stdout.write('\r'+filename_len*' ')
return md5.hexdigest()
return hashlib.md5(open(filename,'rb').read(1024*1024*50)).hexdigest()
return hashlib.md5(open(filename,'rb').read(DEFAULT_CHUNK)).hexdigest()
def has_changes(options):