orvanitag/orvani.py

162 lines
5.5 KiB
Python
Raw Normal View History

2023-06-05 23:57:44 +03:00
import os
import yaml
2023-06-06 01:10:24 +03:00
import shutil
2023-06-05 23:57:44 +03:00
class OrvaniTag:
2023-06-06 12:11:51 +03:00
INDEX = {}
2023-06-05 23:57:44 +03:00
DB = dict()
2023-06-06 01:10:24 +03:00
FILENAME = ''
2023-06-05 23:57:44 +03:00
FILE = ''
CTX = {
'contexts' : '@',
'projects' : '+',
'tags' : '#'
}
def __init__(self, path: str='context.yaml', contexts=CTX):
2023-06-06 01:10:24 +03:00
self.FILENAME = os.path.basename(path)
2023-06-06 12:11:51 +03:00
self.__cd('')
2023-06-05 23:57:44 +03:00
2023-06-06 01:10:24 +03:00
def __cd(self, path: str):
2023-06-05 23:57:44 +03:00
"""change file context to subdirectory"""
2023-06-06 12:11:51 +03:00
self.FILE = os.path.join(path, self.FILENAME)
2023-06-05 23:57:44 +03:00
if os.path.isfile(self.FILE):
try:
with open(self.FILE, 'r') as data:
self.DB = yaml.safe_load(data)
except Exception:
print('File does not exist')
else:
self.DB = dict()
def __save(self):
"""save context file to current directory"""
try:
with open(self.FILE, 'w+') as data:
yaml.dump(self.DB, data)
except Exception:
print('File write error')
def __context(self, argv) -> dict:
"""create context for given filename"""
context = dict()
for ctx in self.CTX.keys():
context.update( { ctx : []})
for arg in argv:
for ctx in self.CTX.keys():
if str(arg)[0] in self.CTX.values():
if str(arg).startswith(self.CTX[ctx]):
context[ctx].append(str(arg)[1:])
else:
context.update( { 'note' : str(arg)})
return context
def add(self, file: str, *argv: str):
2023-06-06 01:10:24 +03:00
if os.path.isfile(file):
if os.path.dirname(file):
2023-06-06 12:11:51 +03:00
self.__cd(os.path.dirname(file))
2023-06-06 01:10:24 +03:00
file = os.path.basename(file)
context = self.__context(argv)
if file in self.DB:
for ctx in self.CTX.keys():
self.DB[file][ctx] = list(set( self.DB[file][ctx] + context[ctx]))
if 'note' in context.keys():
self.DB[file]['note'] = context['note']
else:
self.DB.update( { file : context } )
self.__save()
2023-06-05 23:57:44 +03:00
def remove(self, file: str, *argv: str):
2023-06-06 01:10:24 +03:00
if os.path.isfile(file):
if os.path.dirname(file):
2023-06-06 12:11:51 +03:00
self.__cd(os.path.dirname(file))
2023-06-06 01:10:24 +03:00
file = os.path.basename(file)
context = self.__context(argv)
if file in self.DB:
for ctx in self.CTX.keys():
for item in context[ctx]:
if item in self.DB[file][ctx]:
self.DB[file][ctx].remove(item)
self.__save()
2023-06-05 23:57:44 +03:00
2023-06-06 12:11:51 +03:00
def delete(self, file: str, fop:bool=False):
2023-06-05 23:57:44 +03:00
if os.path.dirname(file):
2023-06-06 12:11:51 +03:00
self.__cd(os.path.dirname(file))
2023-06-06 01:10:24 +03:00
if fop:
os.remove(file)
2023-06-05 23:57:44 +03:00
file = os.path.basename(file)
if file in self.DB:
del self.DB[file]
self.__save()
2023-06-06 12:11:51 +03:00
def move(self, source: str, dest: str, retain: bool=False, fop: bool=True):
2023-06-06 01:10:24 +03:00
if os.path.isfile(source) and os.path.isdir(os.path.dirname(dest)):
sourcefolder = os.path.dirname(source)
sourcefile = os.path.basename(source)
destfolder = os.path.dirname(dest)
destfile = os.path.basename(dest)
if not destfile:
destfile = sourcefile
sourcedata = {}
2023-06-06 12:11:51 +03:00
self.__cd(sourcefolder)
2023-06-06 01:10:24 +03:00
if self.DB[sourcefile]:
sourcedata = self.DB[sourcefile]
if not retain:
self.purge(source)
2023-06-06 12:11:51 +03:00
self.__cd(destfolder)
2023-06-06 01:10:24 +03:00
self.DB.update( { destfile : sourcedata } )
self.__save()
if fop:
if retain:
shutil.copy2(source, dest)
else:
shutil.move(source, dest, copy_function=shutil.copy2)
def purge(self, path: str=''):
if os.path.dirname(path):
2023-06-06 12:11:51 +03:00
self.__cd(os.path.dirname(path))
2023-06-06 01:10:24 +03:00
for file in self.DB.keys():
if not os.path.isfile(file):
if file in self.DB:
del self.DB[file]
self.__save()
if not self.DB:
os.remove(self.FILE)
2023-06-06 12:11:51 +03:00
def list(self, search: str, path: str, filter=None, exact=True, recursive: bool=True):
self.__indexfolder('./')
if recursive:
for (root,dirs,files) in os.walk('.'):
if dirs:
for dir in dirs:
path = os.path.join(root, dir)
self.__indexfolder(path)
for ctx in self.CTX.keys():
for context in self.INDEX[ctx]:
if filter and not filter == ctx:
continue
if exact:
if search == context:
print(self.INDEX[ctx][context])
else:
if search in context:
print(self.INDEX[ctx][context])
def __indexfolder(self, sub: str):
if os.path.dirname(sub):
self.__cd(sub)
if self.DB:
for file in self.DB.keys():
path = os.path.join(sub, file)
for ctx in self.CTX.keys():
for item in self.DB[file][ctx]:
filelist = [path]
if ctx in self.INDEX:
if item in self.INDEX[ctx]:
filelist += self.INDEX[ctx][item]
self.INDEX.update( { ctx : { item : filelist } } )
2023-06-06 01:10:24 +03:00
2023-06-05 23:57:44 +03:00
o = OrvaniTag()
2023-06-06 12:11:51 +03:00
o.list('markdown', '', '')