diff --git a/.context.yaml b/.context.yaml index 05b0551..9690647 100644 --- a/.context.yaml +++ b/.context.yaml @@ -1,4 +1,9 @@ items: + README.md: + contexts: [] + projects: [] + tags: + - writeme TODO.md: contexts: - python diff --git a/README.md b/README.md index 17907f2..c1053e7 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ --- -projects: -- test -- test2 contexts: - frrreee +projects: [] tags: - fool +- writeme --- + # Orvani Tagger (From עוֹרְבָנִי, the Hebrew name for th Eurasian Jay) diff --git a/orvani/cmd.py b/orvani/cmd.py index f9652be..a16649d 100644 --- a/orvani/cmd.py +++ b/orvani/cmd.py @@ -84,6 +84,7 @@ def main(): 'move', 'mv', 'del', 'rm', 'clean', 'purge', + 'export' ]) p.add_argument("file", nargs="?", type=str, help=SUPPRESS) p.add_argument("context", nargs="*", type=str, help=SUPPRESS) @@ -146,5 +147,9 @@ def main(): args.file = '' orvani.clean(args.file, args.not_recursive) + elif args.command in ['export']: + if args.file and args.context: + orvani.export(args.context[0], args.file) + if __name__ == '__main__': main() diff --git a/orvani/orvani.py b/orvani/orvani.py index cca4368..673d18e 100644 --- a/orvani/orvani.py +++ b/orvani/orvani.py @@ -69,7 +69,9 @@ class OrvaniTag: def __config(path): if self.CONF_FILES: for conf_file in self.CONF_FILES: - if '.' + os.path.commonpath([conf_file[1:], path[1:]]) == conf_file: + if path == '.': + 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: @@ -294,18 +296,19 @@ class OrvaniTag: self.INDEX = yaml.safe_load(cachefile.read_text()) else: self.__indexfolder(path) - if recursive: - for (root,dirs,files) in os.walk(path, topdown=True): - if self.ENABLE_PLUGINS: - if files: - for file in files: - fpath = os.path.join(root, file) - self.__cd(root) - self.metadata(fpath) - if dirs: - for dir in dirs: - rpath = os.path.join(root, dir) - self.__indexfolder(rpath) + for (root,dirs,files) in os.walk(path, topdown=True): + if self.ENABLE_PLUGINS: + if files: + for file in files: + fpath = os.path.join(root, file) + self.__cd(root) + self.__metadata(fpath) + if dirs: + for dir in dirs: + rpath = os.path.join(root, dir) + self.__indexfolder(rpath) + if not recursive: + break if cache: with open(cachefile, 'w+') as data: @@ -424,7 +427,7 @@ class OrvaniTag: self.PLUGINS.append(function) return function - def metadata(self, filename: str): + def __metadata(self, filename: str): obj = { 'file' : Path(), 'config' : self.CONFIG, @@ -443,7 +446,37 @@ class OrvaniTag: suffixes = self.CONFIG[plugin_name]['types'] o.file = Path(filename) if o.file.suffix in suffixes: - context = plugin.reader(o) - if context: - for context_key in context.keys(): - self.__updateindex(filename, context, context_key) \ No newline at end of file + if hasattr(plugin, 'reader'): + context = plugin.reader(o) + if context: + for context_key in context.keys(): + self.__updateindex(filename, context, context_key) + + def export(self, filename: str, method: str): + obj = { + 'file' : Path(), + 'config' : self.CONFIG, + 'schema' : self.CTX, + 'context' : {}, + } + o = SimpleNamespace(**obj) + for plugin_name in self.PLUGINS: + if 'plugins' in self.CONFIG: + if plugin_name not in self.CONFIG['plugins']: + continue + if plugin_name == method: + plugin = import_module('plugins.' + plugin_name) + suffixes = plugin.TYPES + if plugin_name in self.CONFIG: + if 'types' in self.CONFIG[plugin_name]: + suffixes = self.CONFIG[plugin_name]['types'] + o.file = Path(filename) + if o.file.suffix in suffixes: + self.__makeindex(o.file.parent, recursive=False) + items = self.INDEX['items'] + for found in items.keys(): + if './' + o.file.name == found: + o.context = items[found] + if hasattr(plugin, 'writer'): + plugin.writer(o) + diff --git a/orvani/plugins/frontmatter.py b/orvani/plugins/frontmatter.py index a74ecda..ea8e13b 100644 --- a/orvani/plugins/frontmatter.py +++ b/orvani/plugins/frontmatter.py @@ -12,4 +12,11 @@ def reader(o): for meta in y.metadata.keys(): if not meta in o.schema: del context[meta] - return context \ No newline at end of file + return context + +def writer(o): + y = frontmatter.load(o.file) + for key in o.context: + y[key] = o.context[key] + with open(o.file, 'w') as data: + data.write(frontmatter.dumps(y)) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index f29c13c..92bdc8e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ olefile==0.46 pytaglib==2.0.0 PyYAML==6.0 -rich==13.4.1 +rich==13.4.1 \ No newline at end of file