Skip to content

Commit

Permalink
Added csv indexer module and fixed a few inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
userwiths committed Aug 12, 2021
1 parent 8f77eea commit fd131ab
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 31 deletions.
12 changes: 10 additions & 2 deletions declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ def __init__(self):
self.value=0
self.other={}

def build(self,data):
def build(self,data:str):
self.value=data.split(';')[1]
self.path=data.split(';')[0]
self.name=re.split(r' |/|\\',self.path)[-1:]
self.name=self.name[0]

return self

def build(self,data:list):
self.value=data[1]
self.path=data[0]
self.name=re.split(r' |/|\\',self.path)[-1:]
self.name=self.name[0]

return self

cache=[
{'name':'TagManager','method':'load_tags','store':None},
Expand All @@ -27,7 +35,7 @@ def invalidate(func):
@wraps(func)
def execute_and_invalidate(*args, **kwargs):
func(*args, **kwargs)
print(factory.getInstanceByName('TagManager').load_tags())
print(func.__name__+" has triggered validation !")
print('\n')
return execute_and_invalidate

Expand Down
2 changes: 1 addition & 1 deletion index/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .custom.index_manager import IndexManager
from .csv.index_manager import IndexManager
86 changes: 86 additions & 0 deletions index/csv/index_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import csv
import sympy
from declaration import config, factory, invalidate

class IndexManager:
def __init__(self):
self.index_item_file=config.indexFile
self.indexed_items=[]

self.delimiter=';'

def get_indexed_files(self):
self.indexed_items=[]
path=self.index_item_file

with open(path,newline='') as csvfile:
reader=csv.reader(csvfile,delimiter=self.delimiter)
for row in reader:
if len(row)==2:
self.indexed_items.append(row)

return self.indexed_items

@invalidate
def tag_item(self,tag_number:int,item_path:str):
"""
Tag a file identified by 'item_path' with the 'tag_number' that represents the collection of tags.
"""
if self.is_tagged(item_path):
self.edit_tag(tag_indexes,item_path)
return

with open(self.index_item_file, 'a', newline='') as f:
writer = csv.writer(f,delimiter=self.delimiter)
writer.writerow([item_path,tag_number])

@invalidate
def edit_tag(self,tag_number:int,item_path:str):
"""
Change tag number of a given item.
"""
files_indexes=self.indexed_items

with open(self.index_item_file, 'w', newline='') as f:
writer = csv.writer(f,delimiter=self.delimiter)
for index in files_indexes:
if index[0]==item_path:
writer.writerow([item_path,tag_number])
else:
writer.writerow(index)

def get_items_with_all_tags(self,number:int):
"""
Returns a list of the items containing ALL tags defined by 'number'
"""
return [i[0] for i in self.indexed_items if i[1].strip('\n').endswith(str(number))]

def get_items_with_any_tag(self,numbers:list):
"""
Returns a list of items tagged with ANY of the tags in 'numbers'
"""
result=[]
for item in self.indexed_items:
number=int(item[1].strip('\n'))
for index in numbers:
if number%index==0:
result.append(item)
break

return result

def is_tagged(self,item:str):
"""
Returns 'true' in case the 'item' has been tagged with any tag.
"""
return len([i for i in self.indexed_items if i[1].strip('\n')==item])>0

def reindex_files(self,removed_tag_number:int):
"""
Reindex in case a tag has been deleted.
"""
files_indexes=self.indexed_items
number=0
with open(self.index_item_file, 'w', newline='') as f:
writer = csv.writer(f,delimiter=self.delimiter)
writer.writerow([index[0],str(number)])
34 changes: 25 additions & 9 deletions index/custom/index_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
class IndexManager:
def __init__(self):
self.index_item_file=config.indexFile

self.indexed_items=[]

# Use and override for new implementations
def get_indexed_files(self):
"""
Load indexed files in memory.
"""
self.indexed_items=[]

file=open(self.index_item_file,"r")
lines=file.readlines()
self.indexed_items=file.readlines()
file.close()

return lines

@invalidate
def tag_item(self,tag_number:int,item_path:str):
"""
Tag a file identified by 'item_path' with the 'tag_number' that represents the collection of tags.
Expand All @@ -27,22 +31,33 @@ def tag_item(self,tag_number:int,item_path:str):
file.write(item_path+";"+str(tag_number)+'\n')
file.close()


def edit_tag(self,tag_indexes:list,item_path:str):
pass
@invalidate
def edit_tag(self,tag_number:int,item_path:str):
"""
Change tag number of a given item.
"""
files_indexes=self.indexed_items
file=open(self.index_item_file,'w')
for index in files_indexes:
fileName=index.split(';')[0]
if fileName==item_path:
file.write(fileName+";"+str(tag_number)+'\n')
else:
file.write(index)
file.close()

def get_items_with_all_tags(self,number:int):
"""
Returns a list of the items containing ALL tags defined by 'number'
"""
return [i.split(';')[0] for i in self.get_indexed_files() if i.strip('\n').endswith(str(number))]
return [i.split(';')[0] for i in self.indexed_items if i.strip('\n').endswith(str(number))]

def get_items_with_any_tag(self,numbers:list):
"""
Returns a list of items tagged with ANY of the tags in 'numbers'
"""
result=[]
for item in self.get_indexed_files():
for item in self.indexed_items:
number=int(item.strip('\n').split(';')[1])
for index in numbers:
if number%index==0:
Expand All @@ -55,13 +70,14 @@ def is_tagged(self,item:str):
"""
Returns 'true' in case the 'item' has been tagged with any tag.
"""
return len([i for i in self.get_indexed_files() if i.strip('\n')==item])>0
return len([i for i in self.indexed_items if i.strip('\n')==item])>0

@invalidate
def reindex_files(self,removed_tag_number:int):
"""
Reindex in case a tag has been deleted.
"""
files_indexes=self.get_indexed_files()
files_indexes=self.indexed_items
number=0
file=open(self.index_item_file,'w')
for index in files_indexes:
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def addButtons(self):
opendir.grid(row=6,column=8)

def tag_item(self):
self.tagManager.tag_item([1,2],self.tree.get_path(self.tree.selection(),False))
self.indexManager.tag_item(self.tagManager.calc_tags_number([1,2]),self.tree.get_path(self.tree.selection(),False))

def addCheckboxes(self):
index=0
Expand Down
8 changes: 0 additions & 8 deletions tags/csv/tag_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ def load_tags(self):
self.available_tags.append(row[1])

return self.tags

def tag_item(self,tag_indexes:list,item_path:str):
if self.indexManager.is_tagged(item_path):
self.edit_tag(tag_indexes,item_path)
return

number=self.calc_tags_number(tag_indexes)
self.indexManager.tag_item(number,item_path)

#Get number coresponding to a unique sequence of tags.
def calc_tags_number(self,tag_indexes:list):
Expand Down
11 changes: 1 addition & 10 deletions tags/custom/tag_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def get_tags(self):

return self.available_tags

@lru_cache(maxsize=4)
def load_tags(self):
path=self.tags_file

Expand All @@ -28,15 +27,7 @@ def load_tags(self):
self.available_tags=[line[1] for line in self.tags]

return tags

def tag_item(self,tag_indexes:list,item_path:str):
if self.indexManager.is_tagged(item_path):
self.edit_tag(tag_indexes,item_path)
return

number=self.calc_tags_number(tag_indexes)
self.indexManager.tag_item(number,item_path)


#Get number coresponding to a unique sequence of tags.
def calc_tags_number(self,tag_indexes:list):
number=1
Expand Down

0 comments on commit fd131ab

Please sign in to comment.