Commit 16bab6ca authored by Nicolas Delaby's avatar Nicolas Delaby

Add new option to convertFile: refresh

This new option is able to recompute dynamics properties inside OOo documents
Like cells, variables, fields, charts. (disable by default)

This is useful for hacked documents.



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@40595 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent ef9e1ca2
...@@ -59,6 +59,7 @@ class OOHandler: ...@@ -59,6 +59,7 @@ class OOHandler:
self.uno_path = kw.get("uno_path", None) self.uno_path = kw.get("uno_path", None)
self.office_binary_path = kw.get("office_binary_path", None) self.office_binary_path = kw.get("office_binary_path", None)
self.timeout = kw.get("timeout", 600) self.timeout = kw.get("timeout", 600)
self.refresh = kw.get('refresh', False)
self.source_format = source_format self.source_format = source_format
if not self.uno_path: if not self.uno_path:
self.uno_path = environ.get("uno_path") self.uno_path = environ.get("uno_path")
...@@ -164,6 +165,7 @@ class OOHandler: ...@@ -164,6 +165,7 @@ class OOHandler:
if destination_format: if destination_format:
kw['destination_format'] = destination_format kw['destination_format'] = destination_format
kw['mimemapper'] = self._serializeMimemapper() kw['mimemapper'] = self._serializeMimemapper()
kw['refresh'] = jsonpickle.encode(self.refresh)
try: try:
stdout, stderr = self._callUnoConverter(*['convert'], **kw) stdout, stderr = self._callUnoConverter(*['convert'], **kw)
finally: finally:
......
...@@ -77,6 +77,7 @@ class UnoConverter(object): ...@@ -77,6 +77,7 @@ class UnoConverter(object):
self.document_url = document_url self.document_url = document_url
self.document_dir_path = dirname(document_url) self.document_dir_path = dirname(document_url)
self.source_format = kw.get('source_format') self.source_format = kw.get('source_format')
self.refresh = kw.get('refresh')
self._setUpUnoEnvironment(kw.get("uno_path"), self._setUpUnoEnvironment(kw.get("uno_path"),
kw.get("office_binary_path")) kw.get("office_binary_path"))
self._load() self._load()
...@@ -159,14 +160,25 @@ class UnoConverter(object): ...@@ -159,14 +160,25 @@ class UnoConverter(object):
return () return ()
def _load(self): def _load(self):
"""Create one document with basic properties""" """Create one document with basic properties
refresh argument tells to uno environment to
replace dynamic properties of document before conversion
"""
service_manager = helper_utils.getServiceManager(self.hostname, self.port) service_manager = helper_utils.getServiceManager(self.hostname, self.port)
desktop = service_manager.createInstance("com.sun.star.frame.Desktop") desktop = service_manager.createInstance("com.sun.star.frame.Desktop")
uno_url = self.systemPathToFileUrl(self.document_url) uno_url = self.systemPathToFileUrl(self.document_url)
uno_document = desktop.loadComponentFromURL(uno_url, "_blank", 0, ()) uno_document = desktop.loadComponentFromURL(uno_url, "_blank", 0, ())
module_manager = service_manager.createInstance("com.sun.star.frame.ModuleManager")
if not uno_document: if not uno_document:
raise AttributeError, "This document can not be loaded or is empty" raise AttributeError, "This document can not be loaded or is empty"
if self.refresh:
# Before converting to expected format, refresh dynamic
# value inside document.
dispatcher = service_manager.createInstance("com.sun.star.frame.DispatchHelper")
for uno_command in ('UpdateFields', 'UpdateAll', 'UpdateInputFields',
'UpdateAllLinks', 'UpdateCharts',):
dispatcher.executeDispatch(uno_document.getCurrentController().getFrame(),
'.uno:%s' % uno_command, '', 0, ())
module_manager = service_manager.createInstance("com.sun.star.frame.ModuleManager")
self.document_type = module_manager.identify(uno_document) self.document_type = module_manager.identify(uno_document)
self.document_loaded = uno_document self.document_loaded = uno_document
...@@ -266,7 +278,7 @@ def main(): ...@@ -266,7 +278,7 @@ def main():
"uno_path=", "office_binary_path=", "uno_path=", "office_binary_path=",
"hostname=", "port=", "source_format=", "hostname=", "port=", "source_format=",
"document_url=", "destination_format=", "document_url=", "destination_format=",
"mimemapper=", "metadata=", "mimemapper=", "metadata=", "refresh=",
"unomimemapper_bin=", "jsonpickle_path="]) "unomimemapper_bin=", "jsonpickle_path="])
except GetoptError, msg: except GetoptError, msg:
msg = msg.msg + help_msg msg = msg.msg + help_msg
...@@ -281,7 +293,7 @@ def main(): ...@@ -281,7 +293,7 @@ def main():
break break
import jsonpickle import jsonpickle
refresh = None
for opt, arg in iter(opt_list): for opt, arg in iter(opt_list):
if opt in ('-h', '--help'): if opt in ('-h', '--help'):
help() help()
...@@ -301,6 +313,8 @@ def main(): ...@@ -301,6 +313,8 @@ def main():
destination_format = arg destination_format = arg
elif opt == '--source_format': elif opt == '--source_format':
source_format = arg source_format = arg
elif opt == '--refresh':
refresh = jsonpickle.decode(arg)
elif opt == '--metadata': elif opt == '--metadata':
arg = decodestring(arg) arg = decodestring(arg)
metadata = jsonpickle.decode(arg) metadata = jsonpickle.decode(arg)
...@@ -316,6 +330,8 @@ def main(): ...@@ -316,6 +330,8 @@ def main():
if 'source_format' in locals(): if 'source_format' in locals():
kw['source_format'] = source_format kw['source_format'] = source_format
if refresh:
kw['refresh'] = refresh
unoconverter = UnoConverter(hostname, port, document_url, **kw) unoconverter = UnoConverter(hostname, port, document_url, **kw)
if "--convert" in param_list and not '--getmetadata' in param_list \ if "--convert" in param_list and not '--getmetadata' in param_list \
......
...@@ -32,7 +32,7 @@ from zope.interface import Interface ...@@ -32,7 +32,7 @@ from zope.interface import Interface
class IManager(Interface): class IManager(Interface):
"""Provides method to manipulate documents and metadatas using OOo""" """Provides method to manipulate documents and metadatas using OOo"""
def convertFile(file, source_format, destination_format, zip): def convertFile(file, source_format, destination_format, zip, refresh):
"""Returns the converted file in the given format. """Returns the converted file in the given format.
zip parameter can be specified to return the result of conversion zip parameter can be specified to return the result of conversion
......
...@@ -45,7 +45,8 @@ class Manager(object): ...@@ -45,7 +45,8 @@ class Manager(object):
self._path_tmp_dir = path_tmp_dir self._path_tmp_dir = path_tmp_dir
self.kw = kw self.kw = kw
def convertFile(self, file, source_format, destination_format, zip=False): def convertFile(self, file, source_format, destination_format, zip=False,
refresh=False):
"""Returns the converted file in the given format. """Returns the converted file in the given format.
Keywords arguments: Keywords arguments:
file -- File as string in base64 file -- File as string in base64
...@@ -57,6 +58,7 @@ class Manager(object): ...@@ -57,6 +58,7 @@ class Manager(object):
if not mimemapper.getFilterList(destination_format): if not mimemapper.getFilterList(destination_format):
raise ValueError, "This format is not supported or is invalid" raise ValueError, "This format is not supported or is invalid"
self.kw['zip'] = zip self.kw['zip'] = zip
self.kw['refresh'] = refresh
document = OOHandler(self._path_tmp_dir, document = OOHandler(self._path_tmp_dir,
decodestring(file), decodestring(file),
source_format, source_format,
......
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