caching
This commit is contained in:
parent
8e68c82057
commit
07c3e974eb
2 changed files with 76 additions and 16 deletions
62
orvani.py
62
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', '', '')
|
||||
r = o.list('markdown', cache=True)
|
||||
print(r)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue