writer plugin framework

This commit is contained in:
randogoth 2023-06-09 17:16:13 +03:00
parent d36123b962
commit 34f9c0b6a9
6 changed files with 73 additions and 23 deletions

View file

@ -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()

View file

@ -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)
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)

View file

@ -12,4 +12,11 @@ def reader(o):
for meta in y.metadata.keys():
if not meta in o.schema:
del context[meta]
return context
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))