diff --git a/.index.yaml b/.index.yaml new file mode 100644 index 0000000..d011ded --- /dev/null +++ b/.index.yaml @@ -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 diff --git a/metareaders.py b/metareaders.py deleted file mode 100644 index c41f4d6..0000000 --- a/metareaders.py +++ /dev/null @@ -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 \ No newline at end of file diff --git a/orvani/cmd.py b/orvani/cmd.py index 0586449..fd77b7d 100644 --- a/orvani/cmd.py +++ b/orvani/cmd.py @@ -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() diff --git a/orvani/plugins/office.py b/orvani/plugins/office.py new file mode 100644 index 0000000..302b20e --- /dev/null +++ b/orvani/plugins/office.py @@ -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>" +RE_ODT_NOTE = r"((.*))<\/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) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index cfd6667..f29c13c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ +olefile==0.46 +pytaglib==2.0.0 PyYAML==6.0 rich==13.4.1 diff --git a/test/office.odt b/test/office.odt new file mode 100644 index 0000000..f2a6e82 Binary files /dev/null and b/test/office.odt differ diff --git a/test/oldword.doc b/test/oldword.doc new file mode 100644 index 0000000..17fc207 Binary files /dev/null and b/test/oldword.doc differ diff --git a/test/sheet.xlsx b/test/sheet.xlsx new file mode 100644 index 0000000..656c7d0 Binary files /dev/null and b/test/sheet.xlsx differ diff --git a/test/table.ods b/test/table.ods new file mode 100644 index 0000000..bee9848 Binary files /dev/null and b/test/table.ods differ diff --git a/test/tableold.xls b/test/tableold.xls new file mode 100644 index 0000000..65cd308 Binary files /dev/null and b/test/tableold.xls differ diff --git a/test/word.docx b/test/word.docx new file mode 100644 index 0000000..e22a651 Binary files /dev/null and b/test/word.docx differ