diff --git a/orvani/plugins/office.py b/orvani/plugins/office.py index 469b0d6..81fdcfa 100644 --- a/orvani/plugins/office.py +++ b/orvani/plugins/office.py @@ -1,3 +1,4 @@ +import os import re import mimetypes import shutil @@ -6,8 +7,9 @@ from olefile import OleFileIO NAME = 'office' TYPES = ['.doc', '.xls', '.ppt', '.docx', '.xlsx', '.pptx', '.odt', '.ods', '.odp'] -RE_ODT_CTX = r"((.*))<\/meta:keyword>" -RE_ODT_NOTE = r"((.*))<\/dc:description>" +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>)"
 
 # Office Document processing inspired by
 # https://github.com/kappapiana/anonymize/blob/master/anonymize.py
@@ -28,6 +30,18 @@ def unzip_file(orig_file, tmp_dir):
     else:
         return type[0]
 
+def get_context(meta_file, re_ctx):
+    tags = note = []
+    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 != '']
+    
+
 def reader(o):
     tmp_folder = Path('unzip__' + o.file.stem)
     file_type = unzip_file(o.file, tmp_folder)
@@ -36,14 +50,7 @@ def reader(o):
     # 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 != '']
+        argv = get_context(meta_file, RE_ODT_CTX)
         context = o.context(argv)
         shutil.rmtree(tmp_folder)
         return context
@@ -51,14 +58,7 @@ def reader(o):
     # 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 != '']
+        argv = get_context(meta_file, RE_DOCX_CTX)
         context = o.context(argv)
         shutil.rmtree(tmp_folder)
         return context
@@ -74,4 +74,40 @@ def reader(o):
             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
+        return context
+    
+
+def writer(o):
+    string = ' '.join([x for x in o.collect(o.context) if x != None])
+    tmp_folder = Path('unzip__' + o.file.stem)
+    file_type = unzip_file(o.file, tmp_folder)
+    regex = re.compile(REGEX)
+    string = f"\\g
{string}\\g"
+
+    # OpenOffice / LibreOffice / Open Document Format
+    if file_type in ['odt', 'ods', 'odp']:
+        meta_file = tmp_folder / 'meta.xml'
+        with meta_file.open('r+') as file:
+            content = regex.sub(string, file.read())
+            file.seek(0, 0)
+            file.write(content)
+        shutil.make_archive(o.file, "zip", tmp_folder)
+        output_file_zip = str(o.file) + ".zip"
+        os.rename(output_file_zip, o.file)
+        shutil.rmtree(tmp_folder)
+
+    # Microsoft Office Format
+    elif file_type in ['docx', 'xlsx', 'pptx']:
+        meta_file = tmp_folder / 'docProps' / 'core.xml'
+        with meta_file.open('r+') as file:
+            content = regex.sub(string, file.read())
+            file.seek(0, 0)
+            file.write(content)
+        shutil.make_archive(o.file, "zip", tmp_folder)
+        output_file_zip = str(o.file) + ".zip"
+        os.rename(output_file_zip, o.file)
+        shutil.rmtree(tmp_folder) 
+
+    # Legacy Microsoft Office Format
+    elif file_type in ['application/msword', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint']:
+        print('unsupported file format')
diff --git a/test/.context.yaml b/test/.context.yaml
index 013efa8..77d5e61 100644
--- a/test/.context.yaml
+++ b/test/.context.yaml
@@ -6,6 +6,16 @@ config:
       projects: album
       tags: genre
 items:
+  office.odt:
+    contexts: []
+    projects:
+    - orvani
+    tags: []
+  sheet.xlsx:
+    contexts: []
+    projects:
+    - orvani
+    tags: []
   sub:
     contexts:
     - folder
diff --git a/test/office.odt b/test/office.odt
index f2a6e82..5f5748e 100644
Binary files a/test/office.odt and b/test/office.odt differ
diff --git a/test/sheet.xlsx b/test/sheet.xlsx
index 656c7d0..cfa167b 100644
Binary files a/test/sheet.xlsx and b/test/sheet.xlsx differ