diff --git a/index.yaml b/index.yaml new file mode 100644 index 0000000..91bb2d9 --- /dev/null +++ b/index.yaml @@ -0,0 +1,30 @@ +contexts: + weird: + - ./dir/test.txt +files: + ./TODO.md: + contexts: [] + projects: + - projectX + - markdown + tags: + - tagx + ./dir/test.txt: + contexts: + - weird + projects: [] + tags: + - testing + ./test/file.md: + contexts: [] + projects: + - markdown + tags: + - test2 +projects: + markdown: + - ./test/file.md + - ./TODO.md +tags: + testing: + - ./dir/test.txt diff --git a/orvani.py b/orvani.py index 9127fbb..7368efc 100644 --- a/orvani.py +++ b/orvani.py @@ -16,6 +16,10 @@ class OrvaniTag: def __init__(self, path: str='context.yaml', contexts=CTX): self.FILENAME = os.path.basename(path) + self.INDEX = self.CTX.copy() + for ctx in self.INDEX: + self.INDEX[ctx] = {} + self.INDEX['files'] = {} self.__cd('') def __cd(self, path: str): @@ -124,24 +128,47 @@ 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) + def list(self, search: str, path: str='./', filter=None, exact=True, recursive: bool=True, cache: bool=False): + if path == '': + path = './' + if not os.path.dirname(path): + path += '/' + cachefile = os.path.join(path, 'index.yaml') + if cache and os.path.isfile(cachefile): + try: + with open(cachefile, 'r') as cache: + self.INDEX = yaml.safe_load(cache) + except Exception: + print('File does not exist') + else: + self.__indexfolder(path) + if recursive: + for (root,dirs,files) in os.walk(path): + if dirs: + for dir in dirs: + rpath = os.path.join(root, dir) + self.__indexfolder(rpath) + if cache: + try: + with open(cachefile, 'w+') as data: + yaml.dump(self.INDEX, data) + except Exception: + print('File write error') + result = { + 'index' : self.INDEX, + 'found' : [] + } 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]) + result['found'] = self.INDEX[ctx][context] else: if search in context: - print(self.INDEX[ctx][context]) + result['found'] = self.INDEX[ctx][context] + return result def __indexfolder(self, sub: str): if os.path.dirname(sub): @@ -149,14 +176,17 @@ class OrvaniTag: if self.DB: for file in self.DB.keys(): path = os.path.join(sub, file) + self.INDEX['files'].update( { path : self.DB[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 } } ) + if item: + 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.list('markdown', '', '') \ No newline at end of file +r = o.list('markdown', cache=True) +print(r)