diff --git a/orvani/orvani.py b/orvani/orvani.py index 673d18e..b246952 100644 --- a/orvani/orvani.py +++ b/orvani/orvani.py @@ -452,20 +452,40 @@ class OrvaniTag: for context_key in context.keys(): self.__updateindex(filename, context, context_key) + def __collect_contexts(self, context: dict): + elements = [] + context_keys = list(CONTEXTS.keys()) + for context_key in context_keys: + if not context_key in context: + context[context_key] = None + if context[context_key]: + if CONTEXTS[context_key]: + prefix = CONTEXTS[context_key] + else: + prefix = '' + if not type(context[context_key]) == str: + elements.append(' '.join([prefix + tag for tag in context[context_key]])) + else: + elements.append(context[context_key]) + else: + elements.append(None) + return elements + def export(self, filename: str, method: str): obj = { 'file' : Path(), 'config' : self.CONFIG, 'schema' : self.CTX, 'context' : {}, + 'collect' : self.__collect_contexts } 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) + plugin = import_module('plugins.' + plugin_name) + if plugin.NAME == method: suffixes = plugin.TYPES if plugin_name in self.CONFIG: if 'types' in self.CONFIG[plugin_name]: @@ -475,8 +495,8 @@ class OrvaniTag: self.__makeindex(o.file.parent, recursive=False) items = self.INDEX['items'] for found in items.keys(): - if './' + o.file.name == found: + if str(o.file).replace('./', '') == found.replace('./', ''): o.context = items[found] if hasattr(plugin, 'writer'): plugin.writer(o) - + diff --git a/orvani/plugins/hashdoubleslash.py b/orvani/plugins/hashdoubleslash.py index 37de317..1ca1f16 100644 --- a/orvani/plugins/hashdoubleslash.py +++ b/orvani/plugins/hashdoubleslash.py @@ -1,12 +1,35 @@ -""" -#// +orvani @python #example It even works in Python code -""" import re from pathlib import Path NAME = 'hash//' -TYPES = ['.md', '.txt', '.py'] REGEX = r'\s*([@+]\w+|#\w+(?'], + '.java' : MLC_C, + '.js' : MLC_C, + '.kt' : MLC_C, + '.md' : ['---', '---'], + '.php' : MLC_C, + '.py' : MLC_Python, + '.rb' : MLC_Perl, + '.rs' : MLC_C, + '.ts' : MLC_C, + '.txt' : ['---', '---'], +} +TYPES = MAP.keys() def reader(o): with o.file.open() as data: @@ -18,4 +41,22 @@ def reader(o): 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 + return context + +def writer(o): + string = PREFIX + ' '.join([x for x in o.collect(o.context) if x != None]) + '\n' + with o.file.open() as data: + content = data.readlines() + for num, line in enumerate(content): + if line.strip().startswith("#// ") or num > 3: + break + if not num > 3 and line.strip().startswith("#// "): + content[num] = string + with o.file.open('w') as file: + file.writelines(content) + else: + frontmatter = MAP[o.file.suffix][0] + '\n' + string + MAP[o.file.suffix][1] + '\n' + with o.file.open('r+') as file: + content = file.read() + file.seek(0, 0) + file.write(frontmatter + content) \ No newline at end of file diff --git a/orvani/plugins/id3.py b/orvani/plugins/id3.py index f8c5304..0bd95ca 100644 --- a/orvani/plugins/id3.py +++ b/orvani/plugins/id3.py @@ -30,8 +30,15 @@ def reader(o): context = o.context(argv) if not universal in context: context[universal] = [] + else: + context[universal] = [context[universal]] if id3map.keys(): for key in id3map.keys(): if key in context.keys(): context[key] += id3map[key] - return context \ No newline at end of file + return context + +def writer(o): + string = ' '.join([x for x in o.collect(o.context) if x != None]) + with taglib.File(o.file, save_on_exit=True) as song: + song.tags['COMMENT'] = [string] \ No newline at end of file diff --git a/orvani/plugins/pdf.py b/orvani/plugins/pdf.py index c42a292..3972601 100644 --- a/orvani/plugins/pdf.py +++ b/orvani/plugins/pdf.py @@ -15,4 +15,10 @@ def reader(o): 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 + return context + +def writer(o): + string = ' '.join([x for x in o.collect(o.context) if x != None]) + pdf = PdfReader(o.file) + pdf.Info.Keywords = string + PdfWriter(o.file, trailer=pdf).write() diff --git a/requirements.txt b/requirements.txt index 92bdc8e..14a4040 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ olefile==0.46 pytaglib==2.0.0 -PyYAML==6.0 -rich==13.4.1 \ No newline at end of file +PyYAML==6.0 \ No newline at end of file diff --git a/test/.context.yaml b/test/.context.yaml index 170423e..013efa8 100644 --- a/test/.context.yaml +++ b/test/.context.yaml @@ -5,8 +5,6 @@ config: note: title projects: album tags: genre - types: - - .wav items: sub: contexts: diff --git a/test/chirpy.wav b/test/chirpy.wav index 53aba4a..e2a7baa 100644 Binary files a/test/chirpy.wav and b/test/chirpy.wav differ diff --git a/test/sub/statement_14793553_USD_2022-11-01_2023-01-31.pdf b/test/sub/statement_14793553_USD_2022-11-01_2023-01-31.pdf index 6e8de79..614e6d8 100644 Binary files a/test/sub/statement_14793553_USD_2022-11-01_2023-01-31.pdf and b/test/sub/statement_14793553_USD_2022-11-01_2023-01-31.pdf differ