From 8e68c820579c34ec4949d7624f0d4b821b29b094 Mon Sep 17 00:00:00 2001 From: randogoth Date: Tue, 6 Jun 2023 12:11:51 +0300 Subject: [PATCH] recursive search --- context.yaml | 1 + dir/test.txt | 0 orvani.py | 57 +++++++++++++++++++++++++++++++++++++++++----------- test/file.md | 0 4 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 dir/test.txt create mode 100644 test/file.md diff --git a/context.yaml b/context.yaml index 87cfe45..979f741 100644 --- a/context.yaml +++ b/context.yaml @@ -2,5 +2,6 @@ TODO.md: contexts: [] projects: - projectX + - markdown tags: - tagx diff --git a/dir/test.txt b/dir/test.txt new file mode 100644 index 0000000..e69de29 diff --git a/orvani.py b/orvani.py index 09e349b..9127fbb 100644 --- a/orvani.py +++ b/orvani.py @@ -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() \ No newline at end of file +o.list('markdown', '', '') \ No newline at end of file diff --git a/test/file.md b/test/file.md new file mode 100644 index 0000000..e69de29