diff --git a/.gitignore b/.gitignore index 065786b..9b30e62 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,6 @@ /** !/.gitignore -!/config-def.py -#!/default -!/func.py +!/image-index #!/index.db -#!/Animals -!/main.py !/README.md \ No newline at end of file diff --git a/README.md b/README.md index 2733200..c4746f9 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,12 @@ # image-index -This project is a collection of scripts which can index and sort files on your pc. You can give every file a title, category, source, tags and content for easier finding later on. +This project is a script which can index and sort files on your pc. You can give every file a title, category, source, tags and content for easier finding later on. +All information is stored inside of an SQLite-database. Two separate tables are used to give aliases to the categories and tags, so that a user can easily change the names without modifying all affected entries. ## Functions * add - Add a file and entry to the index * check - check if all files saved in the index exist and aren't faulty * delete - delete a file and remove the entry +* meta - a command to change aliases of categorties and tags * open - open one or more files from the index in the default application (only Linux and Windows) * show - search through the index and show the matches * update - change a value of an entry in the index or move a file to another category @@ -17,6 +19,7 @@ cp config-def.py config.py ``` ## Configuration -The file `config.py` holds some very important variables which you need to look at before using the index: +The top of the file holds some very important variables that need to be looked at by the user: * ROOT_DIR: The absolute path of where you want to save your files (the directories for the categories will be created there) -* CONFIG_DIR: The absolute path of where you want to store your `index.db`-file. \ No newline at end of file +* CONFIG_DIR: The absolute path of where you want to store your `index.db`-file. +* LINUX_APP_STARTER: The linux command which can open a file in the default application. Most distributions use `xdg-open`. \ No newline at end of file diff --git a/func.py b/func.py deleted file mode 100644 index 04a7e82..0000000 --- a/func.py +++ /dev/null @@ -1,339 +0,0 @@ -import os,shutil,hashlib,re -from uuid import uuid4 -import sqlite3 as sql -from config import * - -class database(): - def __init__(self,filepath): - self.connection=None - self.crsr=None - if not os.path.exists(filepath) : - self.create_database(filepath) - else: - self.connection = sql.connect(filepath) - self.crsr = self.connection.cursor() - - def add_index(self,vallist): - # compile the options into a command for the SQLite database - colstring=",".join(self.collist) - valstring="'{}'".format("','".join(vallist)) - self.crsr.execute("""INSERT INTO {table} ({cols}) - 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) - self.crsr = self.connection.cursor() - sqlcommand = """CREATE TABLE FILES( - FILE TEXT PRIMARY KEY NOT NULL, - HASH TEXT NOT NULL, - TITLE TEXT , - SOURCE TEXT , - CATEGORY TEXT NOT NULL, - TAGS TEXT , - CONTENT TEXT - ); """ - self.crsr.execute(sqlcommand) - '''sqlcommand = """CREATE TABLE META( - CATEGORY TEXT PRIMARY KEY NOT NULL, - ALIAS TEXT, - DESC TEXT - ); """ - self.crsr.execute(sqlcommand)''' - - def delete_index(self,typ,item): - self.crsr.execute("DELETE FROM {} WHERE {}='{}'".format(self.name,typ,item)) - self.connection.commit() - return - - def get_col(self,column = "*"): - # get the column of some table. If no options given, return all columns - self.crsr.execute("SELECT {} FROM {}".format(column,self.name)) - tres=self.crsr.fetchall() - res=[] - for i in tres: - res.append(i) - # if the table is empty, return ".". - #print(column,"res ",res) - 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() - #print("Tres: ",tres) - n=0 - res=[] - for i in tres: - m=0 - for j in tres: - if i in j and n!=m: - continue - else: - res.append(i) - m+=1 - n+=1 - #print("RES: ",tres) - # if the table is empty, return ".". - 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 = CONFIG_DIR + "/index.db"): - self.name="META" - self.collist=["CATEGORY","ALIAS","DESCRIPTION"] - super().__init__(filepath) - - def add_index(self,alias,dirname,desc=""): - super().add_index(vallist) - -class filestable(database): - 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 - category="default" if not category else category - if filehash in self.get_col("HASH"): - print("This file already exists!") - return - filetype=os.path.splitext(filepath)[1] - filename=str(uuid4()) + filetype - if not os.path.exists("{}/{}".format(ROOT_DIR,category)): - os.makedirs("{}/{}".format(ROOT_DIR,category)) - # try to copy the file, return if error. - try: - shutil.copy(filepath,"{}/{}/{}".format(ROOT_DIR,category,filename)) - except Exception as e: - print(e) - print("COULDN'T COPY FILE TO DESTINATION!") - return - #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 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=[] - if not secondlist: - n=0 - for i in self.get_col(typ): - #print(i) - success=0 - for j in firstlist: - #print(j) - if j in str(i).lower(): - #print("Yay") - if not success == -1: - success=1 - else: - success=-1 - if success > 0: - #print("Hey",i[0]) - if typ == "*": - 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,i[0]): - if not k in temp_list: - temp_list.append(k) - #print("Self: ",self.get_item(typ,i[0])[n]) - #print(temp_list) - n+=1 - - else: - #print("Second: ",secondlist) - if not secondlist[0] == ".": - for i in secondlist: - for j in firstlist: - if j in i: - temp_list.append(secondlist[n]) - n+=1 - else: - #print("secondlist") - return secondlist - #print("Temp_list: ",temp_list) - if not temp_list: - 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() - # hash selected file in chunks of 4KiB, read the link above if you ask why. - with open(filepath,"rb") as f: - for byte_block in iter(lambda: f.read(4096),b""): - md5_hash.update(byte_block) - f.close() - return str(md5_hash.hexdigest()) - - def search_index(self,args,quiet=False): - #print(args) - #### - ## WARNING!!!!!! UGLY CODE INCOMING!!!!!! - #### - snext="all" - shash=[] - alle=[] - category=[] - sfile=[] - content=[] - source=[] - title=[] - tags=[] - for arg in args: - arg=arg.lower() - if re.match('^[-]\w{1}$', arg): - if arg == "-h": - snext="hash" - elif arg == "-a": # technically unneeded because of else at the end - snext="all" - elif arg == "-c": - snext="category" - elif arg == "-f": - snext="file" - elif arg == "-i": # Inhalt - snext="content" - elif arg == "-s": - snext="source" - elif arg == "-t": - snext="title" - elif arg == "-g": # Gruppe - snext="tags" - else: - snext="all" - continue - if snext == "hash": - shash.append(arg) - elif snext == "all": - alle.append(arg) - elif snext == "category": - category.append(arg) - elif snext == "file": - sfile.append(arg) - elif snext == "content": - content.append(arg) - elif snext == "source": - source.append(arg) - elif snext == "title": - title.append(arg) - elif snext == "tags": - tags.append(arg) - else: - alle.append(arg) - # search for the right items - selection=[] - selection=self.sql_compare_list("*",alle,selection) - selection=self.sql_compare_list("HASH",shash,selection) - selection=self.sql_compare_list("CATEGORY",category,selection) - selection=self.sql_compare_list("FILE",sfile,selection) - selection=self.sql_compare_list("CONTENT",content,selection) - selection=self.sql_compare_list("SOURCE",source,selection) - selection=self.sql_compare_list("TITLE",title,selection) - selection=self.sql_compare_list("TAGS",tags,selection) - - #print("Selection: ",selection) - if selection: - res=[] - if len(selection) > 1: - n=0 - print("Found several matches:") - for tup in selection: - temp_list=[] - for j in tup: - temp_list.append(j) - #print("sdf",temp_list) - print("Match [{}]".format(n)) - print("\tTitle:\t ",temp_list[2]) - print("\tCategory:",temp_list[4]) - print("\tTags:\t ",temp_list[5]) - n+=1 - eingabe=input("Enter number(s) (0-{}; '*' for all entries): ".format(n-1)) - if not eingabe: - return ["."] - num_list=[] - if re.match('[*]',eingabe): - for i in range(0,n): - num_list.append(i) - else: - num_list=eingabe.split(' ') - nminus=0 - for i in num_list: - if int(i) >= n: - print("The number {} is too big!".format(i)) - nminus+=1 - continue - res.append(selection[int(i)]) - if not quiet: - print("\n\nFinal match{}:".format("es" if len(num_list)-nminus > 1 else "")) - else: - if selection[0] == ".": - print("No matching entry found!") - return ["."] - res=selection - print("\nMatch found!") - return res - return 1 \ No newline at end of file diff --git a/image-index b/image-index new file mode 100755 index 0000000..835d3bd --- /dev/null +++ b/image-index @@ -0,0 +1,753 @@ +#!/bin/python3 +import os +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 uuid import uuid4 +import sqlite3 as sql + +class database(): + def __init__(self,filepath): + self.connection=None + self.crsr=None + if not os.path.exists(filepath) : + self.create_database(filepath) + else: + self.connection = sql.connect(filepath) + self.crsr = self.connection.cursor() + + def add_index(self,vallist): + # compile the options into a command for the SQLite database + colstring=",".join(self.collist) + valstring="'{}'".format("','".join(vallist)) + self.crsr.execute("""INSERT INTO {table} ({cols}) + 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) + self.crsr = self.connection.cursor() + sqlcommand = """CREATE TABLE FILES( + FILE TEXT PRIMARY KEY NOT NULL, + HASH TEXT NOT NULL, + TITLE TEXT , + SOURCE TEXT , + CATEGORY TEXT NOT NULL, + TAGS TEXT , + CONTENT TEXT + ); """ + self.crsr.execute(sqlcommand) + sqlcommand = """CREATE TABLE CATEGORY( + NAME TEXT PRIMARY KEY NOT NULL, + ALIAS TEXT, + DESCRIPTION TEXT + ); """ + self.crsr.execute(sqlcommand) + sqlcommand = """CREATE TABLE TAGS( + NAME TEXT PRIMARY KEY NOT NULL, + ALIAS TEXT, + DESCRIPTION TEXT + ); """ + self.crsr.execute(sqlcommand) + + def delete_index(self,typ,item): + self.crsr.execute("DELETE FROM {} WHERE {}='{}'".format(self.name,typ,item)) + self.connection.commit() + return + + def get_col(self,column = "*"): + # get the column of some table. If no options given, return all columns + self.crsr.execute("SELECT {} FROM {}".format(column,self.name)) + tres=self.crsr.fetchall() + res=[] + for i in tres: + res.append(i) + # if the table is empty, return ".". + #print(column,"res ",res) + 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() + #print("Tres: ",tres) + n=0 + res=[] + for i in tres: + m=0 + for j in tres: + if i in j and n!=m: + continue + else: + res.append(i) + m+=1 + n+=1 + #print("RES: ",tres) + # if the table is empty, return ".". + if not res: + return ["."] + return res + + def select_index(self,sel_list,quiet=False): + if quiet == "strict": + return sel_list + if sel_list: + res=[] + if len(sel_list) > 1: + n=0 + print("Found several matches:") + for tup in sel_list: + temp_list=[] + for j in tup: + temp_list.append(j) + #print("sdf",temp_list) + print("Match [{}]".format(n)) + if self.name == "FILES": + print("\tTitle:\t ",temp_list[2]) + name=ctb.get_alias(temp_list[4]) + if name != ".": + category=name + else: + category=temp_list[4] + print("\tCategory:",category) + tags_list=[] + for tag in temp_list[5].split(","): + name=ttb.get_alias(tag) + if name != ".": + tag=name + tags_list.append(tag) + print("\tTags:\t ",",".join(tags_list)) + else: + print("\tName:\t ",temp_list[0]) + print("\tAlias:\t ",temp_list[1]) + print("\tDescription: ",temp_list[2]) + n+=1 + eingabe=input("Enter number(s) (0-{}; '*' for all entries): ".format(n-1)) + if not eingabe: + return ["."] + num_list=[] + if re.match('[*]',eingabe): + for i in range(0,n): + num_list.append(i) + else: + num_list=eingabe.split(' ') + nminus=0 + for i in num_list: + if int(i) >= n: + print("The number {} is too big!".format(i)) + nminus+=1 + continue + res.append(sel_list[int(i)]) + if not quiet: + print("\nFinal match{}:".format("es" if len(num_list)-nminus > 1 else "")) + else: + if sel_list[0] == ".": + if not quiet == "strict": + print("No matching entry found!") + return ["."] + res=sel_list + if not quiet == "strict": + print("\nMatch found!") + return res + return 1 + + def sql_compare_list(self,typ,firstlist,secondlist): + if isinstance(firstlist, str): + firstlist=firstlist.split(" ") + if firstlist: + n=0 + temp_list=[] + if not secondlist: + n=0 + for i in self.get_col(typ): + istr=" ".join(i).lower() + success=0 + for j in firstlist: + j=j.lower() + if j in istr: + if not success == -1: + success=1 + else: + success=-1 + if success > 0: + #print("Hey",i[0]) + if typ == "*": + 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,i[0]): + if not k in temp_list: + temp_list.append(k) + #print("Self: ",self.get_item(typ,i[0])[n]) + #print(temp_list) + n+=1 + + else: + #print("Second: ",secondlist) + if not secondlist[0] == ".": + for i in secondlist: + for j in firstlist: + if j in i: + temp_list.append(secondlist[n]) + n+=1 + else: + #print("secondlist") + return secondlist + #print("Temp_list: ",temp_list) + if not temp_list: + return ["."] + return temp_list + return secondlist + + def update_index(self,typ,update,where,val): + self.crsr.execute("UPDATE {} SET {}='{}' WHERE {}='{}'".format(self.name,typ,update,where,val)) + self.connection.commit() + return True + +class metatable(database): + def __init__(self,typ,filepath = CONFIG_DIR + "/index.db"): + self.name=typ.upper() + self.collist=["NAME","ALIAS","DESCRIPTION"] + super().__init__(filepath) + + def add_index(self,val,alias): + super().add_index([val.lower(),alias,'']) + + def check_index(self,typ): + res=[] + for i in tb.get_col(typ): + success=0 + for j in self.get_col("NAME"): + if j[0] in i[0].lower(): + success=1 + if not i[0] in res and success == 0: + res.append(i[0]) + return res + + def get_alias(self,arg): + selection=self.search_index(arg,"strict") + item=selection[0] + alias=item[1] + return alias + + def get_name(self,arg): + selection=self.search_index(arg,"strict") + item=selection[0] + name=item[0] + return name + + def search_index(self,args,quiet=True): + selection=[] + selection=self.sql_compare_list("*", args, selection) + selection=self.select_index(selection,quiet) + return selection + +class filestable(database): + 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 hash of file before copy + if filehash in str(self.get_col("HASH")): + print("This file already exists!") + return + n=0 + # get the name of the category from the meta table + if category: + name=ctb.get_name(category) + if name != ".": + category=name + else: + ctb.add_index(category.lower(), category) + category=category.lower() + else: + category="default" + + # get the name of the tags from the meta table + #print(tags) + tags_list=[] + for tag in tags.split(','): + name=ttb.get_name(tag) + if name != ".": + tag=name + else: + ttb.add_index(tag.lower(), tag) + tags_list.append(tag.lower()) + #print(tag,tags_list) + #print("added tags:",",".join(tags_list)) + tags=",".join(tags_list) + + #category="default" if not category else category + filetype=os.path.splitext(filepath)[1] + filename=str(uuid4()) + filetype + if not os.path.exists("{}/{}".format(ROOT_DIR,category)): + os.makedirs("{}/{}".format(ROOT_DIR,category)) + # try to copy the file, return if error. + try: + shutil.copy(filepath,"{}/{}/{}".format(ROOT_DIR,category,filename)) + except Exception as e: + print(e) + print("COULDN'T COPY FILE TO DESTINATION!") + return + #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 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] + if typ in ["CATEGORY"]: + # get alias of category + name=ctb.get_name(update) + if name != ".": + update=name + #print("Moving file to {}/{}/{}".format(ROOT_DIR,update,filename)) + 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)) + if typ in ["TAGS"]: + name=ttb.get_name(update) + if name != ".": + update=name + super().update_index(typ, update, "HASH", filehash) + + 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() + # hash selected file in chunks of 4KiB, read the link above if you ask why. + with open(filepath,"rb") as f: + for byte_block in iter(lambda: f.read(4096),b""): + md5_hash.update(byte_block) + f.close() + return str(md5_hash.hexdigest()) + + def search_index(self,args,quiet=False): + snext="all" + shash=[] + alle=[] + category=[] + sfile=[] + content=[] + source=[] + title=[] + tags=[] + for arg in args: + arg=arg.lower() + if re.match('^[-]\w{1}$', arg): + if arg == "-h": + snext="hash" + elif arg == "-a": # technically unneeded because of else at the end + snext="all" + elif arg == "-c": + snext="category" + elif arg == "-f": + snext="file" + elif arg == "-i": # Inhalt + snext="content" + elif arg == "-s": + snext="source" + elif arg == "-t": + snext="title" + elif arg == "-g": # Gruppe + snext="tags" + else: + snext="all" + continue + if snext == "hash": + shash.append(arg) + elif snext == "all": + alle.append(arg) + elif snext == "category": + name=ctb.get_name(arg) + if name != ".": + arg=name + category.append(arg) + elif snext == "file": + sfile.append(arg) + elif snext == "content": + content.append(arg) + elif snext == "source": + source.append(arg) + elif snext == "title": + title.append(arg) + elif snext == "tags": + if "," in arg: + for tag in arg.split(","): + name=ttb.get_name(arg) + if name != ".": + arg=name + tags.append(arg) + else: + name=ttb.get_name(arg) + if name != ".": + arg=name + tags.append(arg) + else: + alle.append(arg) + # search for the right items + selection=[] + selection=self.sql_compare_list("*",alle,selection) + selection=self.sql_compare_list("HASH",shash,selection) + selection=self.sql_compare_list("CATEGORY",category,selection) + selection=self.sql_compare_list("FILE",sfile,selection) + selection=self.sql_compare_list("CONTENT",content,selection) + selection=self.sql_compare_list("SOURCE",source,selection) + selection=self.sql_compare_list("TITLE",title,selection) + selection=self.sql_compare_list("TAGS",tags,selection) + + return self.select_index(selection,quiet) + +def add(): + args=[] + for i in ["Filepath","Category","Title","Source","Tags","Content"]: + if i == "Tags": + extra=" (Separate with ',')" + else: + 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: + 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 + hash_list,temp_list=tb.check_index() + path_list=[] + hashcheck=pathcheck=True + verbose=False + for arg in args: + if arg == "-v": + verbose=True + elif arg == "-f": + hashcheck=False + elif arg == "-h": + pathcheck=False + + if hash_list: + print("{} file{} faulty!".format(len(hash_list),"s are" if len(hash_list) > 1 else " is")) + success=-1 + if verbose: + for tup in hash_list: + print("Title: ",tup[2]) + print("\tCategory:",tup[4]) + print("\tFilename:",tup[0]) + for i in temp_list: + if not i in path_list: + path_list.append(i) + if path_list: + print("{} file{} missing!".format(len(path_list),"s are" if len(path_list) > 1 else " is")) + success=-1 + if verbose: + for tup in path_list: + print("Title: ",tup[2]) + print("\tCategory:",tup[4]) + print("\tFilename:",tup[0]) + if success >= 0: + print("Everything is good!") + if hash_list and hashcheck: + 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 and pathcheck: + 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: image-index