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): ...@@ -53,6 +53,8 @@ class Handler(object):
""" """
implements(IHandler) implements(IHandler)
enable_scripting = False
def __init__(self, base_folder_url, data, source_format, **kw): def __init__(self, base_folder_url, data, source_format, **kw):
"""Creates document in file system and loads it in OOo.""" """Creates document in file system and loads it in OOo."""
self.zip = kw.get('zip', False) self.zip = kw.get('zip', False)
...@@ -173,6 +175,8 @@ class Handler(object): ...@@ -173,6 +175,8 @@ class Handler(object):
Keyword Arguments: Keyword Arguments:
destination_format -- extension of document as String 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)) logger.debug("OooConvert: %s > %s" % (self.source_format, destination_format))
kw['source_format'] = self.source_format kw['source_format'] = self.source_format
if destination_format: if destination_format:
...@@ -279,6 +283,9 @@ def bootstrapHandler(configuration_dict): ...@@ -279,6 +283,9 @@ def bootstrapHandler(configuration_dict):
signal(SIGQUIT, stopProcesses) signal(SIGQUIT, stopProcesses)
signal(SIGHUP, stopProcesses) signal(SIGHUP, stopProcesses)
Handler.enable_scripting = ('false', 'true').index(
configuration_dict.pop("ooo_enable_scripting", 'false'))
working_path = configuration_dict.get('working_path') working_path = configuration_dict.get('working_path')
application_hostname = configuration_dict.get('application_hostname') application_hostname = configuration_dict.get('application_hostname')
openoffice_port = int(configuration_dict.get('openoffice_port')) openoffice_port = int(configuration_dict.get('openoffice_port'))
......
...@@ -75,6 +75,9 @@ Options: ...@@ -75,6 +75,9 @@ Options:
Dictionary with metadata Dictionary with metadata
--infilter=FILTER_NAME[:FILTER_OPTIONS] --infilter=FILTER_NAME[:FILTER_OPTIONS]
Import filter with 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): ...@@ -193,6 +196,15 @@ class UnoDocument(object):
module_manager = createInstance("com.sun.star.frame.ModuleManager") module_manager = createInstance("com.sun.star.frame.ModuleManager")
self.document_type = module_manager.identify(uno_document) 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): def convert(self):
"""it converts a document to specific format""" """it converts a document to specific format"""
for document_type, filter_name in self.filter_list: for document_type, filter_name in self.filter_list:
...@@ -305,7 +317,7 @@ def main(): ...@@ -305,7 +317,7 @@ def main():
"hostname=", "port=", "source_format=", "hostname=", "port=", "source_format=",
"document_url=", "destination_format=", "document_url=", "destination_format=",
"mimemapper=", "metadata=", "refresh=", "mimemapper=", "metadata=", "refresh=",
"unomimemapper_bin=", "infilter="]) "unomimemapper_bin=", "infilter=", "script="])
except GetoptError as msg: except GetoptError as msg:
msg = msg.msg + help_msg msg = msg.msg + help_msg
sys.stderr.write(msg) sys.stderr.write(msg)
...@@ -317,7 +329,7 @@ def main(): ...@@ -317,7 +329,7 @@ def main():
import json import json
except ImportError: except ImportError:
import simplejson as json import simplejson as json
metadata = mimemapper = None metadata = mimemapper = script = None
hostname = port = office_binary_path = uno_path = None hostname = port = office_binary_path = uno_path = None
document_url = destination_format = source_format = infilter = refresh = None document_url = destination_format = source_format = infilter = refresh = None
for opt, arg in iter(opt_list): for opt, arg in iter(opt_list):
...@@ -346,11 +358,15 @@ def main(): ...@@ -346,11 +358,15 @@ def main():
mimemapper = json.loads(arg) mimemapper = json.loads(arg)
elif opt == '--infilter': elif opt == '--infilter':
infilter = arg infilter = arg
elif opt == '--script':
script = arg
service_manager = helper_util.getServiceManager( service_manager = helper_util.getServiceManager(
hostname, port, uno_path, office_binary_path) hostname, port, uno_path, office_binary_path)
unodocument = UnoDocument(service_manager, document_url, unodocument = UnoDocument(service_manager, document_url,
source_format, destination_format, infilter, refresh) source_format, destination_format, infilter, refresh)
if script:
unodocument.runScript(script)
if '--setmetadata' in param_list: if '--setmetadata' in param_list:
unodocument.setMetadata(metadata) unodocument.setMetadata(metadata)
output = document_url output = document_url
......
...@@ -54,6 +54,8 @@ mimetype_registry = ...@@ -54,6 +54,8 @@ mimetype_registry =
video/* * ffmpeg video/* * ffmpeg
* application/vnd.oasis.opendocument* ooo * application/vnd.oasis.opendocument* ooo
ooo_enable_scripting = false
# This is used to disable ooo filters # This is used to disable ooo filters
# #
# Below is an example list of spreadsheet filters by UI Name. # 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