Made better prompts; made meta search specific
This commit is contained in:
parent
cd0b4767fe
commit
584a5b987c
138
image-index
138
image-index
|
@ -1,17 +1,15 @@
|
|||
#!/bin/python3
|
||||
import os
|
||||
ROOT_DIR=os.getcwd()+"/ii-py" # The directory where all directories and files of the index are located
|
||||
CONFIG_DIR=ROOT_DIR # The directory where the file 'index.db' is located
|
||||
LINUX_APP_STARTER="xdg-open" # The command which opens the files in the default applications
|
||||
|
||||
|
||||
import sys,shutil,hashlib,re,subprocess
|
||||
import os,sys,shutil,hashlib,re,subprocess
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
import sqlite3 as sql
|
||||
|
||||
ROOT_DIR=os.getcwd() # The directory where all directories and files of the index are located
|
||||
CONFIG_DIR=ROOT_DIR # The directory where the file 'index.db' is located
|
||||
LINUX_APP_STARTER="xdg-open" # The command which opens the files in the default applications
|
||||
|
||||
class database():
|
||||
def __init__(self,filepath):
|
||||
def __init__(self,filepath = CONFIG_DIR + "/index.db"):
|
||||
self.connection=None
|
||||
self.crsr=None
|
||||
if not os.path.exists(filepath) :
|
||||
|
@ -69,26 +67,30 @@ class database():
|
|||
for i in tres:
|
||||
res.append(i)
|
||||
# if the table is empty, return ".".
|
||||
#print(column,"res ",res)
|
||||
if not res:
|
||||
res="."
|
||||
return res
|
||||
|
||||
def get_item(self,column,where):
|
||||
def get_item(self,column,where,specific=False):
|
||||
tres=[]
|
||||
if column == "*":
|
||||
for col in self.collist:
|
||||
temp_list=[]
|
||||
if specific:
|
||||
self.crsr.execute("SELECT * FROM {} WHERE {}='{}'".format(self.name,col,where))
|
||||
else:
|
||||
self.crsr.execute("SELECT * FROM {} WHERE {} GLOB '*{}*'".format(self.name,col,where))
|
||||
temp_list=self.crsr.fetchall()
|
||||
if temp_list:
|
||||
for i in temp_list:
|
||||
if not i in tres:
|
||||
tres.append(i)
|
||||
else:
|
||||
if specific:
|
||||
self.crsr.execute("SELECT * FROM {} WHERE {}='{}'".format(self.name,column,where))
|
||||
else:
|
||||
self.crsr.execute("SELECT * FROM {} WHERE {} GLOB '*{}*'".format(self.name,column,where))
|
||||
tres=self.crsr.fetchall()
|
||||
#print("Tres: ",tres)
|
||||
n=0
|
||||
res=[]
|
||||
for i in tres:
|
||||
|
@ -100,7 +102,6 @@ class database():
|
|||
res.append(i)
|
||||
m+=1
|
||||
n+=1
|
||||
#print("RES: ",tres)
|
||||
# if the table is empty, return ".".
|
||||
if not res:
|
||||
return ["."]
|
||||
|
@ -118,7 +119,6 @@ class database():
|
|||
temp_list=[]
|
||||
for j in tup:
|
||||
temp_list.append(j)
|
||||
#print("sdf",temp_list)
|
||||
print("Match [{}]".format(n))
|
||||
if self.name == "FILES":
|
||||
print("\tTitle:\t ",temp_list[2])
|
||||
|
@ -169,7 +169,7 @@ class database():
|
|||
return res
|
||||
return 1
|
||||
|
||||
def sql_compare_list(self,typ,firstlist,secondlist):
|
||||
def sql_compare_list(self,typ,firstlist,secondlist,specific=False):
|
||||
if isinstance(firstlist, str):
|
||||
firstlist=firstlist.split(" ")
|
||||
if firstlist:
|
||||
|
@ -182,26 +182,30 @@ class database():
|
|||
success=0
|
||||
for j in firstlist:
|
||||
j=j.lower()
|
||||
if specific:
|
||||
for part in i:
|
||||
part=part.lower()
|
||||
if j == part:
|
||||
if not success == -1:
|
||||
success=1
|
||||
if success == 0:
|
||||
success=-1
|
||||
else:
|
||||
if j in istr:
|
||||
if not success == -1:
|
||||
success=1
|
||||
else:
|
||||
success=-1
|
||||
if success > 0:
|
||||
#print("Hey",i[0])
|
||||
if typ == "*":
|
||||
temp_list.append(self.get_item("*",i[0])[0])
|
||||
temp_list.append(self.get_item("*",i[0],specific)[0])
|
||||
else:
|
||||
#print("TE",n,self.get_item(typ,i[0])[1])
|
||||
for k in self.get_item(typ,i[0]):
|
||||
for k in self.get_item(typ,i[0],specific):
|
||||
if not k in temp_list:
|
||||
temp_list.append(k)
|
||||
#print("Self: ",self.get_item(typ,i[0])[n])
|
||||
#print(temp_list)
|
||||
n+=1
|
||||
|
||||
else:
|
||||
#print("Second: ",secondlist)
|
||||
if not secondlist[0] == ".":
|
||||
for i in secondlist:
|
||||
for j in firstlist:
|
||||
|
@ -209,9 +213,7 @@ class database():
|
|||
temp_list.append(secondlist[n])
|
||||
n+=1
|
||||
else:
|
||||
#print("secondlist")
|
||||
return secondlist
|
||||
#print("Temp_list: ",temp_list)
|
||||
if not temp_list:
|
||||
return ["."]
|
||||
return temp_list
|
||||
|
@ -245,7 +247,10 @@ class metatable(database):
|
|||
def get_alias(self,arg):
|
||||
selection=self.search_index(arg,"strict")
|
||||
item=selection[0]
|
||||
if item[0] != ".":
|
||||
alias=item[1]
|
||||
else:
|
||||
alias = "."
|
||||
return alias
|
||||
|
||||
def get_name(self,arg):
|
||||
|
@ -256,7 +261,7 @@ class metatable(database):
|
|||
|
||||
def search_index(self,args,quiet=True):
|
||||
selection=[]
|
||||
selection=self.sql_compare_list("*", args, selection)
|
||||
selection=self.sql_compare_list("*", args, selection,True)
|
||||
selection=self.select_index(selection,quiet)
|
||||
return selection
|
||||
|
||||
|
@ -266,25 +271,23 @@ class filestable(database):
|
|||
self.collist=["FILE","HASH","TITLE","SOURCE","CATEGORY","TAGS","CONTENT"]
|
||||
super().__init__(filepath)
|
||||
|
||||
def add_index(self,filepath,category,title="",source="",tags="",content=""):
|
||||
def add_index(self,filepath,category="default",title="",source="",tags="",content=""):
|
||||
filehash=self.get_hash(filepath) # make hash of file before copy
|
||||
if filehash in str(self.get_col("HASH")):
|
||||
print("This file already exists!")
|
||||
return
|
||||
n=0
|
||||
# get the name of the category from the meta table
|
||||
if category:
|
||||
if not category:
|
||||
category="default"
|
||||
name=ctb.get_name(category)
|
||||
if name != ".":
|
||||
category=name
|
||||
else:
|
||||
ctb.add_index(category.lower(), category)
|
||||
category=category.lower()
|
||||
else:
|
||||
category="default"
|
||||
|
||||
# get the name of the tags from the meta table
|
||||
#print(tags)
|
||||
tags_list=[]
|
||||
for tag in tags.split(','):
|
||||
name=ttb.get_name(tag)
|
||||
|
@ -293,11 +296,8 @@ class filestable(database):
|
|||
else:
|
||||
ttb.add_index(tag.lower(), tag)
|
||||
tags_list.append(tag.lower())
|
||||
#print(tag,tags_list)
|
||||
#print("added tags:",",".join(tags_list))
|
||||
tags=",".join(tags_list)
|
||||
|
||||
#category="default" if not category else category
|
||||
filetype=os.path.splitext(filepath)[1]
|
||||
filename=str(uuid4()) + filetype
|
||||
if not os.path.exists("{}/{}".format(ROOT_DIR,category)):
|
||||
|
@ -309,7 +309,6 @@ class filestable(database):
|
|||
print(e)
|
||||
print("COULDN'T COPY FILE TO DESTINATION!")
|
||||
return
|
||||
#print("Executing")
|
||||
vallist=[filename,filehash,title,source,category,tags,content]
|
||||
super().add_index(vallist)
|
||||
|
||||
|
@ -349,7 +348,6 @@ class filestable(database):
|
|||
name=ctb.get_name(update)
|
||||
if name != ".":
|
||||
update=name
|
||||
#print("Moving file to {}/{}/{}".format(ROOT_DIR,update,filename))
|
||||
if not os.path.exists("{}/{}".format(ROOT_DIR,update)):
|
||||
os.makedirs("{}/{}".format(ROOT_DIR,update))
|
||||
shutil.move("{}/{}/{}".format(ROOT_DIR,category,filename), "{}/{}/{}".format(ROOT_DIR,update,filename))
|
||||
|
@ -360,7 +358,7 @@ class filestable(database):
|
|||
super().update_index(typ, update, "HASH", filehash)
|
||||
|
||||
def get_hash(self,filepath):
|
||||
#https://www.quickprogrammingtips.com/python/how-to-calculate-md5-hash-of-a-file-in-python.html
|
||||
# https://www.quickprogrammingtips.com/python/how-to-calculate-md5-hash-of-a-file-in-python.html
|
||||
md5_hash = hashlib.md5()
|
||||
# hash selected file in chunks of 4KiB, read the link above if you ask why.
|
||||
with open(filepath,"rb") as f:
|
||||
|
@ -445,26 +443,33 @@ class filestable(database):
|
|||
|
||||
return self.select_index(selection,quiet)
|
||||
|
||||
def add():
|
||||
args=[]
|
||||
def add(args):
|
||||
if len(args) >= 6:
|
||||
tb.add_index(args[0],args[1],args[2],args[3],args[4],args[5])
|
||||
return
|
||||
n=0
|
||||
for i in ["Filepath","Category","Title","Source","Tags","Content"]:
|
||||
try:
|
||||
print("{}: {}".format(i,args[n]))
|
||||
except Exception:
|
||||
if i == "Tags":
|
||||
extra=" (Separate with ',')"
|
||||
else:
|
||||
extra=""
|
||||
eingabe = input("Enter {}{}: ".format(i,extra))
|
||||
if i in ["Filepath"]:
|
||||
if not eingabe:
|
||||
print("{} must not be empty!".format(i))
|
||||
return 1
|
||||
else:
|
||||
if not Path(eingabe).is_file():
|
||||
print(" The file '{}' doesn't exist!".format(eingabe))
|
||||
return 1
|
||||
if i in ["Category"] and not eingabe:
|
||||
print("{} set to 'default'".format(i))
|
||||
eingabe="default"
|
||||
args.append(eingabe)
|
||||
if i in ["Filepath"]:
|
||||
if not args[n]:
|
||||
print("{} must not be empty!".format(i))
|
||||
return 1
|
||||
else:
|
||||
if not Path(args[n]).is_file():
|
||||
print(" The file '{}' doesn't exist or is not a file!".format(eingabe))
|
||||
return 1
|
||||
n+=1
|
||||
tb.add_index(args[0],args[1],args[2],args[3],args[4],args[5])
|
||||
|
||||
def check(args):
|
||||
|
@ -553,9 +558,11 @@ def meta(args):
|
|||
if len(args) <= 1:
|
||||
print("Options: Category, Tags")
|
||||
args.append(input("Input one type to check from the list above: "))
|
||||
|
||||
command=args[0]
|
||||
typ=args[1]
|
||||
if not re.match('([cC]|[tT]).*', command):
|
||||
print("The type has to be either 'Category' or 'Tags'!")
|
||||
return 1
|
||||
args=args[2:]
|
||||
if re.match('[cC].*',command):
|
||||
meta_check(typ,args)
|
||||
|
@ -602,16 +609,16 @@ def meta_update(typ,args):
|
|||
elif re.match('[tT].*',typ):
|
||||
sel_list=ttb.search_index(search)
|
||||
for item in sel_list:
|
||||
print("Item",item[0])
|
||||
ttb.update_index("ALIAS", update, "NAME", item[0])
|
||||
else:
|
||||
print("The first argument need to be either 'Category' or 'Tags'!")
|
||||
return 1
|
||||
if item[0] != ".":
|
||||
print("Updated {} to {}".format(item[0],update))
|
||||
|
||||
def meta_help():
|
||||
print("SYNTAX: image-index meta <option> [args]")
|
||||
print("OPTIONS:\n\t help:\tdisplays this text")
|
||||
print("OPTIONS:\n\thelp:\tdisplays this text")
|
||||
print("\tcheck:\tcheck which items don't have an entry yet;\n\t\tSyntax: image-index meta check <Category/Tags>")
|
||||
print("\tupdate:\tchange an alias of one entry based on a search query;\n\t\tSyntax: image-index update <Category/Tags> [entry] [alias]")
|
||||
|
||||
|
@ -630,24 +637,18 @@ def sopen(args):
|
|||
else:
|
||||
os.startfile(filepath)
|
||||
|
||||
def update(args): # TODO: Add or remove tags!
|
||||
if len(args) > 2:
|
||||
def update(args):
|
||||
n=0
|
||||
for i in ["column","updated string"]:
|
||||
try:
|
||||
print(args[n])
|
||||
except Exception as e:
|
||||
eingabe=input("Enter {}: ".format(i))
|
||||
args.append(eingabe)
|
||||
n+=1
|
||||
selection=search(args[n:],True)
|
||||
typ=args[0]
|
||||
update=args[1]
|
||||
args=args[2:]
|
||||
selection=search(args,True)
|
||||
elif len(args) == 2:
|
||||
typ=args[0]
|
||||
update=args[1]
|
||||
selection=search([],True)
|
||||
elif len(args) == 1:
|
||||
typ=args[0]
|
||||
update=input("Enter the updated string: ")
|
||||
selection=search([],True)
|
||||
elif len(args) < 1:
|
||||
typ=input("Enter the column: ")
|
||||
update=input("Enter the update: ")
|
||||
selection=search([],True)
|
||||
for sel in selection:
|
||||
if sel[0] != ".":
|
||||
tb.update_index(typ,update,sel)
|
||||
|
@ -655,7 +656,6 @@ def update(args): # TODO: Add or remove tags!
|
|||
def repair(err_list):
|
||||
sel_list=[]
|
||||
for i in err_list:
|
||||
#print(i)
|
||||
if not i in sel_list:
|
||||
sel_list.append(i)
|
||||
for tup in sel_list:
|
||||
|
@ -719,13 +719,7 @@ def main():
|
|||
for i in sys.argv[2:]:
|
||||
args.append(i)
|
||||
if re.match('[aA].*',command):
|
||||
if len(sys.argv) == 2:
|
||||
add()
|
||||
elif len(sys.argv) >= 8:
|
||||
tb.add_index(args[0],args[1],args[2],args[3],args[4],args[5])
|
||||
else:
|
||||
print("Not enough arguments!")
|
||||
return
|
||||
add(args)
|
||||
elif re.match('[mM].*',command):
|
||||
meta(args)
|
||||
elif re.match('[cC].*',command):
|
||||
|
|
Loading…
Reference in a new issue