office meta plugin

This commit is contained in:
randogoth 2023-06-09 01:23:14 +03:00
parent 2c239bc07e
commit 0e7862ee4a
11 changed files with 236 additions and 27 deletions

150
.index.yaml Normal file
View file

@ -0,0 +1,150 @@
contexts:
Chripo McFlu:
- ./test/chirpy.wav
'calc #tessssting':
- ./test/sheet.xlsx
- ./test/table.ods
'concon #tagz':
- ./test/word.docx
erfwegf:
- ./COMMANDS.md
frrreee:
- ./README.md
python:
- ./orvani/plugins/hashdoubleslash.py
- ./TODO.md
test:
- ./test/test.txt
this:
- ./test/office.odt
watch:
- ./test/chirpy.wav
files:
./COMMANDS.md:
contexts:
- erfwegf
note: This is awesome
projects:
- coding
tags:
- super
- great
./README.md:
contexts:
- frrreee
projects:
- test2
- test
./TODO.md:
contexts:
- python
note: Chawera
projects:
- bokertov
tags:
- frrr
- rusty
./orvani/plugins/hashdoubleslash.py:
contexts:
- python
note: It even works in Python code
projects:
- orvani
tags:
- example
./test/chirpy.wav:
contexts:
- watch
- Chripo McFlu
note:
- This is a title
projects:
- parakeetio
- sensorwatch
tags:
- funk
./test/office.odt:
contexts:
- this
note: and a note
projects:
- oopsie
tags:
- whoa
- that
./test/sheet.xlsx:
contexts:
- 'calc #tessssting'
projects: []
tags: []
./test/table.ods:
contexts:
- 'calc #tessssting'
projects: []
tags: []
./test/test.txt:
contexts:
- test
note: frrr
projects:
- test
tags:
- faa
- test
./test/word.docx:
contexts:
- 'concon #tagz'
projects: []
tags: []
note:
Chawera:
- ./TODO.md
It even works in Python code:
- ./orvani/plugins/hashdoubleslash.py
This is a title:
- ./test/chirpy.wav
This is awesome:
- ./COMMANDS.md
and a note:
- ./test/office.odt
frrr:
- ./test/test.txt
projects:
bokertov:
- ./TODO.md
coding:
- ./COMMANDS.md
oopsie:
- ./test/office.odt
orvani:
- ./orvani/plugins/hashdoubleslash.py
parakeetio:
- ./test/chirpy.wav
sensorwatch:
- ./test/chirpy.wav
test:
- ./test/test.txt
- ./README.md
test2:
- ./README.md
tags:
example:
- ./orvani/plugins/hashdoubleslash.py
faa:
- ./test/test.txt
frrr:
- ./TODO.md
funk:
- ./test/chirpy.wav
great:
- ./COMMANDS.md
rusty:
- ./TODO.md
super:
- ./COMMANDS.md
test:
- ./test/test.txt
that:
- ./test/office.odt
whoa:
- ./test/office.odt

View file

@ -1,26 +0,0 @@
import taglib
with taglib.File("/home/flux/Documents/230605-191750.wav", save_on_exit=True) as song:
import frontmatter
with open('README.md', 'r') as data:
y = frontmatter.load(data)
print(y.metadata)
@orvani.metareader
def yaml_read(conf, db):
for (root,dirs,files) in os.walk(path):
if files:
for file in files:
rpath = os.path.join(root, dir)
f, ext = os.path.splitext(file)
if ext[1:] in conf['index'] and ext[1:] in ['md', 'txt']:
with open(file, 'r') as data:
y = frontmatter.load(data)
print(y)
if y.metadata:
for meta in y.metadata:
db[file][meta] = y.metadata[meta]
return db

View file

@ -5,7 +5,7 @@ from pathlib import Path
from rich.console import Console
from rich.table import Table
from orvani import OrvaniTag, CONTEXT_FILE, CACHE_FILE, CONTEXTS
from plugins import frontmatter, hashdoubleslash, id3
from plugins import frontmatter, hashdoubleslash, id3, office
def mktable(query: str, index: dict):
console = Console()
@ -89,6 +89,7 @@ def main():
frontmatter.reader(o)
hashdoubleslash.reader(o)
id3.reader(o)
office.reader(o)
# orvani.metadata()

82
orvani/plugins/office.py Normal file
View file

@ -0,0 +1,82 @@
import re
import mimetypes
import shutil
from pathlib import Path
from olefile import OleFileIO
NAME = 'office'
TYPES = ['.doc', '.xls', '.ppt', '.docx', '.xlsx', '.pptx', '.odt', '.ods', '.odp']
RE_ODT_CTX = r"<meta:keyword>((.*))<\/meta:keyword>"
RE_ODT_NOTE = r"<dc:description>((.*))<\/dc:description>"
# Office Document processing inspired by
# https://github.com/kappapiana/anonymize/blob/master/anonymize.py
def unzip_file(orig_file, tmp_dir):
type = mimetypes.guess_type(orig_file)
filetype = {
"application/vnd.oasis.opendocument.text" : "odt",
"application/vnd.oasis.opendocument.spreadsheet" : "ods",
"application/vnd.oasis.opendocument.presentation" : "odp",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "docx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : "xlsx",
"application/vnd.openxmlformats-officedocument.presentationml.presentation" : "pptx",
}
if type[0] in filetype:
shutil.unpack_archive(orig_file, tmp_dir, 'zip')
return filetype[type[0]]
else:
return type[0]
def reader(o):
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 = []
# 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)
# 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)
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)

View file

@ -1,2 +1,4 @@
olefile==0.46
pytaglib==2.0.0
PyYAML==6.0
rich==13.4.1

BIN
test/office.odt Normal file

Binary file not shown.

BIN
test/oldword.doc Normal file

Binary file not shown.

BIN
test/sheet.xlsx Normal file

Binary file not shown.

BIN
test/table.ods Normal file

Binary file not shown.

BIN
test/tableold.xls Normal file

Binary file not shown.

BIN
test/word.docx Normal file

Binary file not shown.