Package backends :: Module oooserver
[hide private]

Source Code for Module backends.oooserver

  1  # Officeshots.org - Test your office documents in different applications 
  2  # Copyright (C) 2009 Stichting Lone Wolves 
  3  # Written by Sander Marechal <s.marechal@jejik.com> 
  4  # 
  5  # Heavily based on PyODConvert, which is 
  6  # Copyright (C) 2007 Mirko Nasato <mirko@artofsolving.com> 
  7  # Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html 
  8   
  9  """ 
 10  This module is a back-end for the headless OpenOffice.org server 
 11  """ 
 12   
 13  import os 
 14  import uno 
 15  import logging 
 16   
 17  from backends import Backend, BackendException 
 18  from com.sun.star.beans import PropertyValue 
 19  from com.sun.star.connection import NoConnectException 
 20  from com.sun.star.document.UpdateDocMode import QUIET_UPDATE 
 21   
 22  FAMILY_MASTER = "Master" 
 23  FAMILY_PRESENTATION = "Presentation" 
 24  FAMILY_SPREADSHEET = "Spreadsheet" 
 25  FAMILY_TEXT = "Text" 
 26   
 27  FAMILY_BY_DOCTYPE = { 
 28          "odt": FAMILY_TEXT, 
 29          "sxw": FAMILY_TEXT, 
 30          "doc": FAMILY_TEXT, 
 31          "rtf": FAMILY_TEXT, 
 32          "txt": FAMILY_TEXT, 
 33          "wpd": FAMILY_TEXT, 
 34          "html": FAMILY_TEXT, 
 35          "ods": FAMILY_SPREADSHEET, 
 36          "sxc": FAMILY_SPREADSHEET, 
 37          "xls": FAMILY_SPREADSHEET, 
 38          "odp": FAMILY_PRESENTATION, 
 39          "sxi": FAMILY_PRESENTATION, 
 40          "ppt": FAMILY_PRESENTATION 
 41  } 
 42   
 43  FILTER_BY_FORMAT = { 
 44          "pdf": { 
 45                  FAMILY_TEXT: "writer_pdf_Export", 
 46                  FAMILY_SPREADSHEET: "calc_pdf_Export", 
 47                  FAMILY_PRESENTATION: "impress_pdf_Export" 
 48          }, 
 49          "odf": { 
 50                  FAMILY_TEXT: "writer8", 
 51                  FAMILY_SPREADSHEET: "calc8", 
 52                  FAMILY_PRESENTATION: "impress8" 
 53          } 
 54  } 
 55   
56 -def _unoProps(**args):
57 props = [] 58 for key in args: 59 prop = PropertyValue() 60 prop.Name = key 61 prop.Value = args[key] 62 props.append(prop) 63 return tuple(props)
64 65
66 -class OOOServerException(BackendException):
67 - def __str__(self):
68 return 'OOOServerException: ' + BackendException.__str__(self)
69 70
71 -class OOOServer(Backend):
72 """ 73 Backend for the headless OpenOffice.org server 74 """
75 - def initialize(self):
76 self.ooo_host = self.config.get(self.section, 'ooo_host') 77 self.ooo_port = self.config.get(self.section, 'ooo_port') 78 79 localContext = uno.getComponentContext() 80 resolver = localContext.ServiceManager.createInstanceWithContext('com.sun.star.bridge.UnoUrlResolver', localContext) 81 try: 82 context = resolver.resolve('uno:socket,host=%s,port=%s;urp;StarOffice.ComponentContext' % (self.ooo_host, self.ooo_port)) 83 except NoConnectException: 84 raise OOOServerException('failed to connect to OpenOffice.org on %s port %s' % (self.ooo_host, self.ooo_port)) 85 self.desktop = context.ServiceManager.createInstanceWithContext('com.sun.star.frame.Desktop', context)
86
87 - def filter_name(self, doctype, format):
88 try: 89 family = FAMILY_BY_DOCTYPE[doctype] 90 except KeyError: 91 raise OOOServerException("Unknown input doctype: '%s'" % doctype) 92 try: 93 filterByFamily = FILTER_BY_FORMAT[format] 94 except KeyError: 95 raise OOOServerException("Unknown output format: '%s'" % format) 96 try: 97 return filterByFamily[family] 98 except KeyError: 99 raise OOOServerException("Unsupported conversion: from '%s' to '%s'" % (doctype, format))
100
101 - def file_url(self, path):
102 return uno.systemPathToFileUrl(os.path.abspath(path))
103
104 - def process(self, job):
105 src_file = self.save_document(job) 106 (root, ext) = os.path.splitext(src_file) 107 dst_file = root + '_result' + ext 108 109 if (job['format'] == ''): 110 job['format'] = 'pdf' 111 112 filter_name = self.filter_name(job['doctype'], job['format']) 113 document = self.desktop.loadComponentFromURL(self.file_url(src_file), "_blank", 0, _unoProps(Hidden=True, ReadOnly=True, UpdateDocMode=QUIET_UPDATE)) 114 document.storeToURL(self.file_url(dst_file), _unoProps(FilterName=filter_name)) 115 document.close(True) 116 117 logging.info('OOOServer converted %s to %s' % (src_file, dst_file)) 118 contents = self.load_document(dst_file) 119 os.unlink(src_file) 120 os.unlink(dst_file) 121 122 return (job['format'], contents)
123