Commit 9f575385 authored by Gabriel Monnerat's avatar Gabriel Monnerat

refactor code to not pass the object mimemapper, serialize only a part of it

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@38566 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent d24c9441
...@@ -134,6 +134,20 @@ class OOHandler: ...@@ -134,6 +134,20 @@ class OOHandler:
return stdout, stderr return stdout, stderr
def _serializeMimemapper(self):
"""Serialize parts of mimemapper"""
filter_list = []
for extension in mimemapper.extension_list:
for service_type in mimemapper.document_service_list:
if service_type in mimemapper._doc_type_list_by_extension[extension]:
filter_list.append((extension,
service_type,
mimemapper.getFilterName(extension, service_type)))
return jsonpickle.encode(dict(doc_type_list_by_extension=mimemapper._doc_type_list_by_extension,
filter_list=filter_list,
mimetype_by_filter_type=mimemapper._mimetype_by_filter_type))
def convert(self, destination_format=None, **kw): def convert(self, destination_format=None, **kw):
"""Convert a document to another format supported by the OpenOffice """Convert a document to another format supported by the OpenOffice
...@@ -145,7 +159,7 @@ class OOHandler: ...@@ -145,7 +159,7 @@ class OOHandler:
kw['source_format'] = self.source_format kw['source_format'] = self.source_format
if destination_format: if destination_format:
kw['destination_format'] = destination_format kw['destination_format'] = destination_format
kw['mimemapper'] = jsonpickle.encode(mimemapper) kw['mimemapper'] = self._serializeMimemapper()
try: try:
stdout, stderr = self._callUnoConverter(*['convert'], **kw) stdout, stderr = self._callUnoConverter(*['convert'], **kw)
finally: finally:
...@@ -167,7 +181,7 @@ class OOHandler: ...@@ -167,7 +181,7 @@ class OOHandler:
openoffice.acquire() openoffice.acquire()
mimemapper_pickled = jsonpickle.encode(mimemapper) mimemapper_pickled = jsonpickle.encode(mimemapper)
logger.debug("getMetadata") logger.debug("getMetadata")
kw = dict(mimemapper=mimemapper_pickled) kw = dict(mimemapper=self._serializeMimemapper())
if base_document: if base_document:
feature_list = ['getmetadata', 'convert'] feature_list = ['getmetadata', 'convert']
else: else:
......
...@@ -147,15 +147,20 @@ class UnoConverter(object): ...@@ -147,15 +147,20 @@ class UnoConverter(object):
return [property,] return [property,]
def _getFilterName(self, destination_format, type):
for filter_tuple in mimemapper["filter_list"]:
if destination_format == filter_tuple[0] and filter_tuple[1] == type:
return filter_tuple[2]
def _getPropertyToExport(self, destination_format=None): def _getPropertyToExport(self, destination_format=None):
"""Create the property according to the extension of the file.""" """Create the property according to the extension of the file."""
if destination_format and self.document_loaded: if destination_format and self.document_loaded:
doc_type_list = mimemapper._doc_type_list_by_extension.get(destination_format) doc_type_list = mimemapper["doc_type_list_by_extension"].get(destination_format)
if self.document_type not in doc_type_list: if self.document_type not in doc_type_list:
raise AttributeError, \ raise AttributeError, \
"This Document can not be converted for this format" "This Document can not be converted for this format"
type = self.document_type type = self.document_type
filter_name = mimemapper.getFilterName(destination_format, type) filter_name = self._getFilterName(destination_format, type)
property_list = [] property_list = []
property = self._createProperty("Overwrite", True) property = self._createProperty("Overwrite", True)
property_list.append(property) property_list.append(property)
...@@ -230,7 +235,7 @@ class UnoConverter(object): ...@@ -230,7 +235,7 @@ class UnoConverter(object):
filter_name = type_detection.queryTypeByDescriptor(tuple(property_list), \ filter_name = type_detection.queryTypeByDescriptor(tuple(property_list), \
True)[0] True)[0]
doc.closeInput() doc.closeInput()
metadata['MIMEType'] = mimemapper.getMimetypeByFilterType(filter_name) metadata['MIMEType'] = mimemapper["mimetype_by_filter_type"].get(filter_name)
return metadata return metadata
def setMetadata(self, metadata): def setMetadata(self, metadata):
...@@ -304,8 +309,6 @@ def main(): ...@@ -304,8 +309,6 @@ def main():
metadata = jsonpickle.decode(arg) metadata = jsonpickle.decode(arg)
elif opt == '--mimemapper': elif opt == '--mimemapper':
mimemapper = jsonpickle.decode(arg) mimemapper = jsonpickle.decode(arg)
elif opt == '--unomimemapper_bin':
unomimemapper_bin = arg
kw = {} kw = {}
if "uno_path" in locals(): if "uno_path" in locals():
...@@ -314,11 +317,6 @@ def main(): ...@@ -314,11 +317,6 @@ def main():
if "office_binary_path" in locals(): if "office_binary_path" in locals():
kw['office_binary_path'] = office_binary_path kw['office_binary_path'] = office_binary_path
if "mimemapper" not in globals() and "--setmetadata" not in param_list:
from cloudooo.mimemapper import mimemapper
kw['python_path'] = sys.executable
mimemapper.loadFilterList(hostname=hostname, port=port, **kw)
if 'source_format' in locals(): if 'source_format' in locals():
kw['source_format'] = source_format kw['source_format'] = source_format
......
...@@ -52,6 +52,7 @@ class MimeMapper(object): ...@@ -52,6 +52,7 @@ class MimeMapper(object):
self._doc_type_list_by_extension = {} self._doc_type_list_by_extension = {}
# List of extensions that are ODF # List of extensions that are ODF
self._odf_extension_list = [] self._odf_extension_list = []
self.extension_list = []
self._mimetype_by_filter_type = {} self._mimetype_by_filter_type = {}
self._document_type_dict = {} self._document_type_dict = {}
...@@ -128,9 +129,9 @@ class MimeMapper(object): ...@@ -128,9 +129,9 @@ class MimeMapper(object):
filter_type_dict = type_dict.get(filter_type) filter_type_dict = type_dict.get(filter_type)
if not ui_name: if not ui_name:
ui_name = filter_type_dict.get("UIName") ui_name = filter_type_dict.get("UIName")
extension_list = filter_type_dict.get("Extensions") filter_extension_list = filter_type_dict.get("Extensions")
mimetype = filter_type_dict.get("MediaType") mimetype = filter_type_dict.get("MediaType")
if not (extension_list and mimetype): if not (filter_extension_list and mimetype):
continue continue
preferred = filter_type_dict.get("Preferred") preferred = filter_type_dict.get("Preferred")
document_service_str = value.get('DocumentService') document_service_str = value.get('DocumentService')
...@@ -153,9 +154,11 @@ class MimeMapper(object): ...@@ -153,9 +154,11 @@ class MimeMapper(object):
if not self._extension_list_by_type.has_key(document_service_str): if not self._extension_list_by_type.has_key(document_service_str):
self._extension_list_by_type[document_service_str] = [] self._extension_list_by_type[document_service_str] = []
for ext in iter(extension_list): for ext in iter(filter_extension_list):
# All mimetypes that starts with "application/vnd.oasis.opendocument" are # All mimetypes that starts with "application/vnd.oasis.opendocument" are
# ODF. # ODF.
if ext not in self.extension_list:
self.extension_list.append(ext)
if mimetype.startswith("application/vnd.oasis.opendocument"): if mimetype.startswith("application/vnd.oasis.opendocument"):
if not ext in self._odf_extension_list: if not ext in self._odf_extension_list:
self._odf_extension_list.append(ext) self._odf_extension_list.append(ext)
...@@ -178,6 +181,7 @@ class MimeMapper(object): ...@@ -178,6 +181,7 @@ class MimeMapper(object):
preferred=preferred, sort_index=sort_index, label=ui_name) preferred=preferred, sort_index=sort_index, label=ui_name)
# Adds the object in filter_by_extension_dict # Adds the object in filter_by_extension_dict
self._addFilter(filter) self._addFilter(filter)
self.document_service_list = self._extension_list_by_type.keys()
self._loaded = True self._loaded = True
def getMimetypeByFilterType(self, filter_type): def getMimetypeByFilterType(self, filter_type):
......
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