OR search & delete cache
This commit is contained in:
parent
07f9da8655
commit
2c239bc07e
3 changed files with 32 additions and 28 deletions
4
TODO.md
4
TODO.md
|
|
@ -2,6 +2,4 @@
|
|||
tags:
|
||||
- frrr
|
||||
---
|
||||
+ plugin for Comment ID3 tag
|
||||
+ plugin for xattr
|
||||
+ global config
|
||||
+ plugin for xattr
|
||||
|
|
@ -63,8 +63,9 @@ def main():
|
|||
p.add_argument("--no-file_operation", "-nf", action="store_false", help="Copy/Move/Delete file, not just context")
|
||||
p.add_argument("--path", "-p", action="store", type=str, help="List/search custom path", default='')
|
||||
p.add_argument("--exact", "-e", action="store_true", help="Match exact search term")
|
||||
p.add_argument("--cache", action="store_true", help="Cache index search (useful with deep directory trees)")
|
||||
p.add_argument("--context_filter", "-c", action="store", type=str, help="Filter context", choices=['context', 'project', 'tag'], default=None)
|
||||
p.add_argument("--either", "-or", action="store_true", help="Combine search terms by OR instead of AND")
|
||||
p.add_argument("--cache", "-c", action="store_true", help="Cache index search (useful with deep directory trees)")
|
||||
p.add_argument("--delete-cache", "-dc", action="store_true", help="Delete cache file")
|
||||
p.add_argument("command", nargs="?", default=None, metavar='Command',
|
||||
help=command_help, type=str,
|
||||
choices=[
|
||||
|
|
@ -102,7 +103,7 @@ def main():
|
|||
elif args.command in ['list', 'ls', 'dir', 'l']:
|
||||
query = [args.file] + args.context
|
||||
if query:
|
||||
r = orvani.list(args.path, args.context_filter, args.exact, args.not_recursive, args.cache, *query)
|
||||
r = orvani.list(args.path, args.exact, args.either, args.not_recursive, args.cache, args.delete_cache, *query)
|
||||
mktable(' '.join(sys.argv[1:]), r)
|
||||
|
||||
elif args.command in ['copy', 'cp']:
|
||||
|
|
|
|||
|
|
@ -250,7 +250,7 @@ class OrvaniTag:
|
|||
rpath = os.path.join(root, dir)
|
||||
self.clean(rpath, False)
|
||||
|
||||
def list(self, path: str='./', filter=None, exact=True, recursive: bool=True, cache: bool=False, *search: str):
|
||||
def list(self, path: str='./', exact=True, either=False, recursive: bool=True, cache: bool=False, delete_cache: bool=False, *search: str):
|
||||
"""
|
||||
Returns an index object with query results and optionally saves a cached index
|
||||
|
||||
|
|
@ -264,28 +264,12 @@ class OrvaniTag:
|
|||
"""
|
||||
if path == '':
|
||||
path = './'
|
||||
self.__makeindex(path, recursive, cache)
|
||||
result = self.__search(search, filter, exact)
|
||||
self.__makeindex(path, recursive, cache, delete_cache)
|
||||
result = self.__search(search, exact, either)
|
||||
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):
|
||||
def __makeindex(self, path:str, recursive:bool=True, cache: bool=False, delete_cache: bool=False):
|
||||
"""
|
||||
Creates index for searching, optionally also a cache file
|
||||
|
||||
|
|
@ -294,6 +278,8 @@ class OrvaniTag:
|
|||
|
||||
"""
|
||||
cachefile = Path(path) / self.CACHEFILE
|
||||
if delete_cache:
|
||||
cachefile.unlink(missing_ok=True)
|
||||
if cache and cachefile.is_file():
|
||||
self.INDEX = yaml.safe_load(cachefile.read_text())
|
||||
else:
|
||||
|
|
@ -359,7 +345,7 @@ class OrvaniTag:
|
|||
filelist += self.INDEX[context_key][item]
|
||||
self.INDEX[context_key][item] = list(set(filelist))
|
||||
|
||||
def __search(self, search, filter: str=None, exact:bool=False):
|
||||
def __search(self, search, exact:bool=False, either:bool=False):
|
||||
"""
|
||||
Searches the index and adds search result to it
|
||||
|
||||
|
|
@ -395,7 +381,7 @@ class OrvaniTag:
|
|||
if needle in hay:
|
||||
if self.INDEX[param][hay] not in return_dict[param]:
|
||||
return_dict[param] += self.INDEX[param][hay]
|
||||
if len(search) > 1:
|
||||
if len(search) > 1 and not either:
|
||||
result['found'] = list(set(self.__intersect(return_dict)))
|
||||
else:
|
||||
min = []
|
||||
|
|
@ -406,15 +392,34 @@ class OrvaniTag:
|
|||
result['found'] = self.INDEX['files'].keys()
|
||||
result['found'] = sorted(result['found'], 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 metareader(self, function):
|
||||
self.META_IN.append(function)
|
||||
return function
|
||||
|
||||
|
||||
def metawriter(self, function):
|
||||
self.META_OUT.append(function)
|
||||
return function
|
||||
|
||||
|
||||
def metadata(self, filename: str):
|
||||
obj = {
|
||||
'path' : filename,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue