Commit a09d87af authored by Julien Muchembled's avatar Julien Muchembled

ooo: new conversion option to pass a script to edit the document before save

Because LibreOffice does not offer a safe langage (e.g. something
without the Shell funtion), let's go with Python and disable this
feature by default.
parent 706be9f6
Pipeline #24658 passed with stage
in 0 seconds
......@@ -53,6 +53,8 @@ class Handler(object):
"""
implements(IHandler)
enable_scripting = False
def __init__(self, base_folder_url, data, source_format, **kw):
"""Creates document in file system and loads it in OOo."""
self.zip = kw.get('zip', False)
......@@ -173,6 +175,8 @@ class Handler(object):
Keyword Arguments:
destination_format -- extension of document as String
"""
if not self.enable_scripting and kw.get('script'):
raise Exception("ooo: scripting is disabled")
logger.debug("OooConvert: %s > %s" % (self.source_format, destination_format))
kw['source_format'] = self.source_format
if destination_format:
......@@ -279,6 +283,9 @@ def bootstrapHandler(configuration_dict):
signal(SIGQUIT, stopProcesses)
signal(SIGHUP, stopProcesses)
Handler.enable_scripting = ('false', 'true').index(
configuration_dict.pop("ooo_enable_scripting", 'false'))
working_path = configuration_dict.get('working_path')
application_hostname = configuration_dict.get('application_hostname')
openoffice_port = int(configuration_dict.get('openoffice_port'))
......
......@@ -75,6 +75,9 @@ Options:
Dictionary with metadata
--infilter=FILTER_NAME[:FILTER_OPTIONS]
Import filter with options
--script=PYTHON_TEXT
Script to execute on the loaded document,
in Python langage
"""
......@@ -193,6 +196,15 @@ class UnoDocument(object):
module_manager = createInstance("com.sun.star.frame.ModuleManager")
self.document_type = module_manager.identify(uno_document)
def runScript(self, script):
exec(script, {
# Inspired from XSCRIPTCONTEXT API.
# XXX: Is it possible to get a real XSCRIPTCONTEXT object?
"ComponentContext": self.service_manager.DefaultContext,
#"Desktop": self.desktop, # is there any reasonable use?
"Document": self.document_loaded,
})
def convert(self):
"""it converts a document to specific format"""
for document_type, filter_name in self.filter_list:
......@@ -305,7 +317,7 @@ def main():
"hostname=", "port=", "source_format=",
"document_url=", "destination_format=",
"mimemapper=", "metadata=", "refresh=",
"unomimemapper_bin=", "infilter="])
"unomimemapper_bin=", "infilter=", "script="])
except GetoptError as msg:
msg = msg.msg + help_msg
sys.stderr.write(msg)
......@@ -317,7 +329,7 @@ def main():
import json
except ImportError:
import simplejson as json
metadata = mimemapper = None
metadata = mimemapper = script = None
hostname = port = office_binary_path = uno_path = None
document_url = destination_format = source_format = infilter = refresh = None
for opt, arg in iter(opt_list):
......@@ -346,11 +358,15 @@ def main():
mimemapper = json.loads(arg)
elif opt == '--infilter':
infilter = arg
elif opt == '--script':
script = arg
service_manager = helper_util.getServiceManager(
hostname, port, uno_path, office_binary_path)
unodocument = UnoDocument(service_manager, document_url,
source_format, destination_format, infilter, refresh)
if script:
unodocument.runScript(script)
if '--setmetadata' in param_list:
unodocument.setMetadata(metadata)
output = document_url
......
......@@ -54,6 +54,8 @@ mimetype_registry =
video/* * ffmpeg
* application/vnd.oasis.opendocument* ooo
ooo_enable_scripting = false
# This is used to disable ooo filters
#
# Below is an example list of spreadsheet filters by UI Name.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment