added README; added delete function.

This commit is contained in:
Michael Rodin 2022-11-16 10:36:34 +01:00
parent 5d67424b6e
commit f76e7b1b18
5 changed files with 74 additions and 35 deletions

3
.gitignore vendored
View file

@ -1,9 +1,10 @@
/** /**
!/.gitignore !/.gitignore
!/config-def.py
#!/default #!/default
!/func.py !/func.py
#!/index.db #!/index.db
#!/Animals #!/Animals
!/main.py !/main.py
!/vars.py !/README.md

13
README.md Normal file
View file

@ -0,0 +1,13 @@
# 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.
## Installation
```sh
$ git clone https://gitlab.com/rodin_schule/image-index-py.git
$ cd ./image-index-py
$ 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:
* 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.

34
func.py
View file

@ -1,7 +1,7 @@
import os,shutil,hashlib,re import os,shutil,hashlib,re
from uuid import uuid4 from uuid import uuid4
import sqlite3 as sql import sqlite3 as sql
from vars import * from config import *
class database(): class database():
def __init__(self,filepath): def __init__(self,filepath):
@ -39,6 +39,12 @@ class database():
TAGS TEXT PRIMARY KEY NOT NULL TAGS TEXT PRIMARY KEY NOT NULL
); """ ); """
self.crsr.execute(sqlcommand)''' 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 = "*"): def get_col(self,column = "*"):
# get the column of some table. If no options given, return all columns # get the column of some table. If no options given, return all columns
self.crsr.execute("SELECT {} FROM {}".format(column,self.name)) self.crsr.execute("SELECT {} FROM {}".format(column,self.name))
@ -51,7 +57,7 @@ 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):
if column == "*": if column == "*":
for col in self.collist: for col in self.collist:
self.crsr.execute("SELECT * FROM {} WHERE {} GLOB '*{}*'".format(self.name,col,where)) self.crsr.execute("SELECT * FROM {} WHERE {} GLOB '*{}*'".format(self.name,col,where))
@ -109,6 +115,11 @@ class filestable(database):
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 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 sql_compare_list(self,typ,firstlist,secondlist): # TODO: Fix (fixed?)
if firstlist: if firstlist:
n=0 n=0
@ -129,13 +140,14 @@ 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",i[0])[0]) temp_list.append(self.get_item("FILE",j[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 j in self.get_item(typ,i[0]): for k in self.get_item(typ,j[0]):
temp_list.append(i) if not k in temp_list:
temp_list.append(k)
#print("Self: ",self.get_item(typ,i[0])[n]) #print("Self: ",self.get_item(typ,i[0])[n])
print(temp_list) #print(temp_list)
n+=1 n+=1
else: else:
@ -149,7 +161,7 @@ class filestable(database):
else: else:
#print("secondlist") #print("secondlist")
return secondlist return secondlist
print("Temp_list: ",temp_list) #print("Temp_list: ",temp_list)
if not temp_list: if not temp_list:
return ["."] return ["."]
return temp_list return temp_list
@ -164,8 +176,8 @@ class filestable(database):
f.close() f.close()
return str(md5_hash.hexdigest()) return str(md5_hash.hexdigest())
def search_index(self,arglist): def search_index(self,args):
#print(arglist) #print(args)
#### ####
## WARNING!!!!!! UGLY CODE INCOMING!!!!!! ## WARNING!!!!!! UGLY CODE INCOMING!!!!!!
#### ####
@ -178,7 +190,7 @@ class filestable(database):
source=[] source=[]
title=[] title=[]
tags=[] tags=[]
for arg in arglist: for arg in args:
if re.match('^[-]\w{1}$', arg): if re.match('^[-]\w{1}$', arg):
if arg == "-h": if arg == "-h":
snext="hash" snext="hash"
@ -234,10 +246,12 @@ class filestable(database):
if len(selection) > 1: if len(selection) > 1:
n=0 n=0
#print(selection) #print(selection)
print("Found several matches:")
for tup in selection: for tup in selection:
temp_list=[] temp_list=[]
for j in tup: for j in tup:
temp_list.append(j) temp_list.append(j)
#print("sdf",temp_list)
print("Match [{}]".format(n)) print("Match [{}]".format(n))
print("\tTitle:\t ",temp_list[2]) print("\tTitle:\t ",temp_list[2])
print("\tCategory:",temp_list[4]) print("\tCategory:",temp_list[4])

59
main.py
View file

@ -1,8 +1,8 @@
#!/bin/python3 #!/bin/python3
import sys,os,re import sys,os,re
os.chdir("ii-py")
from func import * from func import *
from vars import * from config import *
def add(): def add():
args=[] args=[]
for i in ["Filepath","Category","Title","Source","Tags","Content"]: for i in ["Filepath","Category","Title","Source","Tags","Content"]:
@ -17,9 +17,18 @@ def add():
print(args) 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 delete(args):
selection=search(args)
if selection[0] != ".":
try:
category=selection[4]
filename=selection[0]
os.remove("{}/{}".format(category,filename))
except Exception:
print("Couldn't delete file!")
return 1
tb.delete_index(selection)
def delete():
df="Hi"
def help(): def help():
print("SYNTAX:\n\t'image-index' displays this text") print("SYNTAX:\n\t'image-index' displays this text")
print("\t'image-index <option> [args]' executes the command") print("\t'image-index <option> [args]' executes the command")
@ -32,18 +41,24 @@ def help():
print("\t\tPrompt: image-index delete") print("\t\tPrompt: image-index delete")
input("Press return...") input("Press return...")
def search():
print("Separate the items with spaces ( )") def search(args):
print("FILTER: -a: All types") if len(args) == 0:
print("\t -c: Category") print("Separate the items with spaces ( )")
print("\t -f: Filename") print("FILTERS: -a: All types")
print("\t -g: Tags") print("\t -c: Category")
print("\t -h: Hash") print("\t -f: Filename")
print("\t -i: Content") print("\t -g: Tags")
print("\t -s: Source") print("\t -h: Hash")
print("\t -t: Title") print("\t -i: Content")
query=input("Query: ") print("\t -s: Source")
tb.search_index(query.split(' ')) 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)
def main(): def main():
if len(sys.argv) <= 1: if len(sys.argv) <= 1:
help() help()
@ -60,15 +75,11 @@ def main():
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])
elif re.match('[dD].*',command): elif re.match('[dD].*',command):
if len(sys.argv) == 2: delete(args)
delete() exit()
exit()
tb.del_index()
elif re.match('[sS].*',command): elif re.match('[sS].*',command):
if len(sys.argv) == 2: search(args)
search() exit()
exit()
tb.search_index(args)
else: else:
print("No such option!") print("No such option!")
exit() exit()