simplified plugins
This commit is contained in:
parent
05edcb9f82
commit
7e31c4d997
8 changed files with 113 additions and 156 deletions
|
|
@ -295,7 +295,7 @@ class OrvaniTag:
|
|||
else:
|
||||
self.__indexfolder(path)
|
||||
if recursive:
|
||||
for (root,dirs,files) in os.walk(path):
|
||||
for (root,dirs,files) in os.walk(path, topdown=True):
|
||||
if self.ENABLE_PLUGINS:
|
||||
if files:
|
||||
for file in files:
|
||||
|
|
@ -426,14 +426,26 @@ class OrvaniTag:
|
|||
|
||||
def metadata(self, filename: str):
|
||||
obj = {
|
||||
'path' : filename,
|
||||
'file' : Path(),
|
||||
'config' : self.CONFIG,
|
||||
'index' : self.INDEX,
|
||||
'schema' : self.CTX,
|
||||
'update' : self.__updateindex,
|
||||
'context' : self.__context
|
||||
'context' : self.__context,
|
||||
}
|
||||
o = SimpleNamespace(**obj)
|
||||
for plugin_name in self.PLUGINS:
|
||||
if 'plugins' in self.CONFIG:
|
||||
if plugin_name not in self.CONFIG['plugins']:
|
||||
continue
|
||||
plugin = import_module('plugins.' + plugin_name)
|
||||
plugin.reader(o)
|
||||
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:
|
||||
context = plugin.reader(o)
|
||||
if context:
|
||||
for context_key in context.keys():
|
||||
self.__updateindex(filename, context, context_key)
|
||||
|
|
@ -5,21 +5,11 @@ NAME = 'frontmatter'
|
|||
TYPES = ['.md', '.txt', '.markdown', '.rst']
|
||||
|
||||
def reader(o):
|
||||
if 'plugins' in o.config:
|
||||
if NAME not in o.config['plugins']:
|
||||
return
|
||||
file = Path(o.path)
|
||||
suffixes = TYPES
|
||||
if NAME in o.config:
|
||||
if 'types' in o.config[NAME]:
|
||||
suffixes = o.config[NAME]['types']
|
||||
if file.suffix in suffixes:
|
||||
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]
|
||||
for context_key in context.keys():
|
||||
o.update(o.path, context, context_key)
|
||||
with open(o.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]
|
||||
return context
|
||||
|
|
@ -9,24 +9,13 @@ TYPES = ['.md', '.txt', '.py']
|
|||
REGEX = r'\s*([@+]\w+|#\w+(?<!#//))'
|
||||
|
||||
def reader(o):
|
||||
if 'plugins' in o.config:
|
||||
if NAME not in o.config['plugins']:
|
||||
return
|
||||
file = Path(o.path)
|
||||
suffixes = TYPES
|
||||
if NAME in o.config:
|
||||
if 'types' in o.config[NAME]:
|
||||
suffixes = o.config[NAME]['types']
|
||||
if file.suffix in suffixes:
|
||||
with file.open() as data:
|
||||
for line_num, line in enumerate(data, start=1):
|
||||
if line_num > 5:
|
||||
break
|
||||
if line.strip().startswith("#// "):
|
||||
tags = re.findall(REGEX, line.strip()[3:])
|
||||
note = re.sub(REGEX, '', line.strip()[3:]).lstrip()
|
||||
argv = ' '.join(tags).split(' ') + [note]
|
||||
context = o.context(argv)
|
||||
for context_key in context.keys():
|
||||
o.update(o.path, context, context_key)
|
||||
|
||||
with o.file.open() as data:
|
||||
for line_num, line in enumerate(data, start=1):
|
||||
if line_num > 5:
|
||||
break
|
||||
if line.strip().startswith("#// "):
|
||||
tags = re.findall(REGEX, line.strip()[3:])
|
||||
note = re.sub(REGEX, '', line.strip()[3:]).lstrip()
|
||||
argv = ' '.join(tags).split(' ') + [note]
|
||||
context = o.context(argv)
|
||||
return context
|
||||
|
|
@ -7,37 +7,31 @@ TYPES = ['.mp3', '.wav', '.flac', '.ogg']
|
|||
REGEX = r'\s*([@+]\w+|#\w+)'
|
||||
|
||||
def reader(o):
|
||||
if 'plugins' in o.config:
|
||||
if NAME not in o.config['plugins']:
|
||||
return
|
||||
file = Path(o.path)
|
||||
suffixes = TYPES
|
||||
if NAME in o.config:
|
||||
if 'types' in o.config[NAME]:
|
||||
suffixes = o.config[NAME]['types']
|
||||
if file.suffix in suffixes:
|
||||
with taglib.File(o.path, save_on_exit=False) as song:
|
||||
metadata = song.tags
|
||||
id3map = {}
|
||||
if 'id3' in o.config:
|
||||
if 'map' in o.config['id3']:
|
||||
id3map = o.config['id3']['map'].copy()
|
||||
for context_key in id3map:
|
||||
if context_key not in o.schema.keys():
|
||||
del id3map[context_key]
|
||||
else:
|
||||
ctag = str(id3map[context_key]).upper()
|
||||
if ctag in metadata:
|
||||
id3map[context_key] = metadata[ctag]
|
||||
universal = {i for i in o.schema if o.schema[i]==None}.pop()
|
||||
with taglib.File(o.file, save_on_exit=False) as song:
|
||||
metadata = song.tags
|
||||
id3map = {}
|
||||
if 'id3' in o.config:
|
||||
if 'map' in o.config['id3']:
|
||||
id3map = o.config['id3']['map'].copy()
|
||||
for context_key in id3map:
|
||||
if context_key not in o.schema.keys():
|
||||
del id3map[context_key]
|
||||
else:
|
||||
ctag = str(id3map[context_key]).upper()
|
||||
if ctag in metadata:
|
||||
id3map[context_key] = metadata[ctag]
|
||||
if 'COMMENT' in metadata:
|
||||
comment = metadata['COMMENT']
|
||||
tags = re.findall(REGEX, comment[0].strip())
|
||||
note = re.sub(REGEX, '', comment[0].strip()).lstrip()
|
||||
argv = ' '.join(tags).split(' ') + [note]
|
||||
argv = [x for x in argv if x != '']
|
||||
context = o.context(argv)
|
||||
if not universal in context:
|
||||
context[universal] = []
|
||||
if id3map.keys():
|
||||
for context_key in id3map.keys():
|
||||
o.update(o.path, id3map, context_key)
|
||||
if 'COMMENT' in metadata:
|
||||
comment = metadata['COMMENT']
|
||||
tags = re.findall(REGEX, comment[0].strip())
|
||||
note = re.sub(REGEX, '', comment[0].strip()).lstrip()
|
||||
argv = ' '.join(tags).split(' ') + [note]
|
||||
argv = [x for x in argv if x != '']
|
||||
context = o.context(argv)
|
||||
for context_key in context.keys():
|
||||
o.update(o.path, context, context_key)
|
||||
for key in id3map.keys():
|
||||
if key in context.keys():
|
||||
context[key] += id3map[key]
|
||||
return context
|
||||
|
|
@ -29,61 +29,49 @@ def unzip_file(orig_file, tmp_dir):
|
|||
return type[0]
|
||||
|
||||
def reader(o):
|
||||
if 'plugins' in o.config:
|
||||
if NAME not in o.config['plugins']:
|
||||
return
|
||||
file = Path(o.path)
|
||||
suffixes = TYPES
|
||||
if NAME in o.config:
|
||||
if 'types' in o.config[NAME]:
|
||||
suffixes = o.config[NAME]['types']
|
||||
if file.suffix in suffixes:
|
||||
tmp_folder = Path('unzip__' + file.stem)
|
||||
file_type = unzip_file(o.path, tmp_folder)
|
||||
tags = note = []
|
||||
tmp_folder = Path('unzip__' + o.file.stem)
|
||||
file_type = unzip_file(o.file, tmp_folder)
|
||||
tags = note = []
|
||||
|
||||
# OpenOffice / LibreOffice / Open Document Format
|
||||
if file_type in ['odt', 'ods', 'odp']:
|
||||
meta_file = tmp_folder / 'meta.xml'
|
||||
meta_content = meta_file.read_text()
|
||||
tagsearch = re.findall(RE_ODT_CTX, meta_content)
|
||||
if tagsearch:
|
||||
tags = list(set(tagsearch[0]))
|
||||
notesearch = re.findall(RE_ODT_NOTE, meta_content)
|
||||
if notesearch:
|
||||
note = list(set(notesearch[0]))
|
||||
argv = [x for x in ' '.join(tags).split(' ') + note if x != '']
|
||||
context = o.context(argv)
|
||||
for context_key in context.keys():
|
||||
o.update(o.path, context, context_key)
|
||||
shutil.rmtree(tmp_folder)
|
||||
# OpenOffice / LibreOffice / Open Document Format
|
||||
if file_type in ['odt', 'ods', 'odp']:
|
||||
meta_file = tmp_folder / 'meta.xml'
|
||||
meta_content = meta_file.read_text()
|
||||
tagsearch = re.findall(RE_ODT_CTX, meta_content)
|
||||
if tagsearch:
|
||||
tags = list(set(tagsearch[0]))
|
||||
notesearch = re.findall(RE_ODT_NOTE, meta_content)
|
||||
if notesearch:
|
||||
note = list(set(notesearch[0]))
|
||||
argv = [x for x in ' '.join(tags).split(' ') + note if x != '']
|
||||
context = o.context(argv)
|
||||
shutil.rmtree(tmp_folder)
|
||||
return context
|
||||
|
||||
# Microsoft Office Format
|
||||
elif file_type in ['docx', 'xlsx', 'pptx']:
|
||||
meta_file = tmp_folder / 'docProps' / 'core.xml'
|
||||
meta_content = meta_file.read_text()
|
||||
tagsearch = re.findall(RE_ODT_CTX, meta_content)
|
||||
if tagsearch:
|
||||
tags = list(set(tagsearch[0]))
|
||||
notesearch = re.findall(RE_ODT_NOTE, meta_content)
|
||||
if notesearch:
|
||||
note = list(set(notesearch[0]))
|
||||
argv = [x for x in ' '.join(tags).split(' ') + note if x != '']
|
||||
context = o.context(argv)
|
||||
for context_key in context.keys():
|
||||
o.update(o.path, context, context_key)
|
||||
shutil.rmtree(tmp_folder)
|
||||
# Microsoft Office Format
|
||||
elif file_type in ['docx', 'xlsx', 'pptx']:
|
||||
meta_file = tmp_folder / 'docProps' / 'core.xml'
|
||||
meta_content = meta_file.read_text()
|
||||
tagsearch = re.findall(RE_ODT_CTX, meta_content)
|
||||
if tagsearch:
|
||||
tags = list(set(tagsearch[0]))
|
||||
notesearch = re.findall(RE_ODT_NOTE, meta_content)
|
||||
if notesearch:
|
||||
note = list(set(notesearch[0]))
|
||||
argv = [x for x in ' '.join(tags).split(' ') + note if x != '']
|
||||
context = o.context(argv)
|
||||
shutil.rmtree(tmp_folder)
|
||||
return context
|
||||
|
||||
# Legacy Microsoft Office Format
|
||||
elif file_type in ['application/msword', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint']:
|
||||
ole = OleFileIO(o.path)
|
||||
meta = ole.get_metadata()
|
||||
ole.close()
|
||||
if meta.keywords:
|
||||
tags = meta.keywords.decode('UTF-8').split(' ')
|
||||
if meta.comments:
|
||||
note = [meta.comments.decode('UTF-8')]
|
||||
argv = [x for x in tags + note if x != '']
|
||||
context = o.context(argv)
|
||||
for context_key in context.keys():
|
||||
o.update(o.path, context, context_key)
|
||||
# Legacy Microsoft Office Format
|
||||
elif file_type in ['application/msword', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint']:
|
||||
ole = OleFileIO(o.file)
|
||||
meta = ole.get_metadata()
|
||||
ole.close()
|
||||
if meta.keywords:
|
||||
tags = meta.keywords.decode('UTF-8').split(' ')
|
||||
if meta.comments:
|
||||
note = [meta.comments.decode('UTF-8')]
|
||||
argv = [x for x in tags + note if x != '']
|
||||
context = o.context(argv)
|
||||
return context
|
||||
|
|
@ -8,21 +8,11 @@ TYPES = ['.pdf']
|
|||
REGEX = r'\s*([@+]\w+|#\w+)'
|
||||
|
||||
def reader(o):
|
||||
if 'plugins' in o.config:
|
||||
if NAME not in o.config['plugins']:
|
||||
return
|
||||
file = Path(o.path)
|
||||
suffixes = TYPES
|
||||
if NAME in o.config:
|
||||
if 'types' in o.config[NAME]:
|
||||
suffixes = o.config[NAME]['types']
|
||||
if file.suffix in suffixes:
|
||||
pdf = PdfReader(o.path)
|
||||
if pdf.Info.Keywords:
|
||||
tags = re.findall(REGEX, pdf.Info.Keywords.strip())
|
||||
note = re.sub(REGEX, '', pdf.Info.Keywords.strip()).replace('(', '').replace(',', '').replace(')', '').strip()
|
||||
argv = ' '.join(tags).split(' ') + [note]
|
||||
argv = [x for x in argv if x != '']
|
||||
context = o.context(argv)
|
||||
for context_key in context.keys():
|
||||
o.update(o.path, context, context_key)
|
||||
pdf = PdfReader(o.file)
|
||||
if pdf.Info.Keywords:
|
||||
tags = re.findall(REGEX, pdf.Info.Keywords.strip())
|
||||
note = re.sub(REGEX, '', pdf.Info.Keywords.strip()).replace('(', '').replace(',', '').replace(')', '').strip()
|
||||
argv = ' '.join(tags).split(' ') + [note]
|
||||
argv = [x for x in argv if x != '']
|
||||
context = o.context(argv)
|
||||
return context
|
||||
Loading…
Add table
Add a link
Reference in a new issue