Commit f53bbcb2 authored by Gabriel Monnerat's avatar Gabriel Monnerat

- refactor code to use pkg_resources

- move unoconverter.py and unomimemapper.py to helper folder


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@38552 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent efe445da
...@@ -26,8 +26,8 @@ ...@@ -26,8 +26,8 @@
# #
############################################################################## ##############################################################################
import jsonpickle import jsonpickle, pkg_resources
from os import environ from os import environ, path
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from cloudooo.application.openoffice import openoffice from cloudooo.application.openoffice import openoffice
from cloudooo.application.xvfb import xvfb from cloudooo.application.xvfb import xvfb
...@@ -58,7 +58,6 @@ class OOHandler: ...@@ -58,7 +58,6 @@ 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.unoconverter_bin = kw.get('unoconverter_bin', "unoconverter")
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")
...@@ -71,11 +70,11 @@ class OOHandler: ...@@ -71,11 +70,11 @@ class OOHandler:
kw['hostname'] = hostname kw['hostname'] = hostname
kw['port'] = port kw['port'] = port
command_list = [python_path command_list = [python_path
, "-c" , pkg_resources.resource_filename("cloudooo",
, "'from cloudooo.bin.unoconverter import main;main()'" path.join("helper", "unoconverter.py"))
, "--uno_path='%s'" % self.uno_path , "--uno_path='%s'" % self.uno_path
, "--office_binary_path='%s'" % self.office_binary_path , "--office_binary_path='%s'" % self.office_binary_path
, "--document_url='%s'" % self.document.getUrl()] , "--document_url='%s'" % self.document.getUrl()]
for arg in args: for arg in args:
command_list.insert(3, "'--%s'" % arg) command_list.insert(3, "'--%s'" % arg)
for k, v in kw.iteritems(): for k, v in kw.iteritems():
......
...@@ -30,12 +30,10 @@ ...@@ -30,12 +30,10 @@
import sys import sys
import jsonpickle import jsonpickle
from types import UnicodeType, InstanceType from types import UnicodeType, InstanceType
from os import environ from os import environ, putenv
from os.path import dirname from os.path import dirname, exists
from tempfile import mktemp from tempfile import mktemp
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
from cloudooo import ooolib
from cloudooo.utils import usage
__doc__ = """ __doc__ = """
...@@ -77,8 +75,78 @@ class UnoConverter(object): ...@@ -77,8 +75,78 @@ 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._setUpUnoEnvironment(kw.get("uno_path"),
kw.get("office_binary_path"))
self._load() self._load()
def _setUpUnoEnvironment(self, uno_path=None, office_binary_path=None):
"""Set up the environment to use the uno library and connect with the
openoffice by socket"""
if uno_path is not None:
environ['uno_path'] = uno_path
else:
uno_path = environ.get('uno_path')
if office_binary_path is not None:
environ['office_binary_path'] = office_binary_path
else:
office_binary_path = environ.get('office_binary_path')
# Add in sys.path the path of pyuno
if uno_path not in sys.path:
sys.path.append(uno_path)
fundamentalrc_file = '%s/fundamentalrc' % office_binary_path
if exists(fundamentalrc_file) and \
not environ.has_key('URE_BOOTSTRAP'):
putenv('URE_BOOTSTRAP','vnd.sun.star.pathname:%s' % fundamentalrc_file)
def _createProperty(self, name, value):
"""Create property"""
from com.sun.star.beans import PropertyValue
property = PropertyValue()
property.Name = name
property.Value = value
return property
def _getServiceManager(self, host, port):
"""Get the ServiceManager from the running OpenOffice.org."""
import uno
# Get the uno component context from the PyUNO runtime
uno_context = uno.getComponentContext()
# Create the UnoUrlResolver on the Python side.
url_resolver = "com.sun.star.bridge.UnoUrlResolver"
resolver = uno_context.ServiceManager.createInstanceWithContext(url_resolver,
uno_context)
# Connect to the running OpenOffice.org and get its
# context.
uno_connection = resolver.resolve("uno:socket,host=%s,port=%s;urp;StarOffice.ComponentContext" % (host, port))
# Get the ServiceManager object
return uno_connection.ServiceManager
def _createSpecificProperty(self, filter_name):
"""Creates a property according to the filter"""
import uno
from com.sun.star.beans import PropertyValue
if filter_name == "impress_html_Export":
property = PropertyValue('FilterData', 0,
uno.Any('[]com.sun.star.beans.PropertyValue',
(PropertyValue('IsExportNotes', 0, True, 0),
PropertyValue('Format', 0, 2, 0),),), 0)
elif filter_name == "impress_pdf_Export":
property = PropertyValue('FilterData', 0,
uno.Any('[]com.sun.star.beans.PropertyValue',
(PropertyValue('ExportNotesPages', 0, True, 0),),), 0)
elif filter_name in ("draw_html_Export", "HTML (StarCalc)"):
property = PropertyValue('FilterData', 0,
uno.Any('[]com.sun.star.beans.PropertyValue',
(PropertyValue('Format', 0, 2, 0),),), 0)
elif filter_name == "Text (encoded)":
property = PropertyValue('FilterFlags', 0, 'UTF8,LF', 0)
else:
return []
return [property,]
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:
...@@ -89,27 +157,32 @@ class UnoConverter(object): ...@@ -89,27 +157,32 @@ class UnoConverter(object):
type = self.document_type type = self.document_type
filter_name = mimemapper.getFilterName(destination_format, type) filter_name = mimemapper.getFilterName(destination_format, type)
property_list = [] property_list = []
property = ooolib.createProperty("Overwrite", True) property = self._createProperty("Overwrite", True)
property_list.append(property) property_list.append(property)
property = ooolib.createProperty("FilterName", filter_name) property = self._createProperty("FilterName", filter_name)
property_list.append(property) property_list.append(property)
property_list.extend(ooolib.createSpecificProperty(filter_name)) property_list.extend(self._createSpecificProperty(filter_name))
return property_list return property_list
else: else:
return () return ()
def _load(self): def _load(self):
"""Create one document with basic properties""" """Create one document with basic properties"""
service_manager = ooolib.getServiceManager(self.hostname, self.port) service_manager = self._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 = ooolib.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") 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"
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
def systemPathToFileUrl(self, path):
"""Returns a path in uno library patterns"""
from unohelper import systemPathToFileUrl
return systemPathToFileUrl(path)
def convert(self, output_format=None): def convert(self, output_format=None):
"""it converts a document to specific format""" """it converts a document to specific format"""
if output_format in ("html", "htm", "xhtml"): if output_format in ("html", "htm", "xhtml"):
...@@ -121,7 +194,7 @@ class UnoConverter(object): ...@@ -121,7 +194,7 @@ class UnoConverter(object):
property_list = self._getPropertyToExport(output_format) property_list = self._getPropertyToExport(output_format)
try: try:
self.document_loaded.storeToURL(ooolib.systemPathToFileUrl(output_url), self.document_loaded.storeToURL(self.systemPathToFileUrl(output_url),
tuple(property_list)) tuple(property_list))
finally: finally:
self.document_loaded.dispose() self.document_loaded.dispose()
...@@ -147,11 +220,11 @@ class UnoConverter(object): ...@@ -147,11 +220,11 @@ class UnoConverter(object):
if field_value_str: if field_value_str:
fieldname = document_info.getUserFieldName(number) fieldname = document_info.getUserFieldName(number)
metadata[fieldname] = field_value_str metadata[fieldname] = field_value_str
service_manager = ooolib.getServiceManager(self.hostname, self.port) service_manager = self._getServiceManager(self.hostname, self.port)
uno_file_access = service_manager.createInstance("com.sun.star.ucb.SimpleFileAccess") uno_file_access = service_manager.createInstance("com.sun.star.ucb.SimpleFileAccess")
doc = uno_file_access.openFileRead(ooolib.systemPathToFileUrl(self.document_url)) doc = uno_file_access.openFileRead(self.systemPathToFileUrl(self.document_url))
property_list = [] property_list = []
property = ooolib.createProperty("InputStream", doc) property = self._createProperty("InputStream", doc)
property_list.append(property) property_list.append(property)
type_detection = service_manager.createInstance("com.sun.star.document.TypeDetection") type_detection = service_manager.createInstance("com.sun.star.document.TypeDetection")
filter_name = type_detection.queryTypeByDescriptor(tuple(property_list), \ filter_name = type_detection.queryTypeByDescriptor(tuple(property_list), \
...@@ -186,7 +259,7 @@ class UnoConverter(object): ...@@ -186,7 +259,7 @@ class UnoConverter(object):
self.document_loaded.dispose() self.document_loaded.dispose()
def help(): def help():
usage(sys.stderr, __doc__) print >> sys.stderr, __doc__
sys.exit(1) sys.exit(1)
def main(): def main():
...@@ -203,7 +276,7 @@ def main(): ...@@ -203,7 +276,7 @@ def main():
"unomimemapper_bin="]) "unomimemapper_bin="])
except GetoptError, msg: except GetoptError, msg:
msg = msg.msg + help_msg msg = msg.msg + help_msg
usage(sys.stderr, msg) print >> sys.stderr, msg
sys.exit(2) sys.exit(2)
param_list = [tuple[0] for tuple in opt_list] param_list = [tuple[0] for tuple in opt_list]
...@@ -235,22 +308,17 @@ def main(): ...@@ -235,22 +308,17 @@ def main():
unomimemapper_bin = arg unomimemapper_bin = arg
kw = {} kw = {}
if "uno_path" in locals():
kw['uno_path'] = uno_path
if "office_binary_path" in locals():
kw['office_binary_path'] = office_binary_path
if "mimemapper" not in globals() and "--setmetadata" not in param_list: if "mimemapper" not in globals() and "--setmetadata" not in param_list:
from cloudooo.mimemapper import mimemapper from cloudooo.mimemapper import mimemapper
if "uno_path" in locals():
kw['uno_path'] = uno_path
if "office_binary_path" in locals():
kw['office_binary_path'] = office_binary_path
if "unomimemapper_bin" in locals():
kw['unomimemapper_bin'] = unomimemapper_bin
kw['python_path'] = sys.executable kw['python_path'] = sys.executable
mimemapper.loadFilterList(hostname=hostname, port=port, **kw) mimemapper.loadFilterList(hostname=hostname, port=port, **kw)
kw.clear()
if 'source_format' in locals(): if 'source_format' in locals():
kw['source_format'] = source_format kw['source_format'] = source_format
......
...@@ -28,10 +28,8 @@ ...@@ -28,10 +28,8 @@
############################################################################## ##############################################################################
import sys import sys
from os import environ from os import environ, path, putenv
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
from cloudooo.ooolib import getServiceManager
from cloudooo.utils import usage
from types import InstanceType from types import InstanceType
__doc__ = """ __doc__ = """
...@@ -53,9 +51,11 @@ Options: ...@@ -53,9 +51,11 @@ Options:
class UnoMimemapper(object): class UnoMimemapper(object):
""" """ """ """
def __init__(self, hostname, port): def __init__(self, hostname, port, **kw):
""" Receives hostname and port from openoffice and create a service manager""" """ Receives hostname and port from openoffice and create a service manager"""
self.service_manager = getServiceManager(hostname, port) self._setUpUnoEnvironment(kw.get("uno_path"),
kw.get("office_binary_path"))
self.service_manager = self._getServiceManager(hostname, port)
def _getElementNameByService(self, uno_service, ignore_name_list=[]): def _getElementNameByService(self, uno_service, ignore_name_list=[]):
"""Returns an dict with elements.""" """Returns an dict with elements."""
...@@ -74,6 +74,42 @@ class UnoMimemapper(object): ...@@ -74,6 +74,42 @@ class UnoMimemapper(object):
return service_dict return service_dict
def _setUpUnoEnvironment(self, uno_path=None, office_binary_path=None):
"""Set up the environment to use the uno library and connect with the
openoffice by socket"""
if uno_path is not None:
environ['uno_path'] = uno_path
else:
uno_path = environ.get('uno_path')
if office_binary_path is not None:
environ['office_binary_path'] = office_binary_path
else:
office_binary_path = environ.get('office_binary_path')
# Add in sys.path the path of pyuno
if uno_path not in sys.path:
sys.path.append(uno_path)
fundamentalrc_file = '%s/fundamentalrc' % office_binary_path
if path.exists(fundamentalrc_file) and \
not environ.has_key('URE_BOOTSTRAP'):
putenv('URE_BOOTSTRAP','vnd.sun.star.pathname:%s' % fundamentalrc_file)
def _getServiceManager(self, host, port):
"""Get the ServiceManager from the running OpenOffice.org."""
import uno
# Get the uno component context from the PyUNO runtime
uno_context = uno.getComponentContext()
# Create the UnoUrlResolver on the Python side.
url_resolver = "com.sun.star.bridge.UnoUrlResolver"
resolver = uno_context.ServiceManager.createInstanceWithContext(url_resolver,
uno_context)
# Connect to the running OpenOffice.org and get its
# context.
uno_connection = resolver.resolve("uno:socket,host=%s,port=%s;urp;StarOffice.ComponentContext" % (host, port))
# Get the ServiceManager object
return uno_connection.ServiceManager
def getFilterDict(self): def getFilterDict(self):
"""Return all filters and your properties""" """Return all filters and your properties"""
filter_service = self.service_manager.createInstance("com.sun.star.document.FilterFactory") filter_service = self.service_manager.createInstance("com.sun.star.document.FilterFactory")
...@@ -86,9 +122,8 @@ class UnoMimemapper(object): ...@@ -86,9 +122,8 @@ class UnoMimemapper(object):
type_dict = self._getElementNameByService(type_service, ["UINames", "URLPattern"]) type_dict = self._getElementNameByService(type_service, ["UINames", "URLPattern"])
return type_dict return type_dict
def help(): def help():
usage(sys.stderr, __doc__) print >> sys.stderr, __doc__
sys.exit(1) sys.exit(1)
def main(): def main():
...@@ -98,12 +133,12 @@ def main(): ...@@ -98,12 +133,12 @@ def main():
"hostname=", "port="]) "hostname=", "port="])
except GetoptError, msg: except GetoptError, msg:
msg = msg.msg + "\nUse --help or -h" msg = msg.msg + "\nUse --help or -h"
usage(sys.stderr, msg) print >> sys.stderr, msg
sys.exit(2) sys.exit(2)
if not opt_list: if not opt_list:
help() help()
for opt, arg in opt_list: for opt, arg in opt_list:
if opt in ("-h", "--help"): if opt in ("-h", "--help"):
help() help()
...@@ -116,7 +151,7 @@ def main(): ...@@ -116,7 +151,7 @@ def main():
elif opt == "--port": elif opt == "--port":
port = arg port = arg
mimemapper = UnoMimemapper(hostname, port) mimemapper = UnoMimemapper(hostname, port, **environ)
filter_dict = mimemapper.getFilterDict() filter_dict = mimemapper.getFilterDict()
type_dict = mimemapper.getTypeDict() type_dict = mimemapper.getTypeDict()
print "filter_dict = %s\ntype_dict = %s" % (filter_dict, type_dict) print "filter_dict = %s\ntype_dict = %s" % (filter_dict, type_dict)
......
...@@ -26,11 +26,12 @@ ...@@ -26,11 +26,12 @@
# #
############################################################################## ##############################################################################
import pkg_resources
from re import findall from re import findall
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from zope.interface import implements from zope.interface import implements
from filter import Filter from filter import Filter
from os import environ from os import environ, path
from sys import executable as python_path from sys import executable as python_path
from interfaces.mimemapper import IMimemapper from interfaces.mimemapper import IMimemapper
from types import InstanceType from types import InstanceType
...@@ -103,13 +104,15 @@ class MimeMapper(object): ...@@ -103,13 +104,15 @@ class MimeMapper(object):
# XXX - try find a good way to remove filters that are not used for export # XXX - try find a good way to remove filters that are not used for export
bad_flag_list = [65, 94217, 536641, 1572929, 268959937, 524373, 85, 524353] bad_flag_list = [65, 94217, 536641, 1572929, 268959937, 524373, 85, 524353]
uno_path = kw.get("uno_path", environ.get('uno_path')) uno_path = kw.get("uno_path", environ.get('uno_path'))
office_binary_path = kw.get("office_binary_path", environ.get('office_binary_path')) office_binary_path = kw.get("office_binary_path",
environ.get('office_binary_path'))
command = [python_path command = [python_path
, "'-c'" , pkg_resources.resource_filename(__name__,
, "'from cloudooo.bin.unomimemapper import main;main()'" path.join("helper","unomimemapper.py"))
, "'--uno_path=%s'" % uno_path , "'--uno_path=%s'" % uno_path
, "'--office_binary_path=%s'" % office_binary_path , "'--office_binary_path=%s'" % office_binary_path
, "'--hostname=%s'" % hostname, "--port=%s" % port] , "'--hostname=%s'" % hostname
, "--port=%s" % port]
stdout, stderr = Popen(' '.join(command), stdout, stderr = Popen(' '.join(command),
stdout=PIPE, stdout=PIPE,
close_fds=True, close_fds=True,
......
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