From 584a5b987c1d384bb023ab967baa789a0b4ef961 Mon Sep 17 00:00:00 2001 From: Michael Rodin Date: Wed, 23 Nov 2022 23:00:08 +0100 Subject: [PATCH] Made better prompts; made meta search specific --- image-index | 176 +++++++++++++++++++++++++--------------------------- 1 file changed, 85 insertions(+), 91 deletions(-) diff --git a/image-index b/image-index index 835d3bd..b3f497c 100755 --- a/image-index +++ b/image-index @@ -1,17 +1,15 @@ #!/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 +import os,sys,shutil,hashlib,re,subprocess from pathlib import Path from uuid import uuid4 import sqlite3 as sql +ROOT_DIR=os.getcwd() # 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 + class database(): - def __init__(self,filepath): + def __init__(self,filepath = CONFIG_DIR + "/index.db"): self.connection=None self.crsr=None if not os.path.exists(filepath) : @@ -69,26 +67,30 @@ class database(): 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): + def get_item(self,column,where,specific=False): tres=[] if column == "*": for col in self.collist: temp_list=[] - self.crsr.execute("SELECT * FROM {} WHERE {} GLOB '*{}*'".format(self.name,col,where)) + if specific: + self.crsr.execute("SELECT * FROM {} WHERE {}='{}'".format(self.name,col,where)) + else: + 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)) + if specific: + self.crsr.execute("SELECT * FROM {} WHERE {}='{}'".format(self.name,column,where)) + 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: @@ -100,7 +102,6 @@ class database(): res.append(i) m+=1 n+=1 - #print("RES: ",tres) # if the table is empty, return ".". if not res: return ["."] @@ -118,7 +119,6 @@ class database(): 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]) @@ -169,7 +169,7 @@ class database(): return res return 1 - def sql_compare_list(self,typ,firstlist,secondlist): + def sql_compare_list(self,typ,firstlist,secondlist,specific=False): if isinstance(firstlist, str): firstlist=firstlist.split(" ") if firstlist: @@ -182,26 +182,30 @@ class database(): success=0 for j in firstlist: j=j.lower() - if j in istr: - if not success == -1: - success=1 + if specific: + for part in i: + part=part.lower() + if j == part: + if not success == -1: + success=1 + if success == 0: + success=-1 else: - success=-1 + 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]) + temp_list.append(self.get_item("*",i[0],specific)[0]) else: - #print("TE",n,self.get_item(typ,i[0])[1]) - for k in self.get_item(typ,i[0]): + for k in self.get_item(typ,i[0],specific): 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: @@ -209,9 +213,7 @@ class database(): 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 @@ -245,7 +247,10 @@ class metatable(database): def get_alias(self,arg): selection=self.search_index(arg,"strict") item=selection[0] - alias=item[1] + if item[0] != ".": + alias=item[1] + else: + alias = "." return alias def get_name(self,arg): @@ -256,7 +261,7 @@ class metatable(database): def search_index(self,args,quiet=True): selection=[] - selection=self.sql_compare_list("*", args, selection) + selection=self.sql_compare_list("*", args, selection,True) selection=self.select_index(selection,quiet) return selection @@ -266,25 +271,23 @@ class filestable(database): self.collist=["FILE","HASH","TITLE","SOURCE","CATEGORY","TAGS","CONTENT"] super().__init__(filepath) - def add_index(self,filepath,category,title="",source="",tags="",content=""): + def add_index(self,filepath,category="default",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: + if not category: category="default" + name=ctb.get_name(category) + if name != ".": + category=name + else: + ctb.add_index(category.lower(), category) + category=category.lower() # get the name of the tags from the meta table - #print(tags) tags_list=[] for tag in tags.split(','): name=ttb.get_name(tag) @@ -293,11 +296,8 @@ class filestable(database): 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)): @@ -309,7 +309,6 @@ class filestable(database): print(e) print("COULDN'T COPY FILE TO DESTINATION!") return - #print("Executing") vallist=[filename,filehash,title,source,category,tags,content] super().add_index(vallist) @@ -349,7 +348,6 @@ class filestable(database): 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)) @@ -360,7 +358,7 @@ class filestable(database): 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 + # 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: @@ -445,26 +443,33 @@ class filestable(database): return self.select_index(selection,quiet) -def add(): - args=[] +def add(args): + if len(args) >= 6: + tb.add_index(args[0],args[1],args[2],args[3],args[4],args[5]) + return + n=0 for i in ["Filepath","Category","Title","Source","Tags","Content"]: - if i == "Tags": - extra=" (Separate with ',')" - else: - extra="" - eingabe = input("Enter {}{}: ".format(i,extra)) + try: + print("{}: {}".format(i,args[n])) + except Exception: + if i == "Tags": + extra=" (Separate with ',')" + else: + extra="" + eingabe = input("Enter {}{}: ".format(i,extra)) + if i in ["Category"] and not eingabe: + print("{} set to 'default'".format(i)) + eingabe="default" + args.append(eingabe) if i in ["Filepath"]: - if not eingabe: + if not args[n]: print("{} must not be empty!".format(i)) return 1 else: - if not Path(eingabe).is_file(): - print(" The file '{}' doesn't exist!".format(eingabe)) + if not Path(args[n]).is_file(): + print(" The file '{}' doesn't exist or is not a file!".format(eingabe)) return 1 - if i in ["Category"] and not eingabe: - print("{} set to 'default'".format(i)) - eingabe="default" - args.append(eingabe) + n+=1 tb.add_index(args[0],args[1],args[2],args[3],args[4],args[5]) def check(args): @@ -553,9 +558,11 @@ def meta(args): if len(args) <= 1: print("Options: Category, Tags") args.append(input("Input one type to check from the list above: ")) - command=args[0] typ=args[1] + if not re.match('([cC]|[tT]).*', command): + print("The type has to be either 'Category' or 'Tags'!") + return 1 args=args[2:] if re.match('[cC].*',command): meta_check(typ,args) @@ -602,16 +609,16 @@ def meta_update(typ,args): elif re.match('[tT].*',typ): sel_list=ttb.search_index(search) for item in sel_list: - print("Item",item[0]) ttb.update_index("ALIAS", update, "NAME", item[0]) else: print("The first argument need to be either 'Category' or 'Tags'!") return 1 - print("Updated {} to {}".format(item[0],update)) + if item[0] != ".": + print("Updated {} to {}".format(item[0],update)) def meta_help(): print("SYNTAX: image-index meta