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
|
||||
```sh
|
||||
$ git clone https://gitlab.com/rodin_schule/image-index-py.git
|
||||
$ cd ./image-index-py
|
||||
$ cp config-def.py config.py
|
||||
git clone https://gitlab.com/rodin_schule/image-index-py.git
|
||||
cd ./image-index-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
|
||||
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)
|
||||
|
|
76
func.py
76
func.py
|
@ -20,7 +20,7 @@ class database():
|
|||
VALUES ({vals});
|
||||
""".format(table=self.name,cols=colstring,vals=valstring))
|
||||
self.connection.commit()
|
||||
|
||||
|
||||
def create_database(self, filepath):
|
||||
# create the database and tables
|
||||
self.connection = sql.connect(filepath)
|
||||
|
@ -57,13 +57,21 @@ class database():
|
|||
if not res:
|
||||
res="."
|
||||
return res
|
||||
|
||||
def get_item(self,column,where):
|
||||
tres=[]
|
||||
if column == "*":
|
||||
for col in self.collist:
|
||||
temp_list=[]
|
||||
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:
|
||||
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
|
||||
res=[]
|
||||
|
@ -81,8 +89,15 @@ class database():
|
|||
if not res:
|
||||
return ["."]
|
||||
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):
|
||||
def __init__(self,filepath = os.getcwd() + "/index.db"):
|
||||
def __init__(self,filepath = CONFIG_DIR + "/index.db"):
|
||||
self.name="META"
|
||||
self.collist=["TAGS"]
|
||||
super().__init__(filepath)
|
||||
|
@ -90,13 +105,14 @@ class metatable(database):
|
|||
super().add_index(vallist)
|
||||
|
||||
class filestable(database):
|
||||
def __init__(self,filepath = os.getcwd() + "/index.db"):
|
||||
def __init__(self,filepath = CONFIG_DIR + "/index.db"):
|
||||
self.name="FILES"
|
||||
self.collist=["FILE","HASH","TITLE","SOURCE","CATEGORY","TAGS","CONTENT"]
|
||||
super().__init__(filepath)
|
||||
|
||||
def add_index(self,filepath,category,title="",source="",tags="",content=""):
|
||||
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
|
||||
if filehash in self.get_col("HASH"):
|
||||
print("This file already exists!")
|
||||
|
@ -112,15 +128,48 @@ class filestable(database):
|
|||
print(e)
|
||||
print("COULDN'T COPY FILE TO DESTINATION!")
|
||||
return
|
||||
print("Executing")
|
||||
#print("Executing")
|
||||
vallist=[filename,filehash,title,source,category,tags,content]
|
||||
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):
|
||||
self.get_item("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:
|
||||
n=0
|
||||
temp_list=[]
|
||||
|
@ -140,10 +189,10 @@ class filestable(database):
|
|||
if success > 0:
|
||||
#print("Hey",i[0])
|
||||
if typ == "*":
|
||||
temp_list.append(self.get_item("FILE",j[0])[0])
|
||||
temp_list.append(self.get_item("*",i[0])[0])
|
||||
else:
|
||||
#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:
|
||||
temp_list.append(k)
|
||||
#print("Self: ",self.get_item(typ,i[0])[n])
|
||||
|
@ -166,6 +215,7 @@ class filestable(database):
|
|||
return ["."]
|
||||
return temp_list
|
||||
return secondlist
|
||||
|
||||
def get_hash(self,filepath):
|
||||
#https://www.quickprogrammingtips.com/python/how-to-calculate-md5-hash-of-a-file-in-python.html
|
||||
md5_hash = hashlib.md5()
|
||||
|
@ -175,7 +225,7 @@ class filestable(database):
|
|||
md5_hash.update(byte_block)
|
||||
f.close()
|
||||
return str(md5_hash.hexdigest())
|
||||
|
||||
|
||||
def search_index(self,args):
|
||||
#print(args)
|
||||
####
|
||||
|
@ -245,7 +295,6 @@ class filestable(database):
|
|||
res=[]
|
||||
if len(selection) > 1:
|
||||
n=0
|
||||
#print(selection)
|
||||
print("Found several matches:")
|
||||
for tup in selection:
|
||||
temp_list=[]
|
||||
|
@ -264,7 +313,6 @@ class filestable(database):
|
|||
print("\n\nFinal match:")
|
||||
else:
|
||||
print(type(eingabe))
|
||||
# TODO: Fix
|
||||
else:
|
||||
if selection[0] == ".":
|
||||
print("No matching entry found!")
|
||||
|
@ -273,9 +321,5 @@ class filestable(database):
|
|||
for i in tempres:
|
||||
res.append(i)
|
||||
print("\nMatch found!")
|
||||
print("Title:\t ",res[2])
|
||||
print("Category:",res[4])
|
||||
print("Filename:",res[0])
|
||||
print("Tags:\t ",res[5])
|
||||
return res
|
||||
return 1
|
125
main.py
125
main.py
|
@ -14,9 +14,31 @@ def add():
|
|||
print("{} set to 'default'".format(i))
|
||||
eingabe="default"
|
||||
args.append(eingabe)
|
||||
print(args)
|
||||
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] != ".":
|
||||
|
@ -30,19 +52,55 @@ def delete(args):
|
|||
tb.delete_index(selection)
|
||||
|
||||
def help():
|
||||
print("SYNTAX:\n\t'image-index' displays this text")
|
||||
print("\t'image-index <option> [args]' executes the command")
|
||||
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("\tsearch:\tsearches through the index (enter prompt for options);\n\t\tInstant: image-index search <words/filter>")
|
||||
print("\t\tPrompt: image-index search")
|
||||
print("\tdelete:\tdeletes an entry based on a search query;\n\t\tInstant: image-index delete <words>")
|
||||
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")
|
||||
|
||||
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:
|
||||
print("Separate the items with spaces ( )")
|
||||
print("FILTERS: -a: All types")
|
||||
|
@ -55,14 +113,25 @@ def search(args):
|
|||
print("\t -t: Title")
|
||||
args=input("Query: ")
|
||||
if len(args) > 0:
|
||||
return tb.search_index(args.split(' '))
|
||||
print("\nQuery empty!")
|
||||
return tb.search_index(args)
|
||||
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:
|
||||
if len(sys.argv) <= 1 or re.match('[hH].*',sys.argv[1]):
|
||||
help()
|
||||
exit()
|
||||
else:
|
||||
command=sys.argv[1]
|
||||
args=[]
|
||||
|
@ -71,33 +140,31 @@ def main():
|
|||
if re.match('[aA].*',command):
|
||||
if len(sys.argv) == 2:
|
||||
add()
|
||||
exit()
|
||||
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)
|
||||
exit()
|
||||
elif re.match('[sS].*',command):
|
||||
search(args)
|
||||
exit()
|
||||
elif re.match('[uU].*',command):
|
||||
update(args)
|
||||
else:
|
||||
print("No such option!")
|
||||
exit()
|
||||
tb.connection.close()
|
||||
mtb.connection.close()
|
||||
#for i in sys.argv:
|
||||
# print("Arg:" + i)
|
||||
exit()
|
||||
#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:]))
|
||||
|
||||
input("Press return...")
|
||||
|
||||
if __name__ == "__main__":
|
||||
filepath=CONFIG_DIR + '/index.db'
|
||||
tb = filestable(filepath)
|
||||
mtb = metatable(filepath)
|
||||
tb = filestable()
|
||||
mtb = metatable()
|
||||
main()
|
||||
print("Stopping")
|
||||
tb.connection.close()
|
||||
|
|
Loading…
Add table
Reference in a new issue