Simpler and safer get_randhex() integration

This commit is contained in:
Michael Rodin 2022-12-02 11:58:11 +01:00
parent 3f62f678bd
commit 527f006e6b

View file

@ -43,14 +43,12 @@ class database():
self.crsr.execute(sqlcommand) self.crsr.execute(sqlcommand)
sqlcommand = """CREATE TABLE CATEGORY( sqlcommand = """CREATE TABLE CATEGORY(
NAME TEXT PRIMARY KEY NOT NULL, NAME TEXT PRIMARY KEY NOT NULL,
ALIAS TEXT, ALIAS TEXT
DESCRIPTION TEXT
); """ ); """
self.crsr.execute(sqlcommand) self.crsr.execute(sqlcommand)
sqlcommand = """CREATE TABLE TAGS( sqlcommand = """CREATE TABLE TAGS(
NAME TEXT PRIMARY KEY NOT NULL, NAME TEXT PRIMARY KEY NOT NULL,
ALIAS TEXT, ALIAS TEXT
DESCRIPTION TEXT
); """ ); """
self.crsr.execute(sqlcommand) self.crsr.execute(sqlcommand)
@ -243,11 +241,13 @@ class database():
class metatable(database): class metatable(database):
def __init__(self,typ,filepath = CONFIG_DIR + "/index.db"): def __init__(self,typ,filepath = CONFIG_DIR + "/index.db"):
self.name=typ.upper() self.name=typ.upper()
self.collist=["NAME","ALIAS","DESCRIPTION"] self.collist=["NAME","ALIAS"]
super().__init__(filepath) super().__init__(filepath)
def add_index(self,val,alias,randhex=""): def add_index(self,val,alias):
super().add_index([val.lower() + "-" + randhex,alias,'']) randhex=get_randhex()
val=re.sub('[ ?!/\\:!*"<>|]', '', val)
super().add_index([val[:8] + "-" + randhex,alias])
def check_index(self,typ): def check_index(self,typ):
res=[] res=[]
@ -306,7 +306,6 @@ class filestable(database):
print("This file already exists!") print("This file already exists!")
return False return False
n=0 n=0
randhex=get_randhex()
# get the name of the category from the meta table # get the name of the category from the meta table
if not category: if not category:
category="default" category="default"
@ -314,18 +313,17 @@ class filestable(database):
if name != ".": if name != ".":
category=name category=name
else: else:
ctb.add_index(category.lower(),category,randhex) ctb.add_index(category.lower(),category)
category=ctb.get_name(category) category=ctb.get_name(category)
# get the name of the tags from the meta table # get the name of the tags from the meta table
tags_list=[] tags_list=[]
for tag in tags.split(','): for tag in tags.split(','):
randhex=get_randhex()
name=ttb.get_name(tag) name=ttb.get_name(tag)
if name != ".": if name != ".":
tag=name tag=name
else: else:
ttb.add_index(tag.lower(),tag,randhex) ttb.add_index(tag.lower(),tag)
tags_list.append(ttb.get_name(tag)) tags_list.append(ttb.get_name(tag))
tags=",".join(tags_list) tags=",".join(tags_list)
@ -376,14 +374,13 @@ class filestable(database):
category=sel_list[4] category=sel_list[4]
filehash=sel_list[1] filehash=sel_list[1]
filename=sel_list[0] filename=sel_list[0]
randhex=get_randhex()
if typ in ["CATEGORY"]: if typ in ["CATEGORY"]:
# get alias of category # get alias of category
name=ctb.get_name(update) name=ctb.get_name(update)
if name != ".": if name != ".":
update=name update=name
else: else:
ctb.add_index(update.lower(),update,randhex) ctb.add_index(update.lower(),update)
update=ctb.get_name(update) update=ctb.get_name(update)
if not os.path.exists("{}/{}".format(ROOT_DIR,update)): if not os.path.exists("{}/{}".format(ROOT_DIR,update)):
os.makedirs("{}/{}".format(ROOT_DIR,update)) os.makedirs("{}/{}".format(ROOT_DIR,update))
@ -394,10 +391,9 @@ class filestable(database):
for tag in sel_list[5].split(","): for tag in sel_list[5].split(","):
tags_list.append(tag) tags_list.append(tag)
for tag in update[1:].split(","): for tag in update[1:].split(","):
randhex=get_randhex()
name=ttb.get_name(tag) name=ttb.get_name(tag)
if name == ".": if name == ".":
ttb.add_index(tag.lower(), tag, randhex) ttb.add_index(tag.lower(), tag)
name=ttb.get_name(tag) name=ttb.get_name(tag)
tags_list.append(name) tags_list.append(name)
elif update[0][0] == "-": elif update[0][0] == "-":
@ -406,7 +402,6 @@ class filestable(database):
for i in update[1:].split(","): for i in update[1:].split(","):
name=ttb.get_name(i) name=ttb.get_name(i)
if name == ".": if name == ".":
randhex=get_randhex()
ttb.add_index(tag.lower(), tag) ttb.add_index(tag.lower(), tag)
name=ttb.get_name(tag) name=ttb.get_name(tag)
if name == tag: if name == tag:
@ -415,12 +410,11 @@ class filestable(database):
tags_list.append(tag) tags_list.append(tag)
else: else:
for tag in update.split(","): for tag in update.split(","):
randhex=get_randhex()
name=ttb.get_name(tag) name=ttb.get_name(tag)
if name != ".": if name != ".":
tags_list.append(name) tags_list.append(name)
else: else:
ttb.add_index(tag.lower(), tag, randhex) ttb.add_index(tag.lower(), tag)
tags_list.append(ttb.get_name(tag)) tags_list.append(ttb.get_name(tag))
update=",".join(tags_list) update=",".join(tags_list)
super().update_index(typ, update, "HASH", filehash) super().update_index(typ, update, "HASH", filehash)
@ -542,7 +536,7 @@ def add(args):
return 1 return 1
else: else:
if not Path(args[n]).is_file(): if not Path(args[n]).is_file():
print(" The file '{}' doesn't exist or is not a file!".format(eingabe)) print(" The file '{}' doesn't exist or is not a file!".format(args[n]))
return 1 return 1
n+=1 n+=1
success=tb.add_index(args[0],args[1],args[2],args[3],args[4],args[5]) success=tb.add_index(args[0],args[1],args[2],args[3],args[4],args[5])
@ -693,16 +687,14 @@ def meta_check(typ,args):
yas=False yas=False
print("yo") print("yo")
for val in tres: for val in tres:
randhex=get_randhex()
if yas: if yas:
eingabe=input("Enter Alias for {}: ".format(val.upper())) eingabe=input("Enter Alias for {}: ".format(val.upper()))
else: else:
eingabe="" eingabe=""
if re.match('[cC].*',typ): if re.match('[cC].*',typ):
ctb.add_index(val,eingabe,randhex) ctb.add_index(val,eingabe)
elif re.match('[tT].*',typ): elif re.match('[tT].*',typ):
ttb.add_index(val,eingabe,randhex) ttb.add_index(val,eingabe)
else: else:
print("Everything is good!") print("Everything is good!")