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
!/config-def.py
#!/default
!/func.py
#!/index.db
#!/Animals
!/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
from uuid import uuid4
import sqlite3 as sql
from vars import *
from config import *
class database():
def __init__(self,filepath):
@ -39,6 +39,12 @@ class database():
TAGS TEXT PRIMARY KEY NOT NULL
); """
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))
@ -51,7 +57,7 @@ class database():
if not res:
res="."
return res
def get_item(self,column,where = "."):
def get_item(self,column,where):
if column == "*":
for col in self.collist:
self.crsr.execute("SELECT * FROM {} WHERE {} GLOB '*{}*'".format(self.name,col,where))
@ -109,6 +115,11 @@ class filestable(database):
print("Executing")
vallist=[filename,filehash,title,source,category,tags,content]
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?)
if firstlist:
n=0
@ -129,13 +140,14 @@ class filestable(database):
if success > 0:
#print("Hey",i[0])
if typ == "*":
temp_list.append(self.get_item("FILE",i[0])[0])
temp_list.append(self.get_item("FILE",j[0])[0])
else:
#print("TE",n,self.get_item(typ,i[0])[1])
for j in self.get_item(typ,i[0]):
temp_list.append(i)
for k in self.get_item(typ,j[0]):
if not k in temp_list:
temp_list.append(k)
#print("Self: ",self.get_item(typ,i[0])[n])
print(temp_list)
#print(temp_list)
n+=1
else:
@ -149,7 +161,7 @@ class filestable(database):
else:
#print("secondlist")
return secondlist
print("Temp_list: ",temp_list)
#print("Temp_list: ",temp_list)
if not temp_list:
return ["."]
return temp_list
@ -164,8 +176,8 @@ class filestable(database):
f.close()
return str(md5_hash.hexdigest())
def search_index(self,arglist):
#print(arglist)
def search_index(self,args):
#print(args)
####
## WARNING!!!!!! UGLY CODE INCOMING!!!!!!
####
@ -178,7 +190,7 @@ class filestable(database):
source=[]
title=[]
tags=[]
for arg in arglist:
for arg in args:
if re.match('^[-]\w{1}$', arg):
if arg == "-h":
snext="hash"
@ -234,10 +246,12 @@ class filestable(database):
if len(selection) > 1:
n=0
#print(selection)
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])

59
main.py
View file

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