From 05edcb9f82f2f8edd17499de298efb2809ac2674 Mon Sep 17 00:00:00 2001 From: randogoth Date: Fri, 9 Jun 2023 13:35:39 +0300 Subject: [PATCH] changed plugin system --- TODO.md | 2 +- orvani/cmd.py | 10 +--------- orvani/orvani.py | 36 ++++++++++++++++++------------------ 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/TODO.md b/TODO.md index 73b98d3..7d31ae0 100644 --- a/TODO.md +++ b/TODO.md @@ -2,4 +2,4 @@ tags: - frrr --- -+ plugin for xattr \ No newline at end of file ++ plugin writers \ No newline at end of file diff --git a/orvani/cmd.py b/orvani/cmd.py index 85bf384..f9652be 100644 --- a/orvani/cmd.py +++ b/orvani/cmd.py @@ -92,15 +92,7 @@ def main(): orvani = OrvaniTag() - @orvani.metareader - def reader(o): - frontmatter.reader(o) - hashdoubleslash.reader(o) - id3.reader(o) - office.reader(o) - pdf.reader(o) - - orvani.PLUGINS = args.no_plugins + orvani.ENABLE_PLUGINS = args.no_plugins if args.command in ['add', 'a']: if args.file and args.context: diff --git a/orvani/orvani.py b/orvani/orvani.py index 131b2c2..6981f30 100644 --- a/orvani/orvani.py +++ b/orvani/orvani.py @@ -2,6 +2,7 @@ import os import yaml import shutil from pathlib import Path +from importlib import import_module from types import SimpleNamespace from pprint import pprint @@ -15,11 +16,16 @@ CONTEXTS = { 'tags' : '#', 'note' : None } +DEFAULT_PLUGINS = [ + 'frontmatter', + 'hashdoubleslash', + 'id3', + 'office', + 'pdf' +] class OrvaniTag: - META_IN = [] - META_OUT = [] INDEX = {} # our search index CACHEFILE = '' # search index cache ('.index.yaml') DB = dict() # holds the folder context data @@ -29,9 +35,10 @@ class OrvaniTag: FILENAME = '' # folder context data filename ('.context.yaml') FILE = '' # holds the folder context file path CTX = {} - PLUGINS = True + ENABLE_PLUGINS = True + PLUGINS = [] - def __init__(self, path: str=CONTEXT_FILE, cache: str=CACHE_FILE, contexts=CONTEXTS): + def __init__(self, path: str=CONTEXT_FILE, cache: str=CACHE_FILE, contexts=CONTEXTS, plugins:list=DEFAULT_PLUGINS): """ Initialization @@ -40,6 +47,7 @@ class OrvaniTag: contexts (dict): override the default contexts """ + self.PLUGINS = plugins self.FILENAME = os.path.basename(path) self.CACHEFILE = cache self.CTX = contexts @@ -288,7 +296,7 @@ class OrvaniTag: self.__indexfolder(path) if recursive: for (root,dirs,files) in os.walk(path): - if self.PLUGINS: + if self.ENABLE_PLUGINS: if files: for file in files: fpath = os.path.join(root, file) @@ -412,17 +420,10 @@ class OrvaniTag: common_elements = common_elements.intersection(mresult[key]) return list(common_elements) - - def metareader(self, function): - self.META_IN.append(function) + def register_plugin(self, function): + self.PLUGINS.append(function) return function - - def metawriter(self, function): - self.META_OUT.append(function) - return function - - def metadata(self, filename: str): obj = { 'path' : filename, @@ -433,7 +434,6 @@ class OrvaniTag: 'context' : self.__context } o = SimpleNamespace(**obj) - for reader in self.META_IN: - reader(o) - for writer in self.META_OUT: - writer(o) + for plugin_name in self.PLUGINS: + plugin = import_module('plugins.' + plugin_name) + plugin.reader(o) \ No newline at end of file