added update & check functions
This commit is contained in:
parent
f76e7b1b18
commit
952865fc2a
3 changed files with 167 additions and 48 deletions
14
README.md
14
README.md
|
@ -3,10 +3,18 @@ This project is a collection of scripts which can index and sort files on your p
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
```sh
|
```sh
|
||||||
$ git clone https://gitlab.com/rodin_schule/image-index-py.git
|
git clone https://gitlab.com/rodin_schule/image-index-py.git
|
||||||
$ cd ./image-index-py
|
cd ./image-index-py
|
||||||
$ cp config-def.py config.py
|
cp config-def.py config.py
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Functions
|
||||||
|
* add - Add a file and entry to the index
|
||||||
|
* search - search through the index
|
||||||
|
* delete - delete a file and remove the entry
|
||||||
|
* update - change a value of an entry in the index or move a file to another category
|
||||||
|
|
||||||
|
* check - check if all files in the index exist and have the correct hash
|
||||||
## Configuration
|
## Configuration
|
||||||
The file `config.py` holds some very important variables which you need to look at before using the index:
|
The file `config.py` holds some very important variables which you need to look at before using the index:
|
||||||
* ROOT_DIR: The absolute path of where you want to save your files (the directories for the categories will be created there)
|
* ROOT_DIR: The absolute path of where you want to save your files (the directories for the categories will be created there)
|
||||||
|
|
72
func.py
72
func.py
|
@ -57,13 +57,21 @@ class database():
|
||||||
if not res:
|
if not res:
|
||||||
res="."
|
res="."
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def get_item(self,column,where):
|
def get_item(self,column,where):
|
||||||
|
tres=[]
|
||||||
if column == "*":
|
if column == "*":
|
||||||
for col in self.collist:
|
for col in self.collist:
|
||||||
|
temp_list=[]
|
||||||
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()
|
||||||
|
if temp_list:
|
||||||
|
for i in temp_list:
|
||||||
|
if not i in tres:
|
||||||
|
tres.append(i)
|
||||||
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)
|
#print("Tres: ",tres)
|
||||||
n=0
|
n=0
|
||||||
res=[]
|
res=[]
|
||||||
|
@ -81,8 +89,15 @@ class database():
|
||||||
if not res:
|
if not res:
|
||||||
return ["."]
|
return ["."]
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
def update_index(self,typ,update,filehash):
|
||||||
|
self.crsr.execute("UPDATE {} SET {}='{}' WHERE HASH='{}'".format(self.name,typ,update,filehash))
|
||||||
|
self.connection.commit()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class metatable(database):
|
class metatable(database):
|
||||||
def __init__(self,filepath = os.getcwd() + "/index.db"):
|
def __init__(self,filepath = CONFIG_DIR + "/index.db"):
|
||||||
self.name="META"
|
self.name="META"
|
||||||
self.collist=["TAGS"]
|
self.collist=["TAGS"]
|
||||||
super().__init__(filepath)
|
super().__init__(filepath)
|
||||||
|
@ -90,13 +105,14 @@ class metatable(database):
|
||||||
super().add_index(vallist)
|
super().add_index(vallist)
|
||||||
|
|
||||||
class filestable(database):
|
class filestable(database):
|
||||||
def __init__(self,filepath = os.getcwd() + "/index.db"):
|
def __init__(self,filepath = CONFIG_DIR + "/index.db"):
|
||||||
self.name="FILES"
|
self.name="FILES"
|
||||||
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,title="",source="",tags="",content=""):
|
||||||
filehash=self.get_hash(filepath) # make of hash of file before copy
|
filehash=self.get_hash(filepath) # make of hash of file before copy
|
||||||
n=0 # TODO: show the duplicate file (Title, Category and filename)
|
n=0
|
||||||
category="default" if not category else category
|
category="default" if not category else category
|
||||||
if filehash in self.get_col("HASH"):
|
if filehash in self.get_col("HASH"):
|
||||||
print("This file already exists!")
|
print("This file already exists!")
|
||||||
|
@ -112,15 +128,48 @@ 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")
|
#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)
|
||||||
|
|
||||||
|
def check_index(self):
|
||||||
|
hash_list=[]
|
||||||
|
path_list=[]
|
||||||
|
for i in self.get_col():
|
||||||
|
filename=i[0]
|
||||||
|
category=i[4]
|
||||||
|
filehash1=i[1]
|
||||||
|
filepath="{}/{}/{}".format(ROOT_DIR,category,filename)
|
||||||
|
if not os.path.exists(filepath):
|
||||||
|
path_list.append(i)
|
||||||
|
try:
|
||||||
|
filehash2=self.get_hash(filepath)
|
||||||
|
if not filehash1 == filehash2:
|
||||||
|
hash_list.append(i)
|
||||||
|
except Exception:
|
||||||
|
path_list.append(i)
|
||||||
|
|
||||||
|
return hash_list,path_list
|
||||||
|
|
||||||
def delete_index(self,sel_list):
|
def delete_index(self,sel_list):
|
||||||
self.get_item("HASH",sel_list[1])
|
self.get_item("HASH",sel_list[1])
|
||||||
super().delete_index("HASH",sel_list[1])
|
super().delete_index("HASH",sel_list[1])
|
||||||
|
|
||||||
def sql_compare_list(self,typ,firstlist,secondlist): # TODO: Fix (fixed?)
|
def update_index(self,typ,update,sel_list):
|
||||||
|
typ=typ.upper()
|
||||||
|
if typ in ["FILE","HASH"]:
|
||||||
|
print("This type can't be changed!")
|
||||||
|
return 1
|
||||||
|
category=sel_list[4]
|
||||||
|
filehash=sel_list[1]
|
||||||
|
filename=sel_list[0]
|
||||||
|
super().update_index(typ, update, filehash)
|
||||||
|
if typ in ["CATEGORY"]:
|
||||||
|
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))
|
||||||
|
|
||||||
|
def sql_compare_list(self,typ,firstlist,secondlist):
|
||||||
if firstlist:
|
if firstlist:
|
||||||
n=0
|
n=0
|
||||||
temp_list=[]
|
temp_list=[]
|
||||||
|
@ -140,10 +189,10 @@ class filestable(database):
|
||||||
if success > 0:
|
if success > 0:
|
||||||
#print("Hey",i[0])
|
#print("Hey",i[0])
|
||||||
if typ == "*":
|
if typ == "*":
|
||||||
temp_list.append(self.get_item("FILE",j[0])[0])
|
temp_list.append(self.get_item("*",i[0])[0])
|
||||||
else:
|
else:
|
||||||
#print("TE",n,self.get_item(typ,i[0])[1])
|
#print("TE",n,self.get_item(typ,i[0])[1])
|
||||||
for k in self.get_item(typ,j[0]):
|
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("Self: ",self.get_item(typ,i[0])[n])
|
||||||
|
@ -166,6 +215,7 @@ class filestable(database):
|
||||||
return ["."]
|
return ["."]
|
||||||
return temp_list
|
return temp_list
|
||||||
return secondlist
|
return secondlist
|
||||||
|
|
||||||
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()
|
||||||
|
@ -245,7 +295,6 @@ class filestable(database):
|
||||||
res=[]
|
res=[]
|
||||||
if len(selection) > 1:
|
if len(selection) > 1:
|
||||||
n=0
|
n=0
|
||||||
#print(selection)
|
|
||||||
print("Found several matches:")
|
print("Found several matches:")
|
||||||
for tup in selection:
|
for tup in selection:
|
||||||
temp_list=[]
|
temp_list=[]
|
||||||
|
@ -264,7 +313,6 @@ class filestable(database):
|
||||||
print("\n\nFinal match:")
|
print("\n\nFinal match:")
|
||||||
else:
|
else:
|
||||||
print(type(eingabe))
|
print(type(eingabe))
|
||||||
# TODO: Fix
|
|
||||||
else:
|
else:
|
||||||
if selection[0] == ".":
|
if selection[0] == ".":
|
||||||
print("No matching entry found!")
|
print("No matching entry found!")
|
||||||
|
@ -273,9 +321,5 @@ class filestable(database):
|
||||||
for i in tempres:
|
for i in tempres:
|
||||||
res.append(i)
|
res.append(i)
|
||||||
print("\nMatch found!")
|
print("\nMatch found!")
|
||||||
print("Title:\t ",res[2])
|
|
||||||
print("Category:",res[4])
|
|
||||||
print("Filename:",res[0])
|
|
||||||
print("Tags:\t ",res[5])
|
|
||||||
return res
|
return res
|
||||||
return 1
|
return 1
|
125
main.py
125
main.py
|
@ -14,9 +14,31 @@ def add():
|
||||||
print("{} set to 'default'".format(i))
|
print("{} set to 'default'".format(i))
|
||||||
eingabe="default"
|
eingabe="default"
|
||||||
args.append(eingabe)
|
args.append(eingabe)
|
||||||
print(args)
|
|
||||||
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):
|
||||||
|
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):
|
def delete(args):
|
||||||
selection=search(args)
|
selection=search(args)
|
||||||
if selection[0] != ".":
|
if selection[0] != ".":
|
||||||
|
@ -30,19 +52,55 @@ def delete(args):
|
||||||
tb.delete_index(selection)
|
tb.delete_index(selection)
|
||||||
|
|
||||||
def help():
|
def help():
|
||||||
print("SYNTAX:\n\t'image-index' displays this text")
|
print("SYNTAX:\n\t'image-index <option> [args]' executes the command")
|
||||||
print("\t'image-index <option> [args]' executes the command")
|
|
||||||
print("\nOPTIONS:\n\thelp:\tdisplays this prompt")
|
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("\tadd:\tadds a new entry;\n\t\tInstant: image-index add <filepath> <category> <title> <source> <tags> <content>")
|
||||||
print("\t\tPrompt: image-index add")
|
print("\t\tPrompt: image-index add")
|
||||||
print("\tsearch:\tsearches through the index (enter prompt for options);\n\t\tInstant: image-index search <words/filter>")
|
print("\tcheck:\tchecks the existence and correctness of all files in the index;\n\t\tSyntax: image-index check")
|
||||||
print("\t\tPrompt: image-index search")
|
print("\tdelete:\tdeletes a file and entry based on a search query;\n\t\tInstant: image-index delete <words/filters>")
|
||||||
print("\tdelete:\tdeletes an entry based on a search query;\n\t\tInstant: image-index delete <words>")
|
|
||||||
print("\t\tPrompt: image-index delete")
|
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")
|
||||||
|
|
||||||
input("Press return...")
|
def meta(args):
|
||||||
|
command=args[0]
|
||||||
|
args=args[1:]
|
||||||
|
|
||||||
def search(args):
|
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:
|
if len(args) == 0:
|
||||||
print("Separate the items with spaces ( )")
|
print("Separate the items with spaces ( )")
|
||||||
print("FILTERS: -a: All types")
|
print("FILTERS: -a: All types")
|
||||||
|
@ -55,14 +113,25 @@ def search(args):
|
||||||
print("\t -t: Title")
|
print("\t -t: Title")
|
||||||
args=input("Query: ")
|
args=input("Query: ")
|
||||||
if len(args) > 0:
|
if len(args) > 0:
|
||||||
return tb.search_index(args.split(' '))
|
res=tb.search_index(args.split(' '))
|
||||||
print("\nQuery empty!")
|
else:
|
||||||
return tb.search_index(args)
|
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():
|
def main():
|
||||||
if len(sys.argv) <= 1:
|
if len(sys.argv) <= 1 or re.match('[hH].*',sys.argv[1]):
|
||||||
help()
|
help()
|
||||||
exit()
|
|
||||||
else:
|
else:
|
||||||
command=sys.argv[1]
|
command=sys.argv[1]
|
||||||
args=[]
|
args=[]
|
||||||
|
@ -71,33 +140,31 @@ def main():
|
||||||
if re.match('[aA].*',command):
|
if re.match('[aA].*',command):
|
||||||
if len(sys.argv) == 2:
|
if len(sys.argv) == 2:
|
||||||
add()
|
add()
|
||||||
exit()
|
|
||||||
elif len(sys.argv) >= 8:
|
elif len(sys.argv) >= 8:
|
||||||
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])
|
||||||
|
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):
|
elif re.match('[dD].*',command):
|
||||||
delete(args)
|
delete(args)
|
||||||
exit()
|
|
||||||
elif re.match('[sS].*',command):
|
elif re.match('[sS].*',command):
|
||||||
search(args)
|
search(args)
|
||||||
exit()
|
elif re.match('[uU].*',command):
|
||||||
|
update(args)
|
||||||
else:
|
else:
|
||||||
print("No such option!")
|
print("No such option!")
|
||||||
exit()
|
tb.connection.close()
|
||||||
|
mtb.connection.close()
|
||||||
#for i in sys.argv:
|
#for i in sys.argv:
|
||||||
# print("Arg:" + i)
|
# print("Arg:" + i)
|
||||||
exit()
|
input("Press return...")
|
||||||
#tb.add_index("/home/marcel/Downloads/froggy.jpg","Tiere","Fifel","https://youtu.be","Tier,Meme_template","Ein sitzender Frosch. Ist er nicht süß?")
|
|
||||||
#tb.add_index("/home/marcel/Downloads/people.jpg","Leute","Rote Leute","https://www.pexels.com","Menschen,Uniform","Leute in roten Uniformen. Warum stehen sie so? Wer weiß.")
|
|
||||||
#sys.argv+=("-t","Fifel")
|
|
||||||
sys.argv+=("-g","Meme")
|
|
||||||
#sys.argv+=("-f","ea892d6e-a20a-4784-930e-cabceb7b98ab")
|
|
||||||
print(tb.search_index(sys.argv[1:]))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
filepath=CONFIG_DIR + '/index.db'
|
filepath=CONFIG_DIR + '/index.db'
|
||||||
tb = filestable(filepath)
|
tb = filestable()
|
||||||
mtb = metatable(filepath)
|
mtb = metatable()
|
||||||
main()
|
main()
|
||||||
print("Stopping")
|
|
||||||
tb.connection.close()
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue