Made better prompts; made meta search specific

This commit is contained in:
Michael Rodin 2022-11-23 23:00:08 +01:00
parent cd0b4767fe
commit 584a5b987c

View file

@ -1,17 +1,15 @@
#!/bin/python3 #!/bin/python3
import os import os,sys,shutil,hashlib,re,subprocess
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
from pathlib import Path from pathlib import Path
from uuid import uuid4 from uuid import uuid4
import sqlite3 as sql 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(): class database():
def __init__(self,filepath): def __init__(self,filepath = CONFIG_DIR + "/index.db"):
self.connection=None self.connection=None
self.crsr=None self.crsr=None
if not os.path.exists(filepath) : if not os.path.exists(filepath) :
@ -69,26 +67,30 @@ class database():
for i in tres: for i in tres:
res.append(i) res.append(i)
# if the table is empty, return ".". # if the table is empty, return ".".
#print(column,"res ",res)
if not res: if not res:
res="." res="."
return res return res
def get_item(self,column,where): def get_item(self,column,where,specific=False):
tres=[] tres=[]
if column == "*": if column == "*":
for col in self.collist: for col in self.collist:
temp_list=[] 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)) self.crsr.execute("SELECT * FROM {} WHERE {} GLOB '*{}*'".format(self.name,col,where))
temp_list=self.crsr.fetchall() temp_list=self.crsr.fetchall()
if temp_list: if temp_list:
for i in temp_list: for i in temp_list:
if not i in tres: if not i in tres:
tres.append(i) tres.append(i)
else:
if specific:
self.crsr.execute("SELECT * FROM {} WHERE {}='{}'".format(self.name,column,where))
else: else:
self.crsr.execute("SELECT * FROM {} WHERE {} GLOB '*{}*'".format(self.name,column,where)) self.crsr.execute("SELECT * FROM {} WHERE {} GLOB '*{}*'".format(self.name,column,where))
tres=self.crsr.fetchall() tres=self.crsr.fetchall()
#print("Tres: ",tres)
n=0 n=0
res=[] res=[]
for i in tres: for i in tres:
@ -100,7 +102,6 @@ class database():
res.append(i) res.append(i)
m+=1 m+=1
n+=1 n+=1
#print("RES: ",tres)
# if the table is empty, return ".". # if the table is empty, return ".".
if not res: if not res:
return ["."] return ["."]
@ -118,7 +119,6 @@ class database():
temp_list=[] temp_list=[]
for j in tup: for j in tup:
temp_list.append(j) temp_list.append(j)
#print("sdf",temp_list)
print("Match [{}]".format(n)) print("Match [{}]".format(n))
if self.name == "FILES": if self.name == "FILES":
print("\tTitle:\t ",temp_list[2]) print("\tTitle:\t ",temp_list[2])
@ -169,7 +169,7 @@ class database():
return res return res
return 1 return 1
def sql_compare_list(self,typ,firstlist,secondlist): def sql_compare_list(self,typ,firstlist,secondlist,specific=False):
if isinstance(firstlist, str): if isinstance(firstlist, str):
firstlist=firstlist.split(" ") firstlist=firstlist.split(" ")
if firstlist: if firstlist:
@ -182,26 +182,30 @@ class database():
success=0 success=0
for j in firstlist: for j in firstlist:
j=j.lower() 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 j in istr:
if not success == -1: if not success == -1:
success=1 success=1
else: else:
success=-1 success=-1
if success > 0: if success > 0:
#print("Hey",i[0])
if typ == "*": if typ == "*":
temp_list.append(self.get_item("*",i[0])[0]) temp_list.append(self.get_item("*",i[0],specific)[0])
else: else:
#print("TE",n,self.get_item(typ,i[0])[1]) for k in self.get_item(typ,i[0],specific):
for k in self.get_item(typ,i[0]):
if not k in temp_list: if not k in temp_list:
temp_list.append(k) temp_list.append(k)
#print("Self: ",self.get_item(typ,i[0])[n])
#print(temp_list)
n+=1 n+=1
else: else:
#print("Second: ",secondlist)
if not secondlist[0] == ".": if not secondlist[0] == ".":
for i in secondlist: for i in secondlist:
for j in firstlist: for j in firstlist:
@ -209,9 +213,7 @@ class database():
temp_list.append(secondlist[n]) temp_list.append(secondlist[n])
n+=1 n+=1
else: else:
#print("secondlist")
return secondlist return secondlist
#print("Temp_list: ",temp_list)
if not temp_list: if not temp_list:
return ["."] return ["."]
return temp_list return temp_list
@ -245,7 +247,10 @@ class metatable(database):
def get_alias(self,arg): def get_alias(self,arg):
selection=self.search_index(arg,"strict") selection=self.search_index(arg,"strict")
item=selection[0] item=selection[0]
if item[0] != ".":
alias=item[1] alias=item[1]
else:
alias = "."
return alias return alias
def get_name(self,arg): def get_name(self,arg):
@ -256,7 +261,7 @@ class metatable(database):
def search_index(self,args,quiet=True): def search_index(self,args,quiet=True):
selection=[] selection=[]
selection=self.sql_compare_list("*", args, selection) selection=self.sql_compare_list("*", args, selection,True)
selection=self.select_index(selection,quiet) selection=self.select_index(selection,quiet)
return selection return selection
@ -266,25 +271,23 @@ class filestable(database):
self.collist=["FILE","HASH","TITLE","SOURCE","CATEGORY","TAGS","CONTENT"] self.collist=["FILE","HASH","TITLE","SOURCE","CATEGORY","TAGS","CONTENT"]
super().__init__(filepath) 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 filehash=self.get_hash(filepath) # make hash of file before copy
if filehash in str(self.get_col("HASH")): if filehash in str(self.get_col("HASH")):
print("This file already exists!") print("This file already exists!")
return return
n=0 n=0
# get the name of the category from the meta table # get the name of the category from the meta table
if category: if not category:
category="default"
name=ctb.get_name(category) name=ctb.get_name(category)
if name != ".": if name != ".":
category=name category=name
else: else:
ctb.add_index(category.lower(), category) ctb.add_index(category.lower(), category)
category=category.lower() category=category.lower()
else:
category="default"
# get the name of the tags from the meta table # get the name of the tags from the meta table
#print(tags)
tags_list=[] tags_list=[]
for tag in tags.split(','): for tag in tags.split(','):
name=ttb.get_name(tag) name=ttb.get_name(tag)
@ -293,11 +296,8 @@ class filestable(database):
else: else:
ttb.add_index(tag.lower(), tag) ttb.add_index(tag.lower(), tag)
tags_list.append(tag.lower()) tags_list.append(tag.lower())
#print(tag,tags_list)
#print("added tags:",",".join(tags_list))
tags=",".join(tags_list) tags=",".join(tags_list)
#category="default" if not category else category
filetype=os.path.splitext(filepath)[1] filetype=os.path.splitext(filepath)[1]
filename=str(uuid4()) + filetype filename=str(uuid4()) + filetype
if not os.path.exists("{}/{}".format(ROOT_DIR,category)): if not os.path.exists("{}/{}".format(ROOT_DIR,category)):
@ -309,7 +309,6 @@ class filestable(database):
print(e) print(e)
print("COULDN'T COPY FILE TO DESTINATION!") print("COULDN'T COPY FILE TO DESTINATION!")
return return
#print("Executing")
vallist=[filename,filehash,title,source,category,tags,content] vallist=[filename,filehash,title,source,category,tags,content]
super().add_index(vallist) super().add_index(vallist)
@ -349,7 +348,6 @@ class filestable(database):
name=ctb.get_name(update) name=ctb.get_name(update)
if name != ".": if name != ".":
update=name update=name
#print("Moving file to {}/{}/{}".format(ROOT_DIR,update,filename))
if not os.path.exists("{}/{}".format(ROOT_DIR,update)): if not os.path.exists("{}/{}".format(ROOT_DIR,update)):
os.makedirs("{}/{}".format(ROOT_DIR,update)) os.makedirs("{}/{}".format(ROOT_DIR,update))
shutil.move("{}/{}/{}".format(ROOT_DIR,category,filename), "{}/{}/{}".format(ROOT_DIR,update,filename)) 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) super().update_index(typ, update, "HASH", filehash)
def get_hash(self,filepath): 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() md5_hash = hashlib.md5()
# hash selected file in chunks of 4KiB, read the link above if you ask why. # hash selected file in chunks of 4KiB, read the link above if you ask why.
with open(filepath,"rb") as f: with open(filepath,"rb") as f:
@ -445,26 +443,33 @@ class filestable(database):
return self.select_index(selection,quiet) return self.select_index(selection,quiet)
def add(): def add(args):
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"]: for i in ["Filepath","Category","Title","Source","Tags","Content"]:
try:
print("{}: {}".format(i,args[n]))
except Exception:
if i == "Tags": if i == "Tags":
extra=" (Separate with ',')" extra=" (Separate with ',')"
else: else:
extra="" extra=""
eingabe = input("Enter {}{}: ".format(i,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: if i in ["Category"] and not eingabe:
print("{} set to 'default'".format(i)) print("{} set to 'default'".format(i))
eingabe="default" eingabe="default"
args.append(eingabe) 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]) tb.add_index(args[0],args[1],args[2],args[3],args[4],args[5])
def check(args): def check(args):
@ -553,9 +558,11 @@ def meta(args):
if len(args) <= 1: if len(args) <= 1:
print("Options: Category, Tags") print("Options: Category, Tags")
args.append(input("Input one type to check from the list above: ")) args.append(input("Input one type to check from the list above: "))
command=args[0] command=args[0]
typ=args[1] 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:] args=args[2:]
if re.match('[cC].*',command): if re.match('[cC].*',command):
meta_check(typ,args) meta_check(typ,args)
@ -602,16 +609,16 @@ def meta_update(typ,args):
elif re.match('[tT].*',typ): elif re.match('[tT].*',typ):
sel_list=ttb.search_index(search) sel_list=ttb.search_index(search)
for item in sel_list: for item in sel_list:
print("Item",item[0])
ttb.update_index("ALIAS", update, "NAME", item[0]) ttb.update_index("ALIAS", update, "NAME", item[0])
else: else:
print("The first argument need to be either 'Category' or 'Tags'!") print("The first argument need to be either 'Category' or 'Tags'!")
return 1 return 1
if item[0] != ".":
print("Updated {} to {}".format(item[0],update)) print("Updated {} to {}".format(item[0],update))
def meta_help(): def meta_help():
print("SYNTAX: image-index meta <option> [args]") 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("\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]") 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: else:
os.startfile(filepath) os.startfile(filepath)
def update(args): # TODO: Add or remove tags! def update(args):
if len(args) > 2: 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] typ=args[0]
update=args[1] 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: for sel in selection:
if sel[0] != ".": if sel[0] != ".":
tb.update_index(typ,update,sel) tb.update_index(typ,update,sel)
@ -655,7 +656,6 @@ def update(args): # TODO: Add or remove tags!
def repair(err_list): def repair(err_list):
sel_list=[] sel_list=[]
for i in err_list: for i in err_list:
#print(i)
if not i in sel_list: if not i in sel_list:
sel_list.append(i) sel_list.append(i)
for tup in sel_list: for tup in sel_list:
@ -719,13 +719,7 @@ def main():
for i in sys.argv[2:]: for i in sys.argv[2:]:
args.append(i) args.append(i)
if re.match('[aA].*',command): if re.match('[aA].*',command):
if len(sys.argv) == 2: add(args)
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
elif re.match('[mM].*',command): elif re.match('[mM].*',command):
meta(args) meta(args)
elif re.match('[cC].*',command): elif re.match('[cC].*',command):