import/export

This commit is contained in:
randogoth 2023-06-10 18:34:35 +03:00
parent d1b829f647
commit 96ad299cf3
7 changed files with 80 additions and 48 deletions

View file

@ -12,7 +12,7 @@ def mktable(query: str, index: dict, filter=None):
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("Path", style="white", no_wrap=True)
styles = ["cyan", "yellow", "magenta", "green"]
for i, c in enumerate(ctx):
table.add_column(ctx[i].title(), style=styles[i])

View file

@ -85,13 +85,17 @@ class OrvaniTag:
self.FILE = Path(str(path)) / self.FILENAME
if self.FILE.exists():
y = yaml.safe_load(self.FILE.read_text())
self.DB = y['items']
if 'config' in y:
self.CONFIG = y['config']
self.CONF_FILES.append(str(path))
self.CONF_FILES = [x for x in self.CONF_FILES if x != '']
self.CONF_FILES = sorted(list(set(self.CONF_FILES)), key=lambda x: x.count('/'), reverse=True)
if 'items' in y:
self.DB = y['items']
if 'config' in y:
self.CONFIG = y['config']
self.CONF_FILES.append(str(path))
self.CONF_FILES = [x for x in self.CONF_FILES if x != '']
self.CONF_FILES = sorted(list(set(self.CONF_FILES)), key=lambda x: x.count('/'), reverse=True)
else:
__config(path)
else:
self.DB = dict()
__config(path)
else:
self.DB = dict()
@ -334,6 +338,7 @@ class OrvaniTag:
self.__updateindex(path, self.DB[file], context_key)
def __updateindex(self, path: str, context: dict, context_key: str):
# Update files index
for key, value in context.items():
if path in self.INDEX['items']:
@ -345,6 +350,8 @@ class OrvaniTag:
else:
self.INDEX['items'][path] = { key : value }
self.INDEX['items'] = {key: value for key, value in sorted(self.INDEX['items'].items())}
# Update context index
if type(context[context_key]) == list:
for item in context[context_key]:
@ -462,7 +469,7 @@ class OrvaniTag:
for context_key in context.keys():
self.__updateindex(filename, context, context_key)
def __collect_contexts(self, context: dict):
def __collect_contexts(self, context: dict, delineate: bool = False):
elements = []
context_keys = list(CONTEXTS.keys())
for context_key in context_keys:
@ -476,7 +483,10 @@ class OrvaniTag:
if not type(context[context_key]) == str:
elements.append(' '.join([prefix + tag for tag in context[context_key]]))
else:
elements.append(context[context_key])
if delineate:
elements.append(f"'{context[context_key]}'")
else:
elements.append(context[context_key])
return elements
def write(self, filename: str, method: str):
@ -516,16 +526,42 @@ class OrvaniTag:
index = self.list(file.parent, False, False, True, False, False, '')
items = index['index']['items']
content = ""
for path in items:
string = ' '.join([x for x in self.__collect_contexts(items[path]) if x != None])
content += f'{path} --- {string}\n'
file.write_text(content)
if len(items):
for path in items:
string = ' '.join([x for x in self.__collect_contexts(items[path]) if x != None])
content += f'{path} --- {string}\n'
file.write_text(content)
def load(self, filename:str):
if filename == '' or filename == None:
filename = 'context.txt'
file = Path(filename)
imported = file.read_text()
known = self.list(file.parent, False, False, True, False, False, '')['index']['items']
print(known)
if file.exists():
imported = file.read_text().split('\n')
if len(imported):
known = self.list(file.parent, False, False, True, False, False, '')['index']['items']
for import_line in imported:
line_elements = import_line.split('---')
if len(line_elements) > 1:
path = line_elements[0].strip()
new_file = Path(path)
if new_file.exists():
string = line_elements[1].strip()
if string:
new_file_context = self.__context(self.get_argv(string))
if path in known.keys():
known_file_context = known[path]
context = new_file_context.copy()
for new_context in new_file_context:
if type(new_file_context[new_context]) == list:
for context_item in new_file_context[new_context]:
if new_context in known_file_context.keys():
if context_item in known_file_context[new_context]:
context[new_context].remove(context_item)
else:
if new_context in known_file_context.keys():
if new_file_context[new_context] == known_file_context[new_context]:
del context[new_context]
ctx_str = self.__collect_contexts(context)
if ctx_str:
self.add(path, *ctx_str)

View file

@ -33,6 +33,7 @@ def reader(o):
for key in id3map.keys():
if key in context.keys():
context[key] += id3map[key]
context[universal] = '; '.join(context[universal])
return context
def writer(o):