Commit 706be9f6 authored by Julien Muchembled's avatar Julien Muchembled

ooo: new 'infilter' conversion option to to customize import

For example, it can be used to force the type of a column
when converting from a CSV document.
parent c49972df
...@@ -73,6 +73,8 @@ Options: ...@@ -73,6 +73,8 @@ Options:
created created
--metadata=DICT_SERIALIZED --metadata=DICT_SERIALIZED
Dictionary with metadata Dictionary with metadata
--infilter=FILTER_NAME[:FILTER_OPTIONS]
Import filter with options
""" """
...@@ -130,9 +132,15 @@ class UnoDocument(object): ...@@ -130,9 +132,15 @@ class UnoDocument(object):
return [property, ] return [property, ]
def _getPropertyToImport(self, def _getPropertyToImport(self, infilter,
_ods="com.sun.star.sheet.SpreadsheetDocument"): _ods="com.sun.star.sheet.SpreadsheetDocument"):
"""Create the property for import filter, according to the extension of the file.""" """Create the property for import filter, according to the extension of the file."""
if infilter:
infilter = infilter.split(':', 1)
args = [self._createProperty("FilterName", infilter.pop(0))]
if infilter:
args.append(self._createProperty("FilterOptions", *infilter))
return args
candidates = (x[0] for x in self.filter_list) candidates = (x[0] for x in self.filter_list)
if self.source_format == 'csv': if self.source_format == 'csv':
if _ods in candidates: if _ods in candidates:
...@@ -158,7 +166,7 @@ class UnoDocument(object): ...@@ -158,7 +166,7 @@ class UnoDocument(object):
return () return ()
def _load(self, refresh): def _load(self, infilter, refresh):
"""Create one document with basic properties """Create one document with basic properties
refresh argument tells to uno environment to refresh argument tells to uno environment to
replace dynamic properties of document before conversion replace dynamic properties of document before conversion
...@@ -170,7 +178,7 @@ class UnoDocument(object): ...@@ -170,7 +178,7 @@ class UnoDocument(object):
uno_url, uno_url,
"_blank", "_blank",
0, 0,
self._getPropertyToImport()) self._getPropertyToImport(infilter))
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 refresh: if refresh:
...@@ -297,7 +305,7 @@ def main(): ...@@ -297,7 +305,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="]) "unomimemapper_bin=", "infilter="])
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)
...@@ -311,7 +319,7 @@ def main(): ...@@ -311,7 +319,7 @@ def main():
import simplejson as json import simplejson as json
metadata = mimemapper = None metadata = mimemapper = None
hostname = port = office_binary_path = uno_path = None hostname = port = office_binary_path = uno_path = None
document_url = destination_format = source_format = 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):
if opt in ('-h', '--help'): if opt in ('-h', '--help'):
help() help()
...@@ -336,11 +344,13 @@ def main(): ...@@ -336,11 +344,13 @@ def main():
metadata = json.loads(arg) metadata = json.loads(arg)
elif opt == '--mimemapper': elif opt == '--mimemapper':
mimemapper = json.loads(arg) mimemapper = json.loads(arg)
elif opt == '--infilter':
infilter = 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, refresh) source_format, destination_format, infilter, refresh)
if '--setmetadata' in param_list: if '--setmetadata' in param_list:
unodocument.setMetadata(metadata) unodocument.setMetadata(metadata)
output = document_url output = document_url
...@@ -350,7 +360,7 @@ def main(): ...@@ -350,7 +360,7 @@ def main():
if output: if output:
# Instanciate new UnoDocument instance with new url # Instanciate new UnoDocument instance with new url
unodocument = UnoDocument(service_manager, output, unodocument = UnoDocument(service_manager, output,
destination_format or source_format, None, refresh) destination_format or source_format, None, None, refresh)
metadata_dict = unodocument.getMetadata() metadata_dict = unodocument.getMetadata()
if output: if output:
metadata_dict['document_url'] = output metadata_dict['document_url'] = output
......
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