diff --git a/orvani/cmd.py b/orvani/cmd.py index f26cb53..54f19f7 100644 --- a/orvani/cmd.py +++ b/orvani/cmd.py @@ -1,5 +1,6 @@ import os import sys +import math from argparse import ArgumentParser as argp, SUPPRESS, RawTextHelpFormatter as raw from pathlib import Path from rich import box @@ -8,7 +9,7 @@ from rich.table import Table from orvani import OrvaniTag, CONTEXT_FILE, CACHE_FILE, CONTEXTS from plugins import frontmatter, hashdoubleslash, id3, office, pdf -def mktable(query: str, index: dict, filter=None): +def mktable(query: str, index: dict, filter=None, trunc_len: int=25): console = Console() if len(index['found']) > 0: table = Table(title=f"Results for `{query}`", box=box.HORIZONTALS, show_lines=True) @@ -23,9 +24,12 @@ def mktable(query: str, index: dict, filter=None): continue if filter == 'dirs' and not item.is_dir(): continue + long_file_pre = math.floor(trunc_len/2-1) + long_file_post = len(item.name) - math.ceil(trunc_len/2-1) + path = f'{item.parent}/' + ((item.name[:long_file_pre] + '…' + item.name[long_file_post:]) if len(item.name) > trunc_len else item.name) fc = index['index']['items'][file] elements = [] - elements.append(file) + elements.append(path) for c in ctx: if not c in fc: fc[c] = None diff --git a/orvani/orvani.py b/orvani/orvani.py index 7a6dc3b..c29eb82 100644 --- a/orvani/orvani.py +++ b/orvani/orvani.py @@ -40,6 +40,16 @@ class OrvaniTag: PLUGINS = [] REGEX = r'\s*([@+]\w+|#\w+)' + """ + ERRORS: + 0 - Success + 1 - File Not Found + 2 - Content not found + 3 - Path is wrong type + 4 - Wrong File Type + 5 - Plugin has no method + """ + def __init__(self, path: str=CONTEXT_FILE, cache: str=CACHE_FILE, contexts=CONTEXTS, plugins:list=DEFAULT_PLUGINS): """ Initialization @@ -59,6 +69,7 @@ class OrvaniTag: self.INDEX['items'] = {} self.__cd('') + def __cd(self, path: str): """ Change folder context to other directory. @@ -75,10 +86,11 @@ class OrvaniTag: path = './' if '.' + os.path.commonpath([conf_file, str(path)]) == conf_file: cfile = Path(conf_file) / self.FILENAME - conf = yaml.safe_load(cfile.read_text()) - if 'config' in conf: - self.CONFIG = conf['config'] - continue + if cfile.exists(): + conf = yaml.safe_load(cfile.read_text()) + if 'config' in conf: + self.CONFIG = conf['config'] + continue else: self.CONFIG = dict() @@ -112,8 +124,12 @@ class OrvaniTag: tmp = yaml.safe_load(file.read_text()) if 'config' in tmp: yf['config'] = tmp['config'] - with file.open('w+') as data: - yaml.safe_dump(yf, data) + try: + with file.open('w+') as data: + yaml.safe_dump(yf, data) + return self.FILE + except: + return 0 def __context(self, argv) -> dict: @@ -165,7 +181,11 @@ class OrvaniTag: self.DB[target.name][context_key] = context[context_key] else: self.DB.update( { target.name : context } ) - self.__save() + saved = self.__save() + if saved and context: + return [0, saved, context] + else: + return [1] def remove(self, file: str, *argv: str): """ @@ -186,7 +206,15 @@ class OrvaniTag: for item in context[context_key]: if item in self.DB[file][context_key]: self.DB[file][context_key].remove(item) - self.__save() + saved = self.__save() + if saved and self.DB[file]: + return [0, saved, self.DB[file]] + else: + return [2, item] + else: + return [2, file] + else: + return [1, file] def delete(self, file: str, fop:bool=False): """ @@ -199,13 +227,18 @@ class OrvaniTag: target = Path(file) if target.exists(): self.__cd(target.parent) + else: + return [1, file] if target.name in self.DB: del self.DB[target.name] - self.__save() + else: + return [2, target.name] + saved = self.__save() if not self.DB: Path(self.FILE).unlink(missing_ok=True) if fop: target.unlink(missing_ok=True) + return [0, file, self.FILE if not self.DB else None, fop] def move(self, source: str, dest: str, retain: bool=False, fop: bool=True): """ @@ -238,6 +271,10 @@ class OrvaniTag: shutil.copy2(source, dest) else: shutil.move(source, dest, copy_function=shutil.copy2) + return [0, dest, sourcedata] + else: + return [1, os.path.isfile(source), os.path.isdir(os.path.dirname(dest))] + def clean(self, path: str='.', recursive=True): """ @@ -249,24 +286,27 @@ class OrvaniTag: if path == '': path = '.' target = Path(path) - if target.exists(): + if target.is_dir(): self.__cd(path) - DB = self.DB.copy() - for file in self.DB.keys(): - f = target / file - if not f.is_file(): - if file in self.DB: - del DB[file] - self.DB = DB - self.__save() - if not self.DB: - Path(self.FILE).unlink(missing_ok=True) - if recursive: - for (root,dirs,files) in os.walk(path): - if dirs: - for dir in dirs: - rpath = os.path.join(root, dir) - self.clean(rpath, False) + DB = self.DB.copy() + for file in self.DB.keys(): + f = target / file + if not f.is_file(): + if file in self.DB: + del DB[file] + self.DB = DB + self.__save() + if not self.DB: + Path(self.FILE).unlink(missing_ok=True) + if recursive: + for (root,dirs,files) in os.walk(path): + if dirs: + for dir in dirs: + rpath = os.path.join(root, dir) + self.clean(rpath, False) + return [0, str(target)] + else: + return [3, str(target)] def list(self, path: str='./', exact=True, either=False, recursive: bool=True, cache: bool=False, delete_cache: bool=False, *search: str): """ @@ -518,6 +558,11 @@ class OrvaniTag: o.context = items[found] if hasattr(plugin, 'writer'): plugin.writer(o) + return [0, filename, o.context] + else: + return [5, plugin_name] + else: + return [4, suffixes, plugin_name in self.CONFIG] def export(self, filename:str): if filename == '' or filename == None: @@ -531,6 +576,9 @@ class OrvaniTag: string = ' '.join([x for x in self.__collect_contexts(items[path]) if x != None]) content += f'{path} --- {string}\n' file.write_text(content) + return [0, filename] + else: + return [2] def load(self, filename:str): if filename == '' or filename == None: @@ -543,6 +591,7 @@ class OrvaniTag: for import_line in imported: line_elements = import_line.split('---') if len(line_elements) > 1: + context = {} path = line_elements[0].strip() new_file = Path(path) if new_file.exists(): @@ -564,4 +613,11 @@ class OrvaniTag: del context[new_context] ctx_str = self.__collect_contexts(context) if ctx_str: - self.add(path, *ctx_str) \ No newline at end of file + self.add(path, *ctx_str) + return [0, filename, len(context.keys())] + else: + return [4, filename] + else: + return [2, filename] + else: + return [1, filename] \ No newline at end of file