recursive search

This commit is contained in:
randogoth 2023-06-06 12:11:51 +03:00
parent bd9103d362
commit 8e68c82057
4 changed files with 46 additions and 12 deletions

View file

@ -4,6 +4,7 @@ import shutil
class OrvaniTag:
INDEX = {}
DB = dict()
FILENAME = ''
FILE = ''
@ -15,11 +16,11 @@ class OrvaniTag:
def __init__(self, path: str='context.yaml', contexts=CTX):
self.FILENAME = os.path.basename(path)
self.__cd(path)
self.__cd('')
def __cd(self, path: str):
"""change file context to subdirectory"""
self.FILE = os.path.join(os.path.dirname(path), self.FILENAME)
self.FILE = os.path.join(path, self.FILENAME)
if os.path.isfile(self.FILE):
try:
with open(self.FILE, 'r') as data:
@ -54,7 +55,7 @@ class OrvaniTag:
def add(self, file: str, *argv: str):
if os.path.isfile(file):
if os.path.dirname(file):
self.__cd(file)
self.__cd(os.path.dirname(file))
file = os.path.basename(file)
context = self.__context(argv)
if file in self.DB:
@ -69,7 +70,7 @@ class OrvaniTag:
def remove(self, file: str, *argv: str):
if os.path.isfile(file):
if os.path.dirname(file):
self.__cd(file)
self.__cd(os.path.dirname(file))
file = os.path.basename(file)
context = self.__context(argv)
if file in self.DB:
@ -79,9 +80,9 @@ class OrvaniTag:
self.DB[file][ctx].remove(item)
self.__save()
def delete(self, file: str, fop=False):
def delete(self, file: str, fop:bool=False):
if os.path.dirname(file):
self.__cd(file)
self.__cd(os.path.dirname(file))
if fop:
os.remove(file)
file = os.path.basename(file)
@ -89,7 +90,7 @@ class OrvaniTag:
del self.DB[file]
self.__save()
def move(self, source: str, dest: str, retain=False, fop=True):
def move(self, source: str, dest: str, retain: bool=False, fop: bool=True):
if os.path.isfile(source) and os.path.isdir(os.path.dirname(dest)):
sourcefolder = os.path.dirname(source)
sourcefile = os.path.basename(source)
@ -98,12 +99,12 @@ class OrvaniTag:
if not destfile:
destfile = sourcefile
sourcedata = {}
self.__cd(source)
self.__cd(sourcefolder)
if self.DB[sourcefile]:
sourcedata = self.DB[sourcefile]
if not retain:
self.purge(source)
self.__cd(dest)
self.__cd(destfolder)
self.DB.update( { destfile : sourcedata } )
self.__save()
if fop:
@ -114,7 +115,7 @@ class OrvaniTag:
def purge(self, path: str=''):
if os.path.dirname(path):
self.__cd(path)
self.__cd(os.path.dirname(path))
for file in self.DB.keys():
if not os.path.isfile(file):
if file in self.DB:
@ -123,7 +124,39 @@ class OrvaniTag:
if not self.DB:
os.remove(self.FILE)
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 } } )
o = OrvaniTag()
#o.add('TODO.md', '#tagx', '+projectX')
o.purge()
o.list('markdown', '', '')