diff --git a/.context.yaml b/.context.yaml new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/.context.yaml @@ -0,0 +1 @@ +{} diff --git a/.index.yaml b/.index.yaml new file mode 100644 index 0000000..59ffc71 --- /dev/null +++ b/.index.yaml @@ -0,0 +1,16 @@ +contexts: + trial: + - ./TODO.md +files: + ./TODO.md: + contexts: + - trial + note: null + projects: [] + tags: + - tag1 + - tag2 +projects: {} +tags: + tag2: + - ./TODO.md diff --git a/orvani.py b/orvani.py index 7c55155..2dec13f 100644 --- a/orvani.py +++ b/orvani.py @@ -1,6 +1,7 @@ import os import yaml import shutil +from argparse import ArgumentParser as argp, SUPPRESS, RawTextHelpFormatter as raw # Defaults @@ -82,7 +83,10 @@ class OrvaniTag: if str(arg).startswith(self.CTX[ctx]): context[ctx].append(str(arg)[1:]) else: - context.update( { 'note' : str(arg)}) + if arg != ' ': + context.update( { 'note' : str(arg)}) + else: + context.update( { 'note' : None}) return context def add(self, file: str, *argv: str): @@ -142,6 +146,8 @@ class OrvaniTag: file = os.path.basename(file) if file in self.DB: del self.DB[file] + if not self.DB: + os.remove(self.FILE) self.__save() def move(self, source: str, dest: str, retain: bool=False, fop: bool=True): @@ -176,22 +182,34 @@ class OrvaniTag: else: shutil.move(source, dest, copy_function=shutil.copy2) - def purge(self, path: str=''): + def purge(self, path: str='', recursive=True): """ Removes context data from folder if files do not exist path (str): folder path """ + if path == '': + path = './' + if not os.path.dirname(path): + path += '/' if os.path.dirname(path): self.__cd(os.path.dirname(path)) + DB = self.DB.copy() for file in self.DB.keys(): if not os.path.isfile(file): if file in self.DB: - del self.DB[file] + del DB[file] + self.DB = DB self.__save() if not self.DB: os.remove(self.FILE) + if recursive: + for (root,dirs,files) in os.walk(path): + if dirs: + for dir in dirs: + rpath = os.path.join(root, dir) + self.purge(rpath, False) def list(self, search: str, path: str='./', filter=None, exact=True, recursive: bool=True, cache: bool=False): """ @@ -268,7 +286,94 @@ class OrvaniTag: filelist += self.INDEX[ctx][item] self.INDEX.update( { ctx : { item : filelist } } ) +def main(): -o = OrvaniTag() -r = o.list('markdown', cache=True) -print(r) + command_help = """{add,a} FILE [CONTEXT ...] +{remove,rem,r} FILE [CONTEXT ...] + +{copy,cp} SOURCE DEST +{move,mv} SOURCE DEST +{del,rm} FILE + +{clear,c} FILE +{clean} PATH + +{list,ls,dir,l} QUERY +{listproj,lsprj} QUERY +{listcon,lsc} QUERY +{listtag,lst} QUERY +""" + + p = argp(description="OrvaniTagger", formatter_class=raw) + p.add_argument("--not-recursive", "-nr", action="store_true", help="Include subdirectories in search") + p.add_argument("--no-file_operation", "-nf", action="store_true", 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("command", nargs="?", default=None, metavar='Command', + help=command_help, type=str, + choices=[ + 'add', 'a', + 'remove', 'rem', 'r', + 'list', 'ls', 'dir', 'l', + 'copy', 'cp', + 'move', 'mv', + 'del', 'rm', + 'clear', 'c', + 'listproj', 'lsprj', + 'listcon', 'lsc', + 'listtag', 'lst', + 'clean', 'purge' + ]) + p.add_argument("file", nargs="?", type=str, help=SUPPRESS) + p.add_argument("context", nargs="*", type=str, help=SUPPRESS) + p.print_usage = p.print_help + args = p.parse_args() + + orvani = OrvaniTag() + + if args.command in ['add', 'a']: + if args.file and args.context: + orvani.add(args.file, *args.context) + + elif args.command in ['remove', 'rem', 'r']: + if args.file and args.context: + orvani.remove(args.file, *args.context) + + elif args.command in ['list', 'ls', 'dir', 'l', 'listproj', 'lsprj', 'lp', 'listcon', 'lsc', 'lc', 'listtag', 'lst', 'lt']: + query = args.file + if args.command in ['listproj', 'lsprj', 'lp'] or args.context_filter == 'project': + args.context_filter = 'projects' + if args.command in ['listcon', 'lsc', 'lc'] or args.context_filter == 'context': + args.context_filter = 'contexts' + if args.command in ['listtag', 'lst', 'lt'] or args.context_filter == 'tag': + args.context_filter = 'tags' + if query: + print(args.exact) + r = orvani.list(query, args.path, args.context_filter, args.exact, args.not_recursive, args.cache) + print(r) + + elif args.command in ['copy', 'cp']: + if args.file and args.context: + orvani.move(args.file, args.context[0], True, args.no_file_operation) + + elif args.command in ['move', 'mv']: + if args.file and args.context: + orvani.move(args.file, args.context[0], False, args.no_file_operation) + + elif args.command in ['del', 'rm']: + if args.file: + orvani.delete(args.file, args.no_file_operation) + + elif args.command in ['clear', 'c']: + if args.file: + orvani.delete(args.file, False) + + elif args.command in ['clean', 'purge']: + if not args.file: + args.file = '' + orvani.purge(args.file, args.not_recursive) + +if __name__ == '__main__': + main()