frontmatter reader plugin
This commit is contained in:
parent
8489f6cd6d
commit
f04ffae75a
11 changed files with 56 additions and 9 deletions
Binary file not shown.
|
|
@ -5,6 +5,7 @@ from pathlib import Path
|
|||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
from orvani import OrvaniTag, CONTEXT_FILE, CACHE_FILE, CONTEXTS
|
||||
from plugins import frontmatter
|
||||
|
||||
def mktable(query: str, index: dict):
|
||||
console = Console()
|
||||
|
|
@ -87,6 +88,12 @@ def main():
|
|||
|
||||
orvani = OrvaniTag()
|
||||
|
||||
@orvani.metareader
|
||||
def reader(o):
|
||||
frontmatter.reader(o)
|
||||
|
||||
# orvani.metadata()
|
||||
|
||||
if args.command in ['add', 'a']:
|
||||
if args.file and args.context:
|
||||
orvani.add(args.file, *args.context)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import os
|
|||
import yaml
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
# Defaults
|
||||
|
||||
|
|
@ -60,7 +61,7 @@ class OrvaniTag:
|
|||
self.CONFIG = y['config']
|
||||
else:
|
||||
self.DB = dict()
|
||||
self.CONFIG = dict()
|
||||
# self.CONFIG = dict()
|
||||
|
||||
def __save(self):
|
||||
"""
|
||||
|
|
@ -113,6 +114,7 @@ class OrvaniTag:
|
|||
if target.exists():
|
||||
self.__cd(target.parent.name)
|
||||
context = self.__context(argv)
|
||||
print(context)
|
||||
if target.name in self.DB:
|
||||
for context_key in self.CTX.keys():
|
||||
if self.CTX[context_key] != None:
|
||||
|
|
@ -278,6 +280,10 @@ class OrvaniTag:
|
|||
for dir in dirs:
|
||||
rpath = os.path.join(root, dir)
|
||||
self.__indexfolder(rpath)
|
||||
if files:
|
||||
for file in files:
|
||||
fpath = os.path.join(root, file)
|
||||
self.metadata(fpath)
|
||||
if cache:
|
||||
with open(cachefile, 'w+') as data:
|
||||
yaml.dump(self.INDEX, data)
|
||||
|
|
@ -327,7 +333,6 @@ class OrvaniTag:
|
|||
if filter and not filter == context_key:
|
||||
continue
|
||||
if context_key in query:
|
||||
# Is it a list of context items?
|
||||
if type(query[context_key]) == list:
|
||||
for q in query[context_key]:
|
||||
if exact:
|
||||
|
|
@ -368,7 +373,14 @@ class OrvaniTag:
|
|||
self.META_OUT.append(function)
|
||||
return function
|
||||
|
||||
def read_meta(self):
|
||||
for callback in self.META_IN:
|
||||
entry = callback(self.CONFIG, self.DB)
|
||||
print(entry)
|
||||
def metadata(self, filename: str):
|
||||
obj = {
|
||||
'path' : filename,
|
||||
'index' : self.INDEX,
|
||||
'schema' : self.CTX
|
||||
}
|
||||
o = SimpleNamespace(**obj)
|
||||
for reader in self.META_IN:
|
||||
reader(o)
|
||||
for writer in self.META_OUT:
|
||||
writer(o)
|
||||
|
|
|
|||
0
orvani/plugins/__init__.py
Normal file
0
orvani/plugins/__init__.py
Normal file
BIN
orvani/plugins/__pycache__/__init__.cpython-310.pyc
Normal file
BIN
orvani/plugins/__pycache__/__init__.cpython-310.pyc
Normal file
Binary file not shown.
BIN
orvani/plugins/__pycache__/frontmatter.cpython-310.pyc
Normal file
BIN
orvani/plugins/__pycache__/frontmatter.cpython-310.pyc
Normal file
Binary file not shown.
17
orvani/plugins/frontmatter.py
Normal file
17
orvani/plugins/frontmatter.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import frontmatter
|
||||
from pathlib import Path
|
||||
|
||||
def reader(o):
|
||||
file = Path(o.path)
|
||||
if file.suffix in ['.md', '.txt', '.markdown', '.rst']:
|
||||
with open(file, 'r') as data:
|
||||
y = frontmatter.load(data)
|
||||
if y.metadata:
|
||||
context = y.metadata.copy()
|
||||
for meta in y.metadata.keys():
|
||||
if not meta in o.schema:
|
||||
del context[meta]
|
||||
if o.path in o.index['files']:
|
||||
o.index['files'][o.path].update(context)
|
||||
else:
|
||||
o.index['files'][o.path] = context
|
||||
Loading…
Add table
Add a link
Reference in a new issue