fixed universal search
This commit is contained in:
parent
40026a3c0d
commit
8489f6cd6d
3 changed files with 92 additions and 70 deletions
Binary file not shown.
|
|
@ -4,29 +4,36 @@ from argparse import ArgumentParser as argp, SUPPRESS, RawTextHelpFormatter as r
|
|||
from pathlib import Path
|
||||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
from orvani import OrvaniTag, CONTEXT_FILE, CACHE_FILE
|
||||
from orvani import OrvaniTag, CONTEXT_FILE, CACHE_FILE, CONTEXTS
|
||||
|
||||
def mktable(query: str, index: dict):
|
||||
console = Console()
|
||||
if len(index['found']) > 0:
|
||||
table = Table(title=f"Results for `{query}`")
|
||||
ctx = list(CONTEXTS.keys())
|
||||
table.add_column("Path", justify="right", style="white", no_wrap=True)
|
||||
table.add_column("Note", style="#cccccc")
|
||||
table.add_column("Projects", justify="center", style="cyan")
|
||||
table.add_column("Contexts", justify="center", style="magenta")
|
||||
table.add_column("Tags", justify="center", style="green")
|
||||
note = tags = projects = contexts = tags = ''
|
||||
styles = ["cyan", "yellow", "magenta", "green"]
|
||||
for i, c in enumerate(ctx):
|
||||
table.add_column(ctx[i].title(), style=styles[i])
|
||||
for file in index['found']:
|
||||
path = file
|
||||
fc = index['index']['files'][file]
|
||||
if 'note' in fc:
|
||||
note = fc['note']
|
||||
else:
|
||||
note = ''
|
||||
tags = ' '.join(['#' + tag for tag in fc['tags']])
|
||||
projects = ' '.join(['+' + p for p in fc['projects']])
|
||||
contexts = ' '.join(['@' + c for c in fc['contexts']])
|
||||
table.add_row(path, note, projects, contexts, tags)
|
||||
elements = []
|
||||
elements.append(file)
|
||||
for c in ctx:
|
||||
if not c in fc:
|
||||
fc[c] = None
|
||||
if fc[c]:
|
||||
if CONTEXTS[c]:
|
||||
prefix = CONTEXTS[c]
|
||||
else:
|
||||
prefix = ''
|
||||
if not type(fc[c]) == str:
|
||||
elements.append(' '.join([prefix + tag for tag in fc[c]]))
|
||||
else:
|
||||
elements.append(fc[c])
|
||||
else:
|
||||
elements.append(None)
|
||||
table.add_row(*elements)
|
||||
console.print(table)
|
||||
else:
|
||||
console.print(f"No results found for `{query}`", style="red")
|
||||
|
|
|
|||
125
orvani/orvani.py
125
orvani/orvani.py
|
|
@ -7,6 +7,12 @@ from pathlib import Path
|
|||
|
||||
CONTEXT_FILE = '.context.yaml'
|
||||
CACHE_FILE = '.index.yaml'
|
||||
CONTEXTS = {
|
||||
'projects' : '+',
|
||||
'contexts' : '@',
|
||||
'tags' : '#',
|
||||
'note' : None
|
||||
}
|
||||
|
||||
class OrvaniTag:
|
||||
|
||||
|
|
@ -18,14 +24,9 @@ class OrvaniTag:
|
|||
CONFIG = dict()
|
||||
FILENAME = '' # folder context data filename ('.context.yaml')
|
||||
FILE = '' # holds the folder context file path
|
||||
CTX = { # default contexts
|
||||
'contexts' : '@',
|
||||
'projects' : '+',
|
||||
'tags' : '#',
|
||||
'note' : None
|
||||
}
|
||||
CTX = {}
|
||||
|
||||
def __init__(self, path: str=CONTEXT_FILE, cache: str=CACHE_FILE, contexts=CTX):
|
||||
def __init__(self, path: str=CONTEXT_FILE, cache: str=CACHE_FILE, contexts=CONTEXTS):
|
||||
"""
|
||||
Initialization
|
||||
|
||||
|
|
@ -36,9 +37,10 @@ class OrvaniTag:
|
|||
"""
|
||||
self.FILENAME = os.path.basename(path)
|
||||
self.CACHEFILE = cache
|
||||
self.CTX = contexts
|
||||
self.INDEX = self.CTX.copy()
|
||||
for ctx in self.INDEX:
|
||||
self.INDEX[ctx] = {}
|
||||
for context_key in self.INDEX:
|
||||
self.INDEX[context_key] = {}
|
||||
self.INDEX['files'] = {}
|
||||
self.__cd('')
|
||||
|
||||
|
|
@ -80,23 +82,23 @@ class OrvaniTag:
|
|||
|
||||
"""
|
||||
context = dict()
|
||||
for ctx in self.CTX.keys():
|
||||
if self.CTX[ctx] != None:
|
||||
context.update( { ctx : []})
|
||||
for context_key in self.CTX.keys():
|
||||
if self.CTX[context_key] != None:
|
||||
context.update( { context_key : []})
|
||||
for arg in argv:
|
||||
for ctx in self.CTX.keys():
|
||||
for context_key in self.CTX.keys():
|
||||
if str(arg)[0] in self.CTX.values():
|
||||
# Schema with prefix
|
||||
if not self.CTX[ctx] == None:
|
||||
if str(arg).startswith(self.CTX[ctx]):
|
||||
context[ctx].append(str(arg)[1:])
|
||||
if not self.CTX[context_key] == None:
|
||||
if str(arg).startswith(self.CTX[context_key]):
|
||||
context[context_key].append(str(arg)[1:])
|
||||
else:
|
||||
# Otherwise it's a note
|
||||
if self.CTX[ctx] == None:
|
||||
if self.CTX[context_key] == None:
|
||||
if arg != ' ':
|
||||
context.update( { ctx : str(arg)})
|
||||
context.update( { context_key : str(arg)})
|
||||
else:
|
||||
context.update( { ctx : None})
|
||||
context.update( { context_key : None})
|
||||
return context
|
||||
|
||||
def add(self, file: str, *argv: str):
|
||||
|
|
@ -112,12 +114,12 @@ class OrvaniTag:
|
|||
self.__cd(target.parent.name)
|
||||
context = self.__context(argv)
|
||||
if target.name in self.DB:
|
||||
for ctx in self.CTX.keys():
|
||||
if self.CTX[ctx] != None:
|
||||
self.DB[target.name][ctx] = list(set( self.DB[target.name][ctx] + context[ctx]))
|
||||
for context_key in self.CTX.keys():
|
||||
if self.CTX[context_key] != None:
|
||||
self.DB[target.name][context_key] = list(set( self.DB[target.name][context_key] + context[context_key]))
|
||||
else:
|
||||
if ctx in context:
|
||||
self.DB[target.name][ctx] = context[ctx]
|
||||
if context_key in context:
|
||||
self.DB[target.name][context_key] = context[context_key]
|
||||
else:
|
||||
self.DB.update( { target.name : context } )
|
||||
self.__save()
|
||||
|
|
@ -135,12 +137,12 @@ class OrvaniTag:
|
|||
self.__cd(target.parent.name)
|
||||
context = self.__context(argv)
|
||||
if file in self.DB:
|
||||
for ctx in self.CTX.keys():
|
||||
for context_key in self.CTX.keys():
|
||||
# Only works for context list items
|
||||
if self.CTX[ctx] != None:
|
||||
for item in context[ctx]:
|
||||
if item in self.DB[file][ctx]:
|
||||
self.DB[file][ctx].remove(item)
|
||||
if self.CTX[context_key] != None:
|
||||
for item in context[context_key]:
|
||||
if item in self.DB[file][context_key]:
|
||||
self.DB[file][context_key].remove(item)
|
||||
self.__save()
|
||||
|
||||
def delete(self, file: str, fop:bool=False):
|
||||
|
|
@ -294,14 +296,14 @@ class OrvaniTag:
|
|||
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():
|
||||
if ctx in self.DB[file]:
|
||||
for item in self.DB[file][ctx]:
|
||||
for context_key in self.CTX.keys():
|
||||
if context_key in self.DB[file]:
|
||||
for item in self.DB[file][context_key]:
|
||||
filelist = [path]
|
||||
if ctx in self.INDEX:
|
||||
if item in self.INDEX[ctx]:
|
||||
filelist += self.INDEX[ctx][item]
|
||||
self.INDEX[ctx][item] = filelist
|
||||
if context_key in self.INDEX:
|
||||
if item in self.INDEX[context_key]:
|
||||
filelist += self.INDEX[context_key][item]
|
||||
self.INDEX[context_key][item] = filelist
|
||||
|
||||
def __search(self, search, filter: str=None, exact:bool=False):
|
||||
"""
|
||||
|
|
@ -317,30 +319,43 @@ class OrvaniTag:
|
|||
}
|
||||
if search[0]:
|
||||
query = self.__context(search)
|
||||
ret = self.__context([' '])
|
||||
ret['note'] = []
|
||||
for ctx in self.CTX.keys():
|
||||
for context in self.INDEX[ctx]:
|
||||
if filter and not filter == ctx:
|
||||
return_dict = self.__context([' '])
|
||||
universal = {i for i in self.CTX if self.CTX[i]==None}.pop()
|
||||
for context_key in self.CTX.keys():
|
||||
return_dict[context_key] = []
|
||||
for context in self.INDEX[context_key]:
|
||||
if filter and not filter == context_key:
|
||||
continue
|
||||
if ctx in query:
|
||||
for q in query[ctx]:
|
||||
if exact:
|
||||
if q == context:
|
||||
if self.INDEX[ctx][context] not in ret[ctx]:
|
||||
ret[ctx] += self.INDEX[ctx][context]
|
||||
else:
|
||||
if q in context:
|
||||
if self.INDEX[ctx][context] not in ret[ctx]:
|
||||
ret[ctx] += self.INDEX[ctx][context]
|
||||
ret[ctx] = list(set(ret[ctx]))
|
||||
if context_key in query:
|
||||
# Is it a list of context items?
|
||||
if type(query[context_key]) == list:
|
||||
for q in query[context_key]:
|
||||
if exact:
|
||||
if q == context:
|
||||
if self.INDEX[context_key][context] not in return_dict[context_key]:
|
||||
return_dict[context_key] += self.INDEX[context_key][context]
|
||||
else:
|
||||
if q in context:
|
||||
if self.INDEX[context_key][context] not in return_dict[context_key]:
|
||||
return_dict[context_key] += self.INDEX[context_key][context]
|
||||
if universal in query:
|
||||
if exact:
|
||||
if query[universal] == context:
|
||||
if self.INDEX[context_key][context] not in return_dict[context_key]:
|
||||
return_dict[context_key] += self.INDEX[context_key][context]
|
||||
else:
|
||||
if query[universal] in context:
|
||||
if self.INDEX[context_key][context] not in return_dict[context_key]:
|
||||
return_dict[context_key] += self.INDEX[context_key][context]
|
||||
if context_key in return_dict:
|
||||
return_dict[context_key] = list(set(return_dict[context_key]))
|
||||
if len(search) > 1:
|
||||
result['found'] = sorted(list(set(self.__intersect(ret))), key=lambda x: x.count('/'))
|
||||
result['found'] = sorted(list(set(self.__intersect(return_dict))), key=lambda x: x.count('/'))
|
||||
else:
|
||||
min = []
|
||||
for sublist in ret.values():
|
||||
for sublist in return_dict.values():
|
||||
min += sublist
|
||||
result['found'] = min
|
||||
result['found'] = list(set(min))
|
||||
else:
|
||||
result['found'] = sorted(self.INDEX['files'].keys(), key=lambda x: x.count('/'))
|
||||
return result
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue