This commit is contained in:
randogoth 2023-06-10 16:56:04 +03:00
parent d76fac352f
commit d1b829f647
11 changed files with 94 additions and 41 deletions

View file

@ -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()

View file

@ -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)

View file

@ -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:

View file

@ -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]

View file

@ -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<pre><meta:keyword>)(?P<body>.*?)(?P<post><\/meta:keyword>)"
RE_DOCX_CTX = r"(?P<pre><cp:keywords>)(?P<body>.*?)(?P<post><\/cp:keywords>)"
REGEX = r"(?P<pre><dc:description>)(?P<body>.*?)(?P<post><\/dc:description>)"
XML_REGEX = r"(?P<pre><dc:description>)(?P<body>.*?)(?P<post><\/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<pre>{string}\\g<post>"
# OpenOffice / LibreOffice / Open Document Format

View file

@ -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()