diff --git a/context.txt b/context.txt new file mode 100644 index 0000000..f1a81e3 --- /dev/null +++ b/context.txt @@ -0,0 +1,18 @@ +./README.md --- @frrreee #fool #writeme +./TODO.md --- +bokertov @python #rusty #frrr Chawera +./requirements.txt --- pip reqs +./test --- @folder #spaces +./COMMANDS.md --- +coding @erfwegf #super #great This is awesome +./test/folder with spaces --- +totally #spaces +./test/office.odt --- +oopsie +orvani @this #whoa #that and a note +./test/sheet.xlsx --- +orvani @calc #tessssting +./test/sub --- @folder +./test/test.txt --- +test @test #faa #test frrr +./test/table.ods --- @calc #tessssting +./test/tableold.xls --- @calc #tessssting +./test/chirpy.wav --- +sensorwatch +parakeetio @Chripo @watch @Chripo McFlu #funk McFlu McFlu This is a title This is a title This is a title +./test/oldword.doc --- and a note +./test/word.docx --- @concon #tagz +./test/sub/statement_14793553_USD_2022-11-01_2023-01-31.pdf --- +yeeha @pdfcontext #yay #frrrrrrrr And a comment too +./test/folder with spaces/file with spaces.txt --- #spaces +./orvani/plugins/hashdoubleslash.py --- +orvani @python #example It even works in Python code diff --git a/.context.yaml b/context.yaml similarity index 93% rename from .context.yaml rename to context.yaml index 9690647..b893c3b 100644 --- a/.context.yaml +++ b/context.yaml @@ -21,4 +21,5 @@ items: contexts: - folder projects: [] - tags: [] + tags: + - spaces diff --git a/orvani/cmd.py b/orvani/cmd.py index 2c5e2fc..b511060 100644 --- a/orvani/cmd.py +++ b/orvani/cmd.py @@ -152,5 +152,11 @@ def main(): if args.file and args.context: orvani.write(args.context[0], args.file) + elif args.command in ['export']: + orvani.export(args.file) + + elif args.command in ['import']: + orvani.load(args.file) + if __name__ == '__main__': main() diff --git a/orvani/orvani.py b/orvani/orvani.py index 19f7ece..4751157 100644 --- a/orvani/orvani.py +++ b/orvani/orvani.py @@ -1,4 +1,5 @@ import os +import re import yaml import shutil from pathlib import Path @@ -37,6 +38,7 @@ class OrvaniTag: CTX = {} ENABLE_PLUGINS = True PLUGINS = [] + REGEX = r'\s*([@+]\w+|#\w+)' def __init__(self, path: str=CONTEXT_FILE, cache: str=CACHE_FILE, contexts=CONTEXTS, plugins:list=DEFAULT_PLUGINS): """ @@ -121,7 +123,7 @@ class OrvaniTag: for context_key in self.CTX.keys(): if self.CTX[context_key] != None: context.update( { context_key : []}) - for arg in argv: + for arg in [x for x in argv if len(x) > 0]: arg.replace(',', '') for context_key in self.CTX.keys(): if str(arg)[0] in self.CTX.values(): @@ -427,12 +429,20 @@ class OrvaniTag: self.PLUGINS.append(function) return function + def get_argv(self, context_string: str): + tags = re.findall(self.REGEX, context_string) + note = re.sub(self.REGEX, '', context_string).lstrip() + argv = ' '.join(tags).split(' ') + [note] + argv = [x for x in list(set(argv)) if x != ''] + return argv + def __metadata(self, filename: str): obj = { 'file' : Path(), 'config' : self.CONFIG, 'schema' : self.CTX, 'context' : self.__context, + 'string' : self.get_argv } o = SimpleNamespace(**obj) for plugin_name in self.PLUGINS: @@ -457,7 +467,7 @@ class OrvaniTag: context_keys = list(CONTEXTS.keys()) for context_key in context_keys: if not context_key in context: - context[context_key] = None + context[context_key] = '' if context[context_key]: if CONTEXTS[context_key]: prefix = CONTEXTS[context_key] @@ -467,8 +477,6 @@ class OrvaniTag: elements.append(' '.join([prefix + tag for tag in context[context_key]])) else: elements.append(context[context_key]) - else: - elements.append(None) return elements def write(self, filename: str, method: str): @@ -477,7 +485,8 @@ class OrvaniTag: 'config' : self.CONFIG, 'schema' : self.CTX, 'context' : {}, - 'collect' : self.__collect_contexts + 'collect' : self.__collect_contexts, + 'string' : self.get_argv } o = SimpleNamespace(**obj) for plugin_name in self.PLUGINS: @@ -500,3 +509,23 @@ class OrvaniTag: if hasattr(plugin, 'writer'): plugin.writer(o) + def export(self, filename:str): + if filename == '' or filename == None: + filename = 'context.txt' + file = Path(filename) + index = self.list(file.parent, False, False, True, False, False, '') + items = index['index']['items'] + content = "" + for path in items: + string = ' '.join([x for x in self.__collect_contexts(items[path]) if x != None]) + content += f'{path} --- {string}\n' + file.write_text(content) + + def load(self, filename:str): + if filename == '' or filename == None: + filename = 'context.txt' + file = Path(filename) + imported = file.read_text() + known = self.list(file.parent, False, False, True, False, False, '')['index']['items'] + print(known) + diff --git a/orvani/plugins/hashdoubleslash.py b/orvani/plugins/hashdoubleslash.py index 1ca1f16..28e8130 100644 --- a/orvani/plugins/hashdoubleslash.py +++ b/orvani/plugins/hashdoubleslash.py @@ -37,19 +37,20 @@ def reader(o): 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] + argv = o.string(line.strip()[3:]) context = o.context(argv) return context def writer(o): - string = PREFIX + ' '.join([x for x in o.collect(o.context) if x != None]) + '\n' + string = PREFIX + ' '.join(o.collect(o.context)) + '\n' with o.file.open() as data: content = data.readlines() - for num, line in enumerate(content): - if line.strip().startswith("#// ") or num > 3: - break + num = 0 + line = '' + if len(content): + 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: diff --git a/orvani/plugins/id3.py b/orvani/plugins/id3.py index 0bd95ca..34e9485 100644 --- a/orvani/plugins/id3.py +++ b/orvani/plugins/id3.py @@ -23,10 +23,7 @@ def reader(o): 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 != ''] + argv = o.string(comment[0].strip()) context = o.context(argv) if not universal in context: context[universal] = [] @@ -39,6 +36,6 @@ def reader(o): return context def writer(o): - string = ' '.join([x for x in o.collect(o.context) if x != None]) + string = ' '.join(o.collect(o.context)) 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/office.py b/orvani/plugins/office.py index 81fdcfa..3f1d255 100644 --- a/orvani/plugins/office.py +++ b/orvani/plugins/office.py @@ -7,9 +7,8 @@ from olefile import OleFileIO NAME = 'office' TYPES = ['.doc', '.xls', '.ppt', '.docx', '.xlsx', '.pptx', '.odt', '.ods', '.odp'] -RE_ODT_CTX = r"(?P
)(?P.*?)(?P<\/meta:keyword>)"
-RE_DOCX_CTX = r"(?P
)(?P.*?)(?P<\/cp:keywords>)"
-REGEX = r"(?P
)(?P.*?)(?P<\/dc:description>)"
+XML_REGEX = r"(?P
)(?P.*?)(?P<\/dc:description>)"
+REGEX = r'\s*([@+]\w+|#\w+)'
 
 # Office Document processing inspired by
 # https://github.com/kappapiana/anonymize/blob/master/anonymize.py
@@ -30,16 +29,12 @@ def unzip_file(orig_file, tmp_dir):
     else:
         return type[0]
 
-def get_context(meta_file, re_ctx):
-    tags = note = []
+def get_context_string(meta_file):
+    ctx = ''
     meta_content = meta_file.read_text()
-    tagsearch = re.search(re_ctx, meta_content)
-    if tagsearch:
-        tags = tagsearch.group('body').split(' ')
-    notesearch = re.search(REGEX, meta_content)
-    if notesearch:
-        note = [notesearch.group('body')]
-    return [x for x in ' '.join(tags).split(' ') + note if x != '']
+    csearch = re.search(XML_REGEX, meta_content)
+    if csearch:
+        return csearch.group('body')
     
 
 def reader(o):
@@ -50,7 +45,7 @@ def reader(o):
     # OpenOffice / LibreOffice / Open Document Format
     if file_type in ['odt', 'ods', 'odp']:
         meta_file = tmp_folder / 'meta.xml'
-        argv = get_context(meta_file, RE_ODT_CTX)
+        argv = o.string(get_context_string(meta_file))
         context = o.context(argv)
         shutil.rmtree(tmp_folder)
         return context
@@ -58,7 +53,7 @@ def reader(o):
     # Microsoft Office Format
     elif file_type in ['docx', 'xlsx', 'pptx']:
         meta_file = tmp_folder / 'docProps' / 'core.xml'
-        argv = get_context(meta_file, RE_DOCX_CTX)
+        argv = o.string(get_context_string(meta_file))
         context = o.context(argv)
         shutil.rmtree(tmp_folder)
         return context
@@ -68,20 +63,17 @@ def reader(o):
         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
+            argv = o.string(meta.comments.decode('UTF-8'))
+            context = o.context(argv)
+            return context
     
 
 def writer(o):
-    string = ' '.join([x for x in o.collect(o.context) if x != None])
+    string = ' '.join(o.collect(o.context))
     tmp_folder = Path('unzip__' + o.file.stem)
     file_type = unzip_file(o.file, tmp_folder)
-    regex = re.compile(REGEX)
+    regex = re.compile(XML_REGEX)
     string = f"\\g
{string}\\g"
 
     # OpenOffice / LibreOffice / Open Document Format
diff --git a/orvani/plugins/pdf.py b/orvani/plugins/pdf.py
index 3972601..1b30884 100644
--- a/orvani/plugins/pdf.py
+++ b/orvani/plugins/pdf.py
@@ -18,7 +18,7 @@ def reader(o):
         return context
     
 def writer(o):
-    string = ' '.join([x for x in o.collect(o.context) if x != None])
+    string = ' '.join(o.collect(o.context))
     pdf = PdfReader(o.file)
     pdf.Info.Keywords = string
     PdfWriter(o.file, trailer=pdf).write()
diff --git a/test/.context.yaml b/test/.context.yaml
index 77d5e61..40f4da9 100644
--- a/test/.context.yaml
+++ b/test/.context.yaml
@@ -6,6 +6,12 @@ config:
       projects: album
       tags: genre
 items:
+  folder with spaces:
+    contexts: []
+    projects:
+    - totally
+    tags:
+    - spaces
   office.odt:
     contexts: []
     projects:
diff --git a/test/folder with spaces/file with spaces.txt b/test/folder with spaces/file with spaces.txt
new file mode 100644
index 0000000..56aacb0
--- /dev/null
+++ b/test/folder with spaces/file with spaces.txt	
@@ -0,0 +1,3 @@
+---
+#// #spaces
+---
diff --git a/test/office.odt b/test/office.odt
index 350910b..01eb5e9 100644
Binary files a/test/office.odt and b/test/office.odt differ