image-index-py/main.py
2022-11-16 19:30:32 +01:00

171 lines
5.5 KiB
Python
Executable file

#!/bin/python3
import sys,os,re
from func import *
from config import *
def add():
args=[]
for i in ["Filepath","Category","Title","Source","Tags","Content"]:
eingabe = input("Enter {}: ".format(i))
if i in ["Filepath"] and not eingabe:
print("{} has to not be empty!".format(i))
return 1
if i in ["Category"] and not eingabe:
print("{} set to 'default'".format(i))
eingabe="default"
args.append(eingabe)
tb.add_index(args[0],args[1],args[2],args[3],args[4],args[5])
def check(args):
success=0
if not args:
hash_list,path_list=tb.check_index()
if hash_list:
print("{} file{} faulty!".format(len(hash_list),"s are" if len(hash_list) > 1 else " is"))
success=-1
if path_list:
print("{} file{} missing!".format(int(len(path_list)/2),"s are" if int(len(path_list)/2) > 1 else " is"))
success=-1
if success >= 0:
print("Everything is good!")
if hash_list:
eingabe=input("Do you want to remove the faulty files? [y/N]: ")
if re.match('[yY]',eingabe):
print("Removing faulty files...")
repair(hash_list)
if path_list:
eingabe=input("Do you want to remove the orphaned entries? [Y/n]: ")
if not re.match('[nN]',eingabe):
print("Removing orphaned entries...")
repair(path_list)
def delete(args):
selection=search(args)
if selection[0] != ".":
try:
category=selection[4]
filename=selection[0]
os.remove("{}/{}".format(category,filename))
except Exception:
print("Couldn't delete file!")
return 1
tb.delete_index(selection)
def help():
print("SYNTAX:\n\t'image-index <option> [args]' executes the command")
print("\nOPTIONS:\n\thelp:\tdisplays this prompt")
print("\tmeta:\tdisplays help for the metadata thing")
print("\tadd:\tadds a new entry;\n\t\tInstant: image-index add <filepath> <category> <title> <source> <tags> <content>")
print("\t\tPrompt: image-index add")
print("\tcheck:\tchecks the existence and correctness of all files in the index;\n\t\tSyntax: image-index check")
print("\tdelete:\tdeletes a file and entry based on a search query;\n\t\tInstant: image-index delete <words/filters>")
print("\t\tPrompt: image-index delete")
print("\tsearch:\tsearches through the index (use prompt for list of filters);\n\t\tInstant: image-index search <words/filters>")
print("\t\tPrompt: image-index search")
def meta(args):
command=args[0]
args=args[1:]
def update(args):
if len(args) > 2:
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)
if selection[0] != ".":
tb.update_index(typ,update,selection)
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:
filepath="{}/{}/{}".format(ROOT_DIR,tup[4],tup[0])
if os.path.exists(filepath):
os.remove(filepath)
tb.delete_index(tup)
def search(args,quiet=False):
if len(args) == 0:
print("Separate the items with spaces ( )")
print("FILTERS: -a: All types")
print("\t -c: Category")
print("\t -f: Filename")
print("\t -g: Tags")
print("\t -h: Hash")
print("\t -i: Content")
print("\t -s: Source")
print("\t -t: Title")
args=input("Query: ")
if len(args) > 0:
res=tb.search_index(args.split(' '))
else:
print("\nQuery empty!")
return ["."]
else:
res=tb.search_index(args)
if not quiet and res[0] != ".":
print("Title:\t ",res[2])
print("Source:\t ",res[3])
print("Category:",res[4])
print("Filename:",res[0])
print("Hash:\t ",res[1])
print("Tags:\t ",res[5])
print("Content: ",res[6])
return res
def main():
if len(sys.argv) <= 1 or re.match('[hH].*',sys.argv[1]):
help()
else:
command=sys.argv[1]
args=[]
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
elif re.match('[mM].*',command):
meta(args)
elif re.match('[cC].*',command):
check(args)
elif re.match('[dD].*',command):
delete(args)
elif re.match('[sS].*',command):
search(args)
elif re.match('[uU].*',command):
update(args)
else:
print("No such option!")
tb.connection.close()
mtb.connection.close()
#for i in sys.argv:
# print("Arg:" + i)
input("Press return...")
if __name__ == "__main__":
filepath=CONFIG_DIR + '/index.db'
tb = filestable()
mtb = metatable()
main()