From 7e31c4d997f500604c4758d9bbf6b26c4389ae90 Mon Sep 17 00:00:00 2001 From: randogoth Date: Fri, 9 Jun 2023 14:39:19 +0300 Subject: [PATCH] simplified plugins --- .context.yaml | 6 -- README.md | 2 +- orvani/orvani.py | 20 +++++-- orvani/plugins/frontmatter.py | 26 +++----- orvani/plugins/hashdoubleslash.py | 31 ++++------ orvani/plugins/id3.py | 60 +++++++++---------- orvani/plugins/office.py | 98 ++++++++++++++----------------- orvani/plugins/pdf.py | 26 +++----- 8 files changed, 113 insertions(+), 156 deletions(-) diff --git a/.context.yaml b/.context.yaml index 9cf8360..05b0551 100644 --- a/.context.yaml +++ b/.context.yaml @@ -1,9 +1,3 @@ -config: - id3: - types: - - .wav - plugins: - - id3 items: TODO.md: contexts: diff --git a/README.md b/README.md index a4b1fc9..17907f2 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ projects: - test2 contexts: - frrreee -howard: +tags: - fool --- # Orvani Tagger diff --git a/orvani/orvani.py b/orvani/orvani.py index 6981f30..a9dedb2 100644 --- a/orvani/orvani.py +++ b/orvani/orvani.py @@ -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) \ No newline at end of file + 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) \ No newline at end of file diff --git a/orvani/plugins/frontmatter.py b/orvani/plugins/frontmatter.py index 41ad080..a74ecda 100644 --- a/orvani/plugins/frontmatter.py +++ b/orvani/plugins/frontmatter.py @@ -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) \ No newline at end of file + 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 \ No newline at end of file diff --git a/orvani/plugins/hashdoubleslash.py b/orvani/plugins/hashdoubleslash.py index 85c4164..37de317 100644 --- a/orvani/plugins/hashdoubleslash.py +++ b/orvani/plugins/hashdoubleslash.py @@ -9,24 +9,13 @@ TYPES = ['.md', '.txt', '.py'] REGEX = r'\s*([@+]\w+|#\w+(? 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) - \ No newline at end of file + 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 \ No newline at end of file diff --git a/orvani/plugins/id3.py b/orvani/plugins/id3.py index d90f1ef..f8c5304 100644 --- a/orvani/plugins/id3.py +++ b/orvani/plugins/id3.py @@ -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) \ No newline at end of file + for key in id3map.keys(): + if key in context.keys(): + context[key] += id3map[key] + return context \ No newline at end of file diff --git a/orvani/plugins/office.py b/orvani/plugins/office.py index bd7d192..469b0d6 100644 --- a/orvani/plugins/office.py +++ b/orvani/plugins/office.py @@ -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) \ No newline at end of file + # 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 \ No newline at end of file diff --git a/orvani/plugins/pdf.py b/orvani/plugins/pdf.py index c630793..c42a292 100644 --- a/orvani/plugins/pdf.py +++ b/orvani/plugins/pdf.py @@ -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) \ No newline at end of file + 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 \ No newline at end of file