[Erp5-report] r38552 gabriel - in /erp5/trunk/utils/cloudooo/cloudooo: ./ bin/ handler/ hel...

nobody at svn.erp5.org nobody at svn.erp5.org
Wed Sep 22 17:42:24 CEST 2010


Author: gabriel
Date: Wed Sep 22 17:42:24 2010
New Revision: 38552

URL: http://svn.erp5.org?rev=38552&view=rev
Log:
- refactor code to use pkg_resources
- move unoconverter.py and unomimemapper.py to helper folder

Added:
    erp5/trunk/utils/cloudooo/cloudooo/helper/
    erp5/trunk/utils/cloudooo/cloudooo/helper/unoconverter.py
      - copied, changed from r38533, erp5/trunk/utils/cloudooo/cloudooo/bin/unoconverter.py
    erp5/trunk/utils/cloudooo/cloudooo/helper/unomimemapper.py
      - copied, changed from r38533, erp5/trunk/utils/cloudooo/cloudooo/bin/unomimemapper.py
Removed:
    erp5/trunk/utils/cloudooo/cloudooo/bin/unoconverter.py
    erp5/trunk/utils/cloudooo/cloudooo/bin/unomimemapper.py
Modified:
    erp5/trunk/utils/cloudooo/cloudooo/handler/oohandler.py
    erp5/trunk/utils/cloudooo/cloudooo/mimemapper.py

Removed: erp5/trunk/utils/cloudooo/cloudooo/bin/unoconverter.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/bin/unoconverter.py?rev=38551&view=auto
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/bin/unoconverter.py [utf8] (original)
+++ erp5/trunk/utils/cloudooo/cloudooo/bin/unoconverter.py (removed)
@@ -1,275 +0,0 @@
-#!/usr/bin/env python
-##############################################################################
-#
-# Copyright (c) 2002-2010 Nexedi SA and Contributors. All Rights Reserved.
-#                    Gabriel M. Monnerat <gabriel at tiolive.com>
-#
-# WARNING: This program as such is intended to be used by professional
-# programmers who take the whole responsibility of assessing all potential
-# consequences resulting from its eventual inadequacies and bugs
-# End users who are looking for a ready-to-use solution with commercial
-# guarantees and support are strongly adviced to contract a Free Software
-# Service Company
-#
-# This program is Free Software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-#
-##############################################################################
-
-import sys
-import jsonpickle
-from types import UnicodeType, InstanceType
-from os import environ
-from os.path import dirname
-from tempfile import mktemp
-from getopt import getopt, GetoptError
-from cloudooo import ooolib
-from cloudooo.utils import usage
-
-__doc__ = """
-
-usage: unoconverter [options]
-
-Options:
-  -h, --help            this help screen
-  
-  --test                Test if the Openoffice works correctly
-  
-  --hostname=STRING     OpenOffice Instance address
-  
-  --port=STRING         OpenOffice Instance port
-  
-  --document_url=STRING_URL 
-                        URL of document to load in OpenOffice
-  
-  --office_binary_path=STRING_URL
-                        Folder path were is the binary openoffice
-  --uno_path=STRING_URL
-                        Folter path were is the uno library
-  --destination_format=STRING
-                        extension to export the document
-  --mimemapper=OBJECT_SERIALIZED
-                        Mimemapper serialized. The object is passed using
-                        jsonpickle. IF this option is None, the object is
-                        created
-  --metadata=DICT_SERIALIZED
-                        Dictionary with metadata
-"""
-
-class UnoConverter(object):
-  """A module to easily work with OpenOffice.org."""
-
-  def __init__(self, hostname, port, document_url, **kw):
-    """ """
-    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._load()
-
-  def _getPropertyToExport(self, destination_format=None):
-    """Create the property according to the extension of the file."""
-    if destination_format and self.document_loaded:
-      doc_type_list = mimemapper._doc_type_list_by_extension.get(destination_format)
-      if self.document_type not in doc_type_list:
-        raise AttributeError, \
-                        "This Document can not be converted for this format"
-      type = self.document_type
-      filter_name = mimemapper.getFilterName(destination_format, type)
-      property_list = []
-      property = ooolib.createProperty("Overwrite", True)
-      property_list.append(property)
-      property = ooolib.createProperty("FilterName", filter_name)
-      property_list.append(property)
-      property_list.extend(ooolib.createSpecificProperty(filter_name))
-      return property_list
-    else:
-      return ()
-
-  def _load(self):
-    """Create one document with basic properties"""
-    service_manager = ooolib.getServiceManager(self.hostname, self.port)
-    desktop = service_manager.createInstance("com.sun.star.frame.Desktop")
-    uno_url = ooolib.systemPathToFileUrl(self.document_url)
-    uno_document = desktop.loadComponentFromURL(uno_url, "_blank", 0, ())
-    module_manager = service_manager.createInstance("com.sun.star.frame.ModuleManager")
-    if not uno_document:
-      raise AttributeError, "This document can not be loaded or is empty"
-    self.document_type = module_manager.identify(uno_document)
-    self.document_loaded = uno_document
- 
-  def convert(self, output_format=None):
-    """it converts a document to specific format"""
-    if output_format in ("html", "htm", "xhtml"):
-      destination_format = "impr.html"
-    else:
-      destination_format = output_format
-    output_url = mktemp(suffix='.%s' % destination_format,
-                        dir=self.document_dir_path)
-
-    property_list = self._getPropertyToExport(output_format)
-    try:
-      self.document_loaded.storeToURL(ooolib.systemPathToFileUrl(output_url),
-         tuple(property_list))
-    finally:
-      self.document_loaded.dispose()
-    return output_url
-
-  def getMetadata(self):
-    """Extract all metadata of the document"""
-    metadata = {}
-    document_info = self.document_loaded.getDocumentInfo()
-    property_list = [prop.Name for prop in document_info.getPropertyValues() \
-        if prop.Value]
-    for property_name in iter(property_list):
-      property = document_info.getPropertyValue(property_name)
-      if type(property) == UnicodeType:
-          metadata[property_name] = property
-      elif type(property) == InstanceType:
-        if property.typeName == 'com.sun.star.util.DateTime':
-          datetime = "%s/%s/%s %s:%s:%s" % (property.Day, property.Month,
-              property.Year, property.Hours, property.Minutes, property.Seconds)
-          metadata[property_name] = datetime
-    for number in range(document_info.getUserFieldCount()):
-      field_value_str = document_info.getUserFieldValue(number)
-      if field_value_str:
-        fieldname = document_info.getUserFieldName(number)
-        metadata[fieldname] = field_value_str
-    service_manager = ooolib.getServiceManager(self.hostname, self.port)
-    uno_file_access = service_manager.createInstance("com.sun.star.ucb.SimpleFileAccess")
-    doc = uno_file_access.openFileRead(ooolib.systemPathToFileUrl(self.document_url))
-    property_list = []
-    property = ooolib.createProperty("InputStream", doc)
-    property_list.append(property)
-    type_detection = service_manager.createInstance("com.sun.star.document.TypeDetection")
-    filter_name = type_detection.queryTypeByDescriptor(tuple(property_list), \
-        True)[0]
-    doc.closeInput()
-    metadata['MIMEType'] = mimemapper.getMimetypeByFilterType(filter_name)
-    return metadata
-
-  def setMetadata(self, metadata):
-    """Returns a document with new metadata.
-    
-    Keyword arguments:
-    metadata -- expected an dictionary with metadata.
-    """
-    doc_info = self.document_loaded.getDocumentInfo()
-    prop_name_list = [prop.Name for prop in doc_info.getPropertyValues()]
-    user_info_counter = 0
-    for prop, value in metadata.iteritems():
-      if prop in prop_name_list:
-        doc_info.setPropertyValue(prop, value)
-      else:
-        max_index = doc_info.getUserFieldCount()
-        for index in range(max_index):
-          if doc_info.getUserFieldName(index).lower() == prop.lower():
-            doc_info.setUserFieldValue(index, value)
-            break
-        else:
-          doc_info.setUserFieldName(user_info_counter, prop)
-          doc_info.setUserFieldValue(user_info_counter, value)
-        user_info_counter += 1
-    self.document_loaded.store()
-    self.document_loaded.dispose()
-
-def help():
-  usage(sys.stderr, __doc__)
-  sys.exit(1)
-
-def main():
-  global mimemapper
-
-  help_msg = "\nUse --help or -h"
-  try:
-    opt_list, arg_list = getopt(sys.argv[1:], "h", ["help", "test",
-      "convert", "getmetadata", "setmetadata",
-      "uno_path=", "office_binary_path=", 
-      "hostname=", "port=", "source_format=",
-      "document_url=", "destination_format=", 
-      "mimemapper=", "metadata=",
-      "unomimemapper_bin="])
-  except GetoptError, msg:
-    msg = msg.msg + help_msg
-    usage(sys.stderr, msg)
-    sys.exit(2)
-  
-  param_list = [tuple[0] for tuple in opt_list]
-
-  for opt, arg in opt_list:
-    if opt in ('-h', '--help'):
-      help()
-    elif opt == '--hostname':
-      hostname = arg
-    elif opt == '--port':
-      port = arg
-    elif opt == '--document_url':
-      document_url = arg
-    elif opt == '--office_binary_path':
-      environ['office_binary_path'] = arg
-      office_binary_path = arg
-    elif opt == '--uno_path':
-      environ['uno_path'] = arg
-      uno_path = arg
-    elif opt == '--destination_format':
-      destination_format = arg
-    elif opt == '--source_format':
-      source_format = arg
-    elif opt == '--metadata':
-      metadata = jsonpickle.decode(arg)
-    elif opt == '--mimemapper':
-      mimemapper = jsonpickle.decode(arg)
-    elif opt == '--unomimemapper_bin':
-      unomimemapper_bin = arg
-   
-  kw = {}
-  
-  if "mimemapper" not in globals() and "--setmetadata" not in param_list:
-    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
-    mimemapper.loadFilterList(hostname=hostname, port=port, **kw)
-  
-  kw.clear()
-  if 'source_format' in locals():
-    kw['source_format'] = source_format
-  
-  unoconverter = UnoConverter(hostname, port, document_url, **kw)
-  if "--convert" in param_list and not '--getmetadata' in param_list \
-      and 'destination_format' not in locals():
-    output = unoconverter.convert()
-  elif '--convert' in param_list and 'destination_format' in locals():
-    output = unoconverter.convert(destination_format)
-  elif '--getmetadata' in param_list and not '--convert' in param_list:
-    output = unoconverter.getMetadata()
-  elif '--getmetadata' in param_list and '--convert' in param_list:
-    output = unoconverter.getMetadata()
-    output['Data'] = unoconverter.convert()
-  elif '--setmetadata' in param_list:
-    unoconverter.setMetadata(metadata)
-    output = document_url
-
-  print output
-    
-if "__main__" == __name__:
-  main()

Removed: erp5/trunk/utils/cloudooo/cloudooo/bin/unomimemapper.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/bin/unomimemapper.py?rev=38551&view=auto
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/bin/unomimemapper.py [utf8] (original)
+++ erp5/trunk/utils/cloudooo/cloudooo/bin/unomimemapper.py (removed)
@@ -1,125 +0,0 @@
-#!/usr/bin/env python
-##############################################################################
-#
-# Copyright (c) 2002-2010 Nexedi SA and Contributors. All Rights Reserved.
-#                    Gabriel M. Monnerat <gabriel at tiolive.com>
-#
-# WARNING: This program as such is intended to be used by professional
-# programmers who take the whole responsibility of assessing all potential
-# consequences resulting from its eventual inadequacies and bugs
-# End users who are looking for a ready-to-use solution with commercial
-# guarantees and support are strongly adviced to contract a Free Software
-# Service Company
-#
-# This program is Free Software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-#
-##############################################################################
-
-import sys
-from os import environ
-from getopt import getopt, GetoptError
-from cloudooo.ooolib import getServiceManager
-from cloudooo.utils import usage
-from types import InstanceType
-
-__doc__ = """
-
-usage: unomimemapper [options]
-
-Options:
-  -h, --help            this help screen
-  --hostname=STRING     OpenOffice Instance address
-  
-  --port=STRING         OpenOffice Instance port
-  
-  --office_binary_path=STRING_URL
-                        Folder path were is the binary openoffice
-  --uno_path=STRING_URL
-                        Folter path were is the uno library
-"""
-
-class UnoMimemapper(object):
-  """ """
-
-  def __init__(self, hostname, port):
-    """ Receives hostname and port from openoffice and create a service manager"""
-    self.service_manager = getServiceManager(hostname, port)
-
-  def _getElementNameByService(self, uno_service, ignore_name_list=[]):
-    """Returns an dict with elements."""
-    name_list = uno_service.getElementNames()
-    service_dict = {}
-    for name in iter(name_list):
-        element_dict = {}
-        element_list = uno_service.getByName(name)
-        for obj in iter(element_list):
-            if obj.Name in ignore_name_list:
-              continue
-            elif type(obj.Value) == InstanceType:
-              continue
-            element_dict[obj.Name] = obj.Value
-            service_dict[name] = element_dict
-
-    return service_dict
-
-  def getFilterDict(self):
-    """Return all filters and your properties"""
-    filter_service = self.service_manager.createInstance("com.sun.star.document.FilterFactory")
-    filter_dict = self._getElementNameByService(filter_service, ["UINames", "UserData"])
-    return filter_dict
-
-  def getTypeDict(self):
-    """Return all types and your properties"""
-    type_service = self.service_manager.createInstance("com.sun.star.document.TypeDetection")
-    type_dict = self._getElementNameByService(type_service, ["UINames", "URLPattern"])
-    return type_dict
-
-
-def help():
-  usage(sys.stderr, __doc__)
-  sys.exit(1)
-
-def main():
-  try:
-    opt_list, arg_list = getopt(sys.argv[1:], "h", ["help",
-      "uno_path=", "office_binary_path=",
-      "hostname=", "port="])
-  except GetoptError, msg:
-    msg = msg.msg + "\nUse --help or -h"
-    usage(sys.stderr, msg)
-    sys.exit(2)
-  
-  if not opt_list:
-    help()
-
-  for opt, arg in opt_list:
-    if opt in ("-h", "--help"):
-      help()
-    if opt == "--uno_path":
-      environ['uno_path'] = arg
-    elif opt == "--office_binary_path":
-      environ['office_binary_path'] = arg
-    elif opt == '--hostname':
-      hostname = arg
-    elif opt == "--port":
-      port = arg
-  
-  mimemapper = UnoMimemapper(hostname, port)
-  filter_dict = mimemapper.getFilterDict()
-  type_dict = mimemapper.getTypeDict()
-  print "filter_dict = %s\ntype_dict = %s" % (filter_dict, type_dict)
-
-if "__main__" == __name__:
-  main()

Modified: erp5/trunk/utils/cloudooo/cloudooo/handler/oohandler.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/handler/oohandler.py?rev=38552&r1=38551&r2=38552&view=diff
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/handler/oohandler.py [utf8] (original)
+++ erp5/trunk/utils/cloudooo/cloudooo/handler/oohandler.py [utf8] Wed Sep 22 17:42:24 2010
@@ -26,8 +26,8 @@
 #
 ##############################################################################
 
-import jsonpickle
-from os import environ
+import jsonpickle, pkg_resources
+from os import environ, path
 from subprocess import Popen, PIPE
 from cloudooo.application.openoffice import openoffice
 from cloudooo.application.xvfb import xvfb
@@ -58,7 +58,6 @@ class OOHandler:
     self.uno_path = kw.get("uno_path", None)
     self.office_binary_path = kw.get("office_binary_path", None)
     self.timeout = kw.get("timeout", 600)
-    self.unoconverter_bin = kw.get('unoconverter_bin', "unoconverter")
     self.source_format = source_format
     if not self.uno_path:
       self.uno_path = environ.get("uno_path")
@@ -71,11 +70,11 @@ class OOHandler:
     kw['hostname'] = hostname
     kw['port'] = port
     command_list = [python_path
-        , "-c"
-	, "'from cloudooo.bin.unoconverter import main;main()'"
-        , "--uno_path='%s'" % self.uno_path
-        , "--office_binary_path='%s'" % self.office_binary_path
-        , "--document_url='%s'" % self.document.getUrl()]
+                    , pkg_resources.resource_filename("cloudooo",
+                                        path.join("helper", "unoconverter.py"))
+                    , "--uno_path='%s'" % self.uno_path
+                    , "--office_binary_path='%s'" % self.office_binary_path
+                    , "--document_url='%s'" % self.document.getUrl()]
     for arg in args:
       command_list.insert(3, "'--%s'" % arg)
     for k, v in kw.iteritems():

Copied: erp5/trunk/utils/cloudooo/cloudooo/helper/unoconverter.py (from r38533, erp5/trunk/utils/cloudooo/cloudooo/bin/unoconverter.py)
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/helper/unoconverter.py?p2=erp5/trunk/utils/cloudooo/cloudooo/helper/unoconverter.py&p1=erp5/trunk/utils/cloudooo/cloudooo/bin/unoconverter.py&r1=38533&r2=38552&rev=38552&view=diff
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/bin/unoconverter.py [utf8] (original)
+++ erp5/trunk/utils/cloudooo/cloudooo/helper/unoconverter.py [utf8] Wed Sep 22 17:42:24 2010
@@ -30,12 +30,10 @@
 import sys
 import jsonpickle
 from types import UnicodeType, InstanceType
-from os import environ
-from os.path import dirname
+from os import environ, putenv
+from os.path import dirname, exists
 from tempfile import mktemp
 from getopt import getopt, GetoptError
-from cloudooo import ooolib
-from cloudooo.utils import usage
 
 __doc__ = """
 
@@ -77,8 +75,78 @@ class UnoConverter(object):
     self.document_url = document_url
     self.document_dir_path = dirname(document_url)
     self.source_format = kw.get('source_format')
+    self._setUpUnoEnvironment(kw.get("uno_path"), 
+                              kw.get("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 \
+       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):
     """Create the property according to the extension of the file."""
     if destination_format and self.document_loaded:
@@ -89,27 +157,32 @@ class UnoConverter(object):
       type = self.document_type
       filter_name = mimemapper.getFilterName(destination_format, type)
       property_list = []
-      property = ooolib.createProperty("Overwrite", True)
+      property = self._createProperty("Overwrite", True)
       property_list.append(property)
-      property = ooolib.createProperty("FilterName", filter_name)
+      property = self._createProperty("FilterName", filter_name)
       property_list.append(property)
-      property_list.extend(ooolib.createSpecificProperty(filter_name))
+      property_list.extend(self._createSpecificProperty(filter_name))
       return property_list
     else:
       return ()
 
   def _load(self):
     """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")
-    uno_url = ooolib.systemPathToFileUrl(self.document_url)
+    uno_url = self.systemPathToFileUrl(self.document_url)
     uno_document = desktop.loadComponentFromURL(uno_url, "_blank", 0, ())
     module_manager = service_manager.createInstance("com.sun.star.frame.ModuleManager")
     if not uno_document:
       raise AttributeError, "This document can not be loaded or is empty"
     self.document_type = module_manager.identify(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):
     """it converts a document to specific format"""
     if output_format in ("html", "htm", "xhtml"):
@@ -121,7 +194,7 @@ class UnoConverter(object):
 
     property_list = self._getPropertyToExport(output_format)
     try:
-      self.document_loaded.storeToURL(ooolib.systemPathToFileUrl(output_url),
+      self.document_loaded.storeToURL(self.systemPathToFileUrl(output_url),
          tuple(property_list))
     finally:
       self.document_loaded.dispose()
@@ -147,11 +220,11 @@ class UnoConverter(object):
       if field_value_str:
         fieldname = document_info.getUserFieldName(number)
         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")
-    doc = uno_file_access.openFileRead(ooolib.systemPathToFileUrl(self.document_url))
+    doc = uno_file_access.openFileRead(self.systemPathToFileUrl(self.document_url))
     property_list = []
-    property = ooolib.createProperty("InputStream", doc)
+    property = self._createProperty("InputStream", doc)
     property_list.append(property)
     type_detection = service_manager.createInstance("com.sun.star.document.TypeDetection")
     filter_name = type_detection.queryTypeByDescriptor(tuple(property_list), \
@@ -186,7 +259,7 @@ class UnoConverter(object):
     self.document_loaded.dispose()
 
 def help():
-  usage(sys.stderr, __doc__)
+  print >> sys.stderr, __doc__
   sys.exit(1)
 
 def main():
@@ -203,7 +276,7 @@ def main():
       "unomimemapper_bin="])
   except GetoptError, msg:
     msg = msg.msg + help_msg
-    usage(sys.stderr, msg)
+    print >> sys.stderr, msg
     sys.exit(2)
   
   param_list = [tuple[0] for tuple in opt_list]
@@ -235,22 +308,17 @@ def main():
       unomimemapper_bin = arg
    
   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:
     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
     mimemapper.loadFilterList(hostname=hostname, port=port, **kw)
   
-  kw.clear()
   if 'source_format' in locals():
     kw['source_format'] = source_format
   

Copied: erp5/trunk/utils/cloudooo/cloudooo/helper/unomimemapper.py (from r38533, erp5/trunk/utils/cloudooo/cloudooo/bin/unomimemapper.py)
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/helper/unomimemapper.py?p2=erp5/trunk/utils/cloudooo/cloudooo/helper/unomimemapper.py&p1=erp5/trunk/utils/cloudooo/cloudooo/bin/unomimemapper.py&r1=38533&r2=38552&rev=38552&view=diff
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/bin/unomimemapper.py [utf8] (original)
+++ erp5/trunk/utils/cloudooo/cloudooo/helper/unomimemapper.py [utf8] Wed Sep 22 17:42:24 2010
@@ -28,10 +28,8 @@
 ##############################################################################
 
 import sys
-from os import environ
+from os import environ, path, putenv
 from getopt import getopt, GetoptError
-from cloudooo.ooolib import getServiceManager
-from cloudooo.utils import usage
 from types import InstanceType
 
 __doc__ = """
@@ -53,9 +51,11 @@ Options:
 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"""
-    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=[]):
     """Returns an dict with elements."""
@@ -74,6 +74,42 @@ 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 \
+       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):
     """Return all filters and your properties"""
     filter_service = self.service_manager.createInstance("com.sun.star.document.FilterFactory")
@@ -86,9 +122,8 @@ class UnoMimemapper(object):
     type_dict = self._getElementNameByService(type_service, ["UINames", "URLPattern"])
     return type_dict
 
-
 def help():
-  usage(sys.stderr, __doc__)
+  print >> sys.stderr, __doc__
   sys.exit(1)
 
 def main():
@@ -98,12 +133,12 @@ def main():
       "hostname=", "port="])
   except GetoptError, msg:
     msg = msg.msg + "\nUse --help or -h"
-    usage(sys.stderr, msg)
+    print >> sys.stderr, msg
     sys.exit(2)
   
   if not opt_list:
     help()
-
+  
   for opt, arg in opt_list:
     if opt in ("-h", "--help"):
       help()
@@ -116,7 +151,7 @@ def main():
     elif opt == "--port":
       port = arg
   
-  mimemapper = UnoMimemapper(hostname, port)
+  mimemapper = UnoMimemapper(hostname, port, **environ)
   filter_dict = mimemapper.getFilterDict()
   type_dict = mimemapper.getTypeDict()
   print "filter_dict = %s\ntype_dict = %s" % (filter_dict, type_dict)

Modified: erp5/trunk/utils/cloudooo/cloudooo/mimemapper.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/mimemapper.py?rev=38552&r1=38551&r2=38552&view=diff
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/mimemapper.py [utf8] (original)
+++ erp5/trunk/utils/cloudooo/cloudooo/mimemapper.py [utf8] Wed Sep 22 17:42:24 2010
@@ -26,11 +26,12 @@
 #
 ##############################################################################
 
+import pkg_resources
 from re import findall
 from subprocess import Popen, PIPE
 from zope.interface import implements
 from filter import Filter
-from os import environ
+from os import environ, path
 from sys import executable as python_path
 from interfaces.mimemapper import IMimemapper
 from types import InstanceType
@@ -103,13 +104,15 @@ class MimeMapper(object):
     # 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]
     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
-              , "'-c'"
-              , "'from cloudooo.bin.unomimemapper import main;main()'"
+              , pkg_resources.resource_filename(__name__, 
+                                        path.join("helper","unomimemapper.py"))
               , "'--uno_path=%s'" % uno_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=PIPE,
                           close_fds=True,




More information about the Erp5-report mailing list