Commit 115e6f1b authored by Nicolas Delaby's avatar Nicolas Delaby

factorise _setUpUnoEnvironment function

parent 5e41f4c1
def getServiceManager(host, port):
"""Get the ServiceManager from the running OpenOffice.org."""
import sys
import os
def getServiceManager(host, port, uno_path, office_binary_path):
"""Get the ServiceManager from the running OpenOffice.org.
"""
# 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 os.path.exists(fundamentalrc_file) and \
'URE_BOOTSTRAP' not in os.environ:
os.putenv('URE_BOOTSTRAP', 'vnd.sun.star.pathname:%s' % fundamentalrc_file)
import uno
# Get the uno component context from the PyUNO runtime
uno_context = uno.getComponentContext()
......
......@@ -17,20 +17,24 @@ def test_openoffice(hostname, port):
def main():
try:
opt_list, arg_list = getopt(sys.argv[1:], "",
["port=", "hostname=", "uno_path="])
["port=", "hostname=", "uno_path=",
"office_binary_path="])
except GetoptError, e:
print >> sys.stderr, "%s \nUse --port and --hostname" % e
sys.exit(2)
port = hostname = uno_path = office_binary_path = None
for opt, arg in opt_list:
if opt == "--port":
port = arg
elif opt == "--hostname":
hostname = arg
elif opt == "--uno_path":
environ["uno_path"] = arg
uno_path = arg
elif opt == "--office_binary_path":
office_binary_path = arg
print test_openoffice(hostname, port)
print test_openoffice(hostname, port, uno_path, office_binary_path)
if __name__ == "__main__":
......
......@@ -70,39 +70,19 @@ Options:
class UnoConverter(object):
"""A module to easily work with OpenOffice.org."""
def __init__(self, hostname, port, document_url, **kw):
def __init__(self, hostname, port, document_url, source_format, uno_path,
office_binary_path, refresh=None,):
""" """
self.hostname = hostname
self.port = port
self.document_url = document_url
self.document_dir_path = dirname(document_url)
self.source_format = kw.get('source_format')
self.refresh = kw.get('refresh')
self._setUpUnoEnvironment(kw.get("uno_path"),
kw.get("office_binary_path"))
self.source_format = source_format
self.refresh = refresh
self.uno_path = uno_path
self.office_binary_path = office_binary_path
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 \
'URE_BOOTSTRAP' not in environ:
putenv('URE_BOOTSTRAP', 'vnd.sun.star.pathname:%s' % fundamentalrc_file)
def _createProperty(self, name, value):
"""Create property"""
from com.sun.star.beans import PropertyValue
......@@ -159,7 +139,9 @@ class UnoConverter(object):
refresh argument tells to uno environment to
replace dynamic properties of document before conversion
"""
service_manager = helper_util.getServiceManager(self.hostname, self.port)
service_manager = helper_util.getServiceManager(self.hostname, self.port,
self.uno_path,
self.office_binary_path)
desktop = service_manager.createInstance("com.sun.star.frame.Desktop")
uno_url = self.systemPathToFileUrl(self.document_url)
uno_document = desktop.loadComponentFromURL(uno_url, "_blank", 0, ())
......@@ -219,7 +201,9 @@ class UnoConverter(object):
if field_value_str:
fieldname = document_info.getUserFieldName(number)
metadata[fieldname] = field_value_str
service_manager = helper_util.getServiceManager(self.hostname, self.port)
service_manager = helper_util.getServiceManager(self.hostname, self.port,
self.uno_path,
self.office_binary_path)
type_detection = service_manager.createInstance("com.sun.star.document.TypeDetection")
uno_file_access = service_manager.createInstance("com.sun.star.ucb.SimpleFileAccess")
doc = uno_file_access.openFileRead(self.systemPathToFileUrl(self.document_url))
......
......@@ -33,7 +33,6 @@ try:
except ImportError:
import simplejson as json
import helper_util
from os import environ, path, putenv
from getopt import getopt, GetoptError
from types import InstanceType
......@@ -57,11 +56,11 @@ Options:
class UnoMimemapper(object):
""" """
def __init__(self, hostname, port, **kw):
def __init__(self, hostname, port, uno_path=None, office_binary_path=None):
""" Receives hostname and port from openoffice and create a service manager"""
self._setUpUnoEnvironment(kw.get("uno_path"),
kw.get("office_binary_path"))
self.service_manager = helper_util.getServiceManager(hostname, port)
self.service_manager = helper_util.getServiceManager(hostname, port,
uno_path,
office_binary_path)
def _getElementNameByService(self, uno_service, ignore_name_list=[]):
"""Returns an dict with elements."""
......@@ -80,27 +79,6 @@ class UnoMimemapper(object):
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 \
'URE_BOOTSTRAP' not in environ:
putenv('URE_BOOTSTRAP', 'vnd.sun.star.pathname:%s' % fundamentalrc_file)
def getFilterDict(self):
"""Return all filters and your properties"""
filter_service = self.service_manager.createInstance("com.sun.star.document.FilterFactory")
......@@ -132,19 +110,20 @@ def main():
if not opt_list:
help()
port = hostname = uno_path = office_binary_path = None
for opt, arg in opt_list:
if opt in ("-h", "--help"):
help()
if opt == "--uno_path":
environ['uno_path'] = arg
uno_path = arg
elif opt == "--office_binary_path":
environ['office_binary_path'] = arg
office_binary_path = arg
elif opt == '--hostname':
hostname = arg
elif opt == "--port":
port = arg
mimemapper = UnoMimemapper(hostname, port, **dict(environ))
mimemapper = UnoMimemapper(hostname, port, uno_path, office_binary_path)
filter_dict = mimemapper.getFilterDict()
type_dict = mimemapper.getTypeDict()
......
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