diff --git a/declaration.py b/declaration.py index 7d61c99..1c44517 100644 --- a/declaration.py +++ b/declaration.py @@ -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}, @@ -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 diff --git a/index/__init__.py b/index/__init__.py index 10e90e8..e8c4df7 100644 --- a/index/__init__.py +++ b/index/__init__.py @@ -1 +1 @@ -from .custom.index_manager import IndexManager \ No newline at end of file +from .csv.index_manager import IndexManager \ No newline at end of file diff --git a/index/csv/index_manager.py b/index/csv/index_manager.py new file mode 100644 index 0000000..b0d6926 --- /dev/null +++ b/index/csv/index_manager.py @@ -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)]) \ No newline at end of file diff --git a/index/custom/index_manager.py b/index/custom/index_manager.py index ac2c615..b1c2b07 100644 --- a/index/custom/index_manager.py +++ b/index/custom/index_manager.py @@ -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. @@ -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: @@ -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: diff --git a/main.py b/main.py index 177ed7e..df3cfad 100644 --- a/main.py +++ b/main.py @@ -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 diff --git a/tags/csv/tag_manager.py b/tags/csv/tag_manager.py index 0c368f7..5d6c876 100644 --- a/tags/csv/tag_manager.py +++ b/tags/csv/tag_manager.py @@ -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): diff --git a/tags/custom/tag_manager.py b/tags/custom/tag_manager.py index 555e854..2cf9602 100644 --- a/tags/custom/tag_manager.py +++ b/tags/custom/tag_manager.py @@ -15,7 +15,6 @@ def get_tags(self): return self.available_tags - @lru_cache(maxsize=4) def load_tags(self): path=self.tags_file @@ -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