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

factorise _setUpUnoEnvironment function

parent 5e41f4c1
def getServiceManager(host, port): import sys
"""Get the ServiceManager from the running OpenOffice.org.""" 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 import uno
# Get the uno component context from the PyUNO runtime # Get the uno component context from the PyUNO runtime
uno_context = uno.getComponentContext() uno_context = uno.getComponentContext()
......
...@@ -17,20 +17,24 @@ def test_openoffice(hostname, port): ...@@ -17,20 +17,24 @@ def test_openoffice(hostname, port):
def main(): def main():
try: try:
opt_list, arg_list = getopt(sys.argv[1:], "", opt_list, arg_list = getopt(sys.argv[1:], "",
["port=", "hostname=", "uno_path="]) ["port=", "hostname=", "uno_path=",
"office_binary_path="])
except GetoptError, e: except GetoptError, e:
print >> sys.stderr, "%s \nUse --port and --hostname" % e print >> sys.stderr, "%s \nUse --port and --hostname" % e
sys.exit(2) sys.exit(2)
port = hostname = uno_path = office_binary_path = None
for opt, arg in opt_list: for opt, arg in opt_list:
if opt == "--port": if opt == "--port":
port = arg port = arg
elif opt == "--hostname": elif opt == "--hostname":
hostname = arg hostname = arg
elif opt == "--uno_path": 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__": if __name__ == "__main__":
......
...@@ -70,39 +70,19 @@ Options: ...@@ -70,39 +70,19 @@ Options:
class UnoConverter(object): class UnoConverter(object):
"""A module to easily work with OpenOffice.org.""" """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.hostname = hostname
self.port = port self.port = port
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 = source_format
self.refresh = kw.get('refresh') self.refresh = refresh
self._setUpUnoEnvironment(kw.get("uno_path"), self.uno_path = uno_path
kw.get("office_binary_path")) self.office_binary_path = 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 \
'URE_BOOTSTRAP' not in environ:
putenv('URE_BOOTSTRAP', 'vnd.sun.star.pathname:%s' % fundamentalrc_file)
def _createProperty(self, name, value): def _createProperty(self, name, value):
"""Create property""" """Create property"""
from com.sun.star.beans import PropertyValue from com.sun.star.beans import PropertyValue
...@@ -159,7 +139,9 @@ class UnoConverter(object): ...@@ -159,7 +139,9 @@ class UnoConverter(object):
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
""" """
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") 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, ())
...@@ -219,7 +201,9 @@ class UnoConverter(object): ...@@ -219,7 +201,9 @@ 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 = 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") type_detection = service_manager.createInstance("com.sun.star.document.TypeDetection")
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(self.systemPathToFileUrl(self.document_url)) doc = uno_file_access.openFileRead(self.systemPathToFileUrl(self.document_url))
......
...@@ -33,7 +33,6 @@ try: ...@@ -33,7 +33,6 @@ try:
except ImportError: except ImportError:
import simplejson as json import simplejson as json
import helper_util import helper_util
from os import environ, path, putenv
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
from types import InstanceType from types import InstanceType
...@@ -57,11 +56,11 @@ Options: ...@@ -57,11 +56,11 @@ Options:
class UnoMimemapper(object): 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""" """ Receives hostname and port from openoffice and create a service manager"""
self._setUpUnoEnvironment(kw.get("uno_path"), self.service_manager = helper_util.getServiceManager(hostname, port,
kw.get("office_binary_path")) uno_path,
self.service_manager = helper_util.getServiceManager(hostname, port) office_binary_path)
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."""
...@@ -80,27 +79,6 @@ class UnoMimemapper(object): ...@@ -80,27 +79,6 @@ 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 \
'URE_BOOTSTRAP' not in environ:
putenv('URE_BOOTSTRAP', 'vnd.sun.star.pathname:%s' % fundamentalrc_file)
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")
...@@ -132,19 +110,20 @@ def main(): ...@@ -132,19 +110,20 @@ def main():
if not opt_list: if not opt_list:
help() help()
port = hostname = uno_path = office_binary_path = None
for opt, arg in opt_list: for opt, arg in opt_list:
if opt in ("-h", "--help"): if opt in ("-h", "--help"):
help() help()
if opt == "--uno_path": if opt == "--uno_path":
environ['uno_path'] = arg uno_path = arg
elif opt == "--office_binary_path": elif opt == "--office_binary_path":
environ['office_binary_path'] = arg office_binary_path = arg
elif opt == '--hostname': elif opt == '--hostname':
hostname = arg hostname = arg
elif opt == "--port": elif opt == "--port":
port = arg port = arg
mimemapper = UnoMimemapper(hostname, port, **dict(environ)) mimemapper = UnoMimemapper(hostname, port, uno_path, office_binary_path)
filter_dict = mimemapper.getFilterDict() filter_dict = mimemapper.getFilterDict()
type_dict = mimemapper.getTypeDict() 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