Package nsi :: Package granulate :: Module Granulate'
[hide private]
[frames] | no frames]

Source Code for Module nsi.granulate.Granulate'

  1  # -*- coding: utf-8 -*- 
  2  ############################################################################## 
  3  # 
  4  # Copyright (c) 2007 ISrg (NSI, CEFETCAMPOS, BRAZIL) and Contributors.  
  5  #                                                         All Rights Reserved. 
  6  #                              Ronaldo Amaral Santos <ronaldinho.as@gmail.com>  
  7  # 
  8  # WARNING: This program as such is intended to be used by professional 
  9  # programmers who take the whole responsability of assessing all potential 
 10  # consequences resulting from its eventual inadequacies and bugs 
 11  # End users who are looking for a ready-to-use solution with commercial 
 12  # garantees and support are strongly adviced to contract a Free Software 
 13  # Service Company 
 14  # 
 15  # This program is Free Software; you can redistribute it and/or 
 16  # modify it under the terms of the GNU General Public License 
 17  # as published by the Free Software Foundation; either version 2 
 18  # of the License, or (at your option) any later version. 
 19  # 
 20  # This program is distributed in the hope that it will be useful, 
 21  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 22  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 23  # GNU General Public License for more details. 
 24  # 
 25  # You should have received a copy of the GNU General Public License 
 26  # along with this program; if not, write to the Free Software 
 27  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 28  # 
 29  ############################################################################## 
 30   
 31  __author__ = """Ronaldo Amaral Santos <ronaldinho.as@gmail.com>""" 
 32  __docformat__ = 'plaintext' 
 33   
 34  from GranularUtils import Grain 
 35  from FileUtils import File 
 36  from GranulateOffice import GranulateOffice 
 37  from GranulatePDF import GranulatePDF 
 38   
 39   
40 -class Granulate(object):
41 """ 42 - Provides the content granularization delegating the responsability to the appropriate class. 43 - Keeps the oood server related information (host and port). 44 45 About the usage of the methods: getSummaryDocument, getThumbnailsDocument, getTableDocumentList, 46 getImageDocumentList and granulateDocument: 47 - The first parameter is the filename (without the path), like "test.odt" 48 - The second one is a string representing the file content. 49 Ex. >>> file = open(filepath,'r') 50 >>> file.read() 51 """ 52 ooodServer = None 53 supportedOfficeDocument=('application/vnd.oasis.opendocument.text', 54 'application/vnd.sun.xml.writer', 55 'application/msword', 56 'application/rtf', 57 'application/vnd.stardivision.writer', 58 'application/x-starwriter', 59 'application/vnd.oasis.opendocument.spreadsheet', 60 'application/vnd.sun.xml.calc', 61 'application/vnd.ms-excel', 62 'application/vnd.stardivision.calc', 63 'application/x-starcalc', 64 'application/vnd.oasis.opendocument.presentation', 65 'application/vnd.sun.xml.impress', 66 'application/vnd.ms-powerpoint', 67 'application/vnd.stardivision.draw', 68 'application/vnd.stardivision.impress', 69 'application/x-starimpress',) 70 71
72 - def __init__(self, host=None, port=None):
73 """ 74 Set the host and port of the oood daemon during the instance creation 75 """ 76 if (host is not None) and (port is not None): 77 self.ooodServer = 'http://%s:%d' % (host, port)
78 79 80 # Methods 81
82 - def __process(self, filename=None, data=None):
83 """ 84 Checks the file type and creates the proper object. 85 """ 86 if filename and data: 87 # create instance FileUtils 88 Document = File(filename=filename, data=data) 89 90 if Document.getContentType() in self.supportedOfficeDocument: 91 return GranulateOffice(Document, self.ooodServer) 92 93 elif Document.getContentType() == 'application/pdf': 94 return GranulatePDF(Document) 95 96 return None
97
98 - def setServer(self, host=None, port=None):
99 """ 100 Set OpenOffice Daemon - oood host and port for conversion document 101 """ 102 self.ooodServer = 'http://%s:%d' % (host, port)
103
104 - def getServer(self):
105 """ 106 Get OpenOffice Daemon - oood informations for conversion document 107 """ 108 return self.ooodServer
109
110 - def getSummaryDocument(self, filename=None, data=None):
111 """ 112 Get Office Document's summary (odf, doc, ppt for instance) 113 """ 114 GranulateObj = self.__process(filename=filename, data=data) 115 if hasattr(GranulateObj, 'getSummaryDocument'): 116 return GranulateObj.getSummaryDocument() 117 else: 118 return None
119
120 - def getThumbnailsDocument(self, filename=None, data=None):
121 """ 122 Get Office Document's Thumbnails (odf, doc, ppt for instance) 123 """ 124 GranulateObj = self.__process(filename=filename, data=data) 125 if hasattr(GranulateObj, 'getThumbnailsDocument'): 126 return GranulateObj.getThumbnailsDocument() 127 else: 128 return None
129
130 - def getTableDocumentList(self, filename=None, data=None):
131 """ 132 Get a Table List from Office and PDF Documents 133 """ 134 GranulateObj = self.__process(filename=filename, data=data) 135 if hasattr(GranulateObj, 'getTableDocumentList'): 136 return GranulateObj.getTableDocumentList() 137 else: 138 return None
139
140 - def getImageDocumentList(self, filename=None, data=None):
141 """ 142 Get a Image List from Office and PDF Documents 143 """ 144 GranulateObj = self.__process(filename=filename, data=data) 145 if hasattr(GranulateObj, 'getImageDocumentList'): 146 return GranulateObj.getImageDocumentList() 147 else: 148 return None
149
150 - def granulateDocument(self, filename=None, data=None ):
151 """ 152 Retrieve the grains of Documents. (tables and images, for example) 153 """ 154 GranulateObj = self.__process(filename=filename, data=data) 155 if hasattr(GranulateObj, 'granulateDocument'): 156 return GranulateObj.granulateDocument() 157 else: 158 return None
159