diff --git a/README.md b/README.md
index cfa0c6f..aba7c81 100644
--- a/README.md
+++ b/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)
diff --git a/func.py b/func.py
index 32b9429..d8f4ef1 100644
--- a/func.py
+++ b/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
\ No newline at end of file
diff --git a/main.py b/main.py
index fcd6929..b43c69b 100755
--- a/main.py
+++ b/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