modularized list method
This commit is contained in:
parent
ef12ef0091
commit
42c9cbf002
7 changed files with 90 additions and 51 deletions
98
orvani.py
98
orvani.py
|
|
@ -230,6 +230,35 @@ class OrvaniTag:
|
|||
path = './'
|
||||
if not os.path.dirname(path):
|
||||
path += '/'
|
||||
self.__makeindex(path, recursive, cache)
|
||||
result = self.__search(search, exact)
|
||||
return result
|
||||
|
||||
|
||||
def __intersect(self, result: dict):
|
||||
"""
|
||||
Returns an intersection of two search queries
|
||||
|
||||
result (dict): dict with search result lists to be intersected
|
||||
|
||||
"""
|
||||
mresult = {key: value for key, value in result.items() if value}
|
||||
if len(mresult) < 2:
|
||||
return []
|
||||
common_elements = set(mresult[next(iter(mresult))])
|
||||
for key in mresult.keys():
|
||||
common_elements = common_elements.intersection(mresult[key])
|
||||
return list(common_elements)
|
||||
|
||||
|
||||
def __makeindex(self, path:str, recursive:bool=True, cache: bool=False):
|
||||
"""
|
||||
Creates index for searching, optionally also a cache file
|
||||
|
||||
path (dict): root directory
|
||||
recursive (bool): traverse subdirectories
|
||||
|
||||
"""
|
||||
cachefile = os.path.join(path, self.CACHEFILE)
|
||||
if cache and os.path.isfile(cachefile):
|
||||
try:
|
||||
|
|
@ -251,6 +280,36 @@ class OrvaniTag:
|
|||
yaml.dump(self.INDEX, data)
|
||||
except Exception:
|
||||
print('File write error')
|
||||
|
||||
def __indexfolder(self, sub: str):
|
||||
"""
|
||||
Indexes a folder context
|
||||
|
||||
sub (str): path of folder
|
||||
|
||||
"""
|
||||
if os.path.dirname(sub):
|
||||
self.__cd(sub)
|
||||
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[ctx][item] = filelist
|
||||
|
||||
def __search(self, search, filter: str=None, exact:bool=False):
|
||||
"""
|
||||
Searches the index and adds search result to it
|
||||
|
||||
search (list): search query list
|
||||
exact (bool): exact match or partial match
|
||||
|
||||
"""
|
||||
result = {
|
||||
'index' : self.INDEX,
|
||||
'found' : []
|
||||
|
|
@ -289,46 +348,9 @@ class OrvaniTag:
|
|||
for sublist in ret.values():
|
||||
min += sublist
|
||||
result['found'] = min
|
||||
return result
|
||||
else:
|
||||
result['found'] = sorted(self.INDEX['files'].keys(), key=lambda x: x.count('/'))
|
||||
return result
|
||||
|
||||
def __intersect(self, result: dict):
|
||||
"""
|
||||
Returns an intersection of two search queries
|
||||
|
||||
result (dict): dict with search result lists to be intersected
|
||||
|
||||
"""
|
||||
mresult = {key: value for key, value in result.items() if value}
|
||||
if len(mresult) < 2:
|
||||
return []
|
||||
common_elements = set(mresult[next(iter(mresult))])
|
||||
for key in mresult.keys():
|
||||
common_elements = common_elements.intersection(mresult[key])
|
||||
return list(common_elements)
|
||||
|
||||
def __indexfolder(self, sub: str):
|
||||
"""
|
||||
Indexes a folder context
|
||||
|
||||
sub (str): path of folder
|
||||
|
||||
"""
|
||||
if os.path.dirname(sub):
|
||||
self.__cd(sub)
|
||||
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[ctx][item] = filelist
|
||||
return result
|
||||
|
||||
def mktable(query: str, index: dict):
|
||||
console = Console()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue