196 lines
6.6 KiB
Python
Executable file
196 lines
6.6 KiB
Python
Executable file
#!/bin/python3
|
|
import sys,os,re,subprocess
|
|
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): # TODO: Option to see all faulty/missing files.
|
|
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,True)
|
|
for sel in selection:
|
|
if sel[0] != ".":
|
|
try:
|
|
category=sel[4]
|
|
filename=sel[0]
|
|
os.remove("{}/{}/{}".format(ROOT_DIR,category,filename))
|
|
except Exception as e:
|
|
print(e)
|
|
print("Couldn't delete a file!")
|
|
return 1
|
|
tb.delete_index(sel)
|
|
|
|
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("\topen:\topens a file based on a search query in the standard app;\n\t\tInstant: image-index open <words/filters>")
|
|
print("\t\tPrompt: image-index open")
|
|
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")
|
|
print("\tupdate:\tchanges specific column based on a search query;\n\t\tInstant: image-index update <column> <updated_value> <words/filters>")
|
|
print("\t\tPrompt: image-index update")
|
|
|
|
def meta(args):
|
|
command=args[0]
|
|
args=args[1:]
|
|
|
|
def open(args):
|
|
plat=sys.platform
|
|
selection=search(args,True)
|
|
for sel in selection:
|
|
if not sel[0] == ".":
|
|
filename=sel[0]
|
|
category=sel[4]
|
|
filepath="{}/{}/{}".format(ROOT_DIR,category,filename)
|
|
else:
|
|
continue
|
|
if plat.startswith('linux'):
|
|
subprocess.Popen([LINUX_APP_STARTER, filepath])
|
|
else:
|
|
os.startfile(filepath)
|
|
|
|
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)
|
|
for sel in selection:
|
|
if sel[0] != ".":
|
|
tb.update_index(typ,update,sel)
|
|
|
|
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(' '),quiet)
|
|
else:
|
|
print("\nQuery empty!")
|
|
return ["."]
|
|
else:
|
|
tres=tb.search_index(args,quiet)
|
|
if not quiet and tres[0] != ".":
|
|
for res in tres:
|
|
print("Title: ",res[2])
|
|
print("\tSource:\t ",res[3])
|
|
print("\tCategory:",res[4])
|
|
print("\tFilename:",res[0])
|
|
print("\tHash:\t ",res[1])
|
|
print("\tTags:\t ",res[5])
|
|
print("\tContent: ",res[6])
|
|
return tres
|
|
|
|
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('[oO].*',command):
|
|
open(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()
|