Skip to content

Commit

Permalink
refactor: Got motivation during holidays.
Browse files Browse the repository at this point in the history
Got some motivation during holidays, and will try to freshen up a little
bit my side projects once again.
This is mostly refactoring wrong formatting, missing spaces and so on.

* Starting to migrate to `logging`
* Using `os.sep` insetead of `\\`
* Add basic `requirements.txt` will revise soon again.
  • Loading branch information
userwiths committed Jan 2, 2023
1 parent ba33ee3 commit e5980c6
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 184 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.txt
!requirements.txt
*.js
__pycache__
_*
Expand Down
36 changes: 17 additions & 19 deletions declaration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
from functools import wraps
from core import Factory,ConfigAdvanced
import logging

class VNode:
def __init__(self):
Expand All @@ -10,44 +11,41 @@ def __init__(self):
self.other={}

def build(self,data:object):
dtype=type(data)
dtype = type(data)
if dtype is list:
return self.__buildList(data)
elif dtype is str:
return self.__buildString(data)
return None

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

self.other=data[2:]

self.value = data[1]
self.path = data[0]
self.name = re.split(r' |/|\\',self.path)[-1:]
self.name = self.name[0]
self.other = data[2:]
return self

def __buildString(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]
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

cache=[
{'name':'TagManager','method':'load_tags','store':None},
{'name':'IndexManager','method':'load_files','store':None},
{'name':'TagManager','method':'load_tags','store':None}
{'name': 'TagManager','method':'load_tags','store': None},
{'name': 'IndexManager','method':'load_files','store': None},
{'name': 'TagManager','method':'load_tags','store': None}
]

def invalidate(func):
@wraps(func)
def execute_and_invalidate(*args, **kwargs):
func(*args, **kwargs)
print(func.__name__+" has triggered validation !")
print('\n')
logging.info(func.__name__ + " has triggered validation !")
return execute_and_invalidate

factory=Factory()
config=ConfigAdvanced()
factory = Factory()
config = ConfigAdvanced()
4 changes: 2 additions & 2 deletions files/handlers/system_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class FileHandler(FileHandler):
This class is meant to function as an abstraction for actions upon files.
This current version allows the execution of a command in the system shell with the file passed as argument.
"""
def __init__(self,command="start",arguments=""):
def __init__(self, command="start", arguments=""):
self.shell_command=command
self.shell_arguments=arguments

def handle(self,path:str):
def handle(self, path:str):
"""
Upon a given 'path' to a file, either apply logic for different types of files or directly execute operation.
"""
Expand Down
10 changes: 5 additions & 5 deletions files/real/filesystem/files_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class FileManager(FileManager):
def __init__(self):
pass

def get_children(self,path:str):
def get_children(self, path:str):
"""
In case the given 'path' is traversible
Returns the 'children' of the given 'path'.
Expand All @@ -17,15 +17,15 @@ def get_children(self,path:str):
return os.listdir(path)
return []

def get_parent(self,path:str):
def get_parent(self, path:str):
"""
Returns one level lower path.
get_parent('/1/2/3/4/5') will return '\\1\\2\\3\\4'
"""
pathArray=re.split(r' |/|\\',path)[:-1]
return '\\'.join(pathArray)
pathArray=re.split(r' |/|\\', path)[:-1]
return os.sep.join(pathArray)

def is_traversable(self,path:str):
def is_traversable(self, path:str):
"""
Returns if the path is traversible.
"""
Expand Down
22 changes: 11 additions & 11 deletions files/virtual/custom/files_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

class VirtualManager(VirtualManager):
def __init__(self):
self.index_manager=factory.getInstanceByName(config.indexManager)
self.tag_manager=factory.getInstanceByName(config.tagsManager)
self.index_manager = factory.getInstanceByName(config.indexManager)
self.tag_manager = factory.getInstanceByName(config.tagsManager)

def get_root_items(self):
result=[]
result = []
for i in self.index_manager.get_indexed_files():
vnode=i
root=re.split(r' |/|\\',vnode.path)[0]
vnode = i
root = re.split(r' |/|\\',vnode.path)[0]

if root not in result:
result.append(root)
Expand All @@ -24,16 +24,16 @@ def get_root_items(self):
def get_all_items(self):
return self.index_manager.get_indexed_files()

def get_item(self,path:str):
def get_item(self, path:str):
return [i for i in self.index_manager.get_indexed_files() if i.path==path]

def get_children(self,path:str):
def get_children(self, path:str):
return [r for r in self.index_manager.get_indexed_files() if r.path!=path and r.path.startswith(path)]

def get_parent(self,path:str):
files=self.index_manager.get_indexed_files()
files=[i for i in files if path.startswith(i.path)]
def get_parent(self, path:str):
files = self.index_manager.get_indexed_files()
files = [i for i in files if path.startswith(i.path)]
return files

def is_traversable(self,path:str):
def is_traversable(self, path:str):
return len([i for i in self.index_manager.get_indexed_files() if i.path.startswith(path) and i.path!=path])>0
23 changes: 12 additions & 11 deletions files/virtual/onetab.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import re
import os
from declaration import config,factory,VNode

class VirtualManager:
def __init__(self):
self.index_manager=factory.getInstanceByName(config.indexManager)
self.tag_manager=factory.getInstanceByName(config.tagsManager)
self.index_manager = factory.getInstanceByName(config.indexManager)
self.tag_manager = factory.getInstanceByName(config.tagsManager)

def get_root_items(self):
result=[]
result= []
for i in self.index_manager.get_indexed_files():
vnode=VNode().build(i)
root=re.split(r' |/|\\',vnode.path)[0]
Expand All @@ -28,23 +29,23 @@ def get_item(self,path):
return [VNode().build(i) for i in self.index_manager.get_indexed_files() if i.count(path)>0]

def get_children(self,path):
result=[VNode().build(i) for i in self.index_manager.get_indexed_files() if i.count(path)>0]
result = [VNode().build(i) for i in self.index_manager.get_indexed_files() if i.count(path)>0]
return [r for r in result if result.path!=path and result.path.count(path)>0]

def get_parent(self,path):
i=-1
parts=re.split(r' |/|\\',path)
shorterPath='\\'.join(parts[:i])
files=self.index_manager.get_indexed_files()
i = -1
parts = re.split(r' |/|\\',path)
shorterPath = '\\'.join(parts[:i])
files = self.index_manager.get_indexed_files()

while i>-len(parts):
for e in files:
if e.count(shorterPath)>0 and e.cont(path)==0:
return VNode().build(e)
i=i-1
shorterPath='\\'.join(parts[:i])
i = i-1
shorterPath = os.sep.join(parts[:i])

return None

def is_traversable(self,path):
def is_traversable(self, path:str):
return len([i for i in self.index_manager.get_indexed_files() if i.count(path)>0 and len(i)>len(path)])>0
49 changes: 23 additions & 26 deletions index/csv/index_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,27 @@

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

self.delimiter=';'
self.new_line=''
self.index_item_file = config.indexFile
self.indexed_items = []
self.delimiter = ';'
self.new_line = ''

def get_indexed_files(self):
"""
Returns all currently tagged files.
"""
self.indexed_items=[]
path=self.index_item_file
self.indexed_items = []
path = self.index_item_file

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

return self.indexed_items

@invalidate
def tag_item(self,tag_number:int,item_path:str):
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 @@ -40,47 +38,46 @@ def tag_item(self,tag_number:int,item_path:str):
writer.writerow([item_path,tag_number,self.get_type(item_path)])

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

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

def get_items_with_all_tags(self,number:int):
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].endswith(str(number))]

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

return result

def is_tagged(self,item:str):
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.path==item])>0

def reindex_files(self,removed_tag_number:int):
def reindex_files(self, removed_tag_number:int):
"""
Reindex in case a tag has been deleted.
"""
Expand All @@ -92,7 +89,7 @@ def reindex_files(self,removed_tag_number:int):
else:
writer.writerow([index.path,index.value,self.get_type(index.path)])

def get_type(self,path:str):
def get_type(self, path:str):
if path.startswith("http"):
return "http"
else:
Expand Down
Loading

0 comments on commit e5980c6

Please sign in to comment.