Ejemplo n.º 1
0
class TestFile(unittest.TestCase):
  """Test to class File"""

  def setUp(self):
    """Create data to tests and instantiated a File"""
    self.tmp_url = '/tmp'
    self.data = decodestring("cloudooo Test")
    self.fsdocument = File(self.tmp_url, self.data, 'txt')

  def tearDown(self):
    """Remove the file in system"""
    if self.fsdocument.getUrl() is not None:
      self.fsdocument.trash()

  def testRestoreOriginal(self):
    """Test if changing the document and call remake, the document back to
    original state"""
    old_document_url = self.fsdocument.getUrl()
    document_filename = "document"
    document_test_url = path.join(self.fsdocument.directory_name,
                                  document_filename)
    open(document_test_url, 'wb').write(decodestring("Test Document"))
    self.fsdocument.reload(document_test_url)
    self.assertEquals(path.exists(old_document_url), False)
    self.assertNotEquals(self.fsdocument.original_data,
        self.fsdocument.getContent())
    old_document_url = self.fsdocument.getUrl()
    self.fsdocument.restoreOriginal()
    self.assertEquals(path.exists(old_document_url), False)
    self.assertNotEquals(old_document_url, self.fsdocument.getUrl())
    self.assertTrue(path.exists(self.fsdocument.getUrl()))
    self.assertEquals(self.fsdocument.getContent(), self.data)

  def testgetContent(self):
    """Test if returns the data correctly"""
    self.assertEquals(self.fsdocument.getContent(), self.data)

  def testgetUrl(self):
    """Check if the url is correct"""
    url = self.fsdocument.getUrl()
    self.assertTrue(path.exists(url))

  def testLoadDocumentFile(self):
    """Test if the document is created correctly"""
    url = self.fsdocument.getUrl()
    tmp_document = open(url, 'r').read()
    self.assertEquals(self.data, tmp_document)
    self.fsdocument.trash()
    self.assertEquals(path.exists(url), False)

  def testReload(self):
    """Change url and check if occurs correctly"""
    old_document_url = self.fsdocument.getUrl()
    document_filename = "document"
    document_test_url = path.join(self.fsdocument.directory_name,
                                               document_filename)
    open(document_test_url, 'wb').write(self.data)
    self.fsdocument.reload(document_test_url)
    url = self.fsdocument.getUrl()
    self.assertEquals(path.exists(old_document_url), False)
    self.assertEquals(self.fsdocument.getContent(), self.data)
    self.fsdocument.trash()
    self.assertEquals(path.exists(url), False)

  def testZipDocumentList(self):
    """Tests if the zip file is returned correctly"""
    open(path.join(self.fsdocument.directory_name, 'document2'), 'w').write('test')
    zip_file = self.fsdocument.getContent(True)
    mime = magic.Magic(mime=True)
    mimetype = mime.from_buffer(zip_file)
    self.assertEquals(mimetype, 'application/zip')
    ziptest = ZipFile(StringIO(zip_file), 'r')
    self.assertEquals(len(ziptest.filelist), 2)
    for file in ziptest.filelist:
      if file.filename.endswith("document2"):
        self.assertEquals(file.file_size, 4)
      else:
        self.assertEquals(file.file_size, 9)

  def testSendZipFile(self):
    """Tests if the htm is extrated from zipfile"""
    zip_input_url = 'data/test.zip'
    data = open(zip_input_url).read()
    zipdocument = File(self.tmp_url, data, 'zip')
    mime = magic.Magic(mime=True)
    mimetype = mime.from_buffer(zipdocument.getContent(True))
    self.assertEquals(mimetype, "application/zip")
    mimetype = mime.from_buffer(zipdocument.getContent())
    self.assertEquals(mimetype, "text/html")
    zipfile = ZipFile(StringIO(zipdocument.getContent(True)))
    self.assertEquals(sorted(zipfile.namelist()),
                sorted(['logo.gif', 'test.htm']))
Ejemplo n.º 2
0
class Handler(object):
  """OOO Handler is used to access the one Document and OpenOffice.
  For each Document inputed is created on instance of this class to manipulate
  the document. This Document must be able to create and remove a temporary
  document at FS, load and export.
  """
  implements(IHandler)

  def __init__(self, base_folder_url, data, source_format, **kw):
    """Creates document in file system and loads it in OOo."""
    self.document = File(base_folder_url, data, source_format)
    self.zip = kw.get('zip', False)
    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.refresh = kw.get('refresh', False)
    self.source_format = source_format
    if not self.uno_path:
      self.uno_path = environ.get("uno_path")
    if not self.office_binary_path:
      self.office_binary_path = environ.get("office_binary_path")

  def _getCommand(self, *args, **kw):
    """Transforms all parameters passed in a command"""
    hostname, port = openoffice.getAddress()
    kw['hostname'] = hostname
    kw['port'] = port
    python = path.join(self.office_binary_path, "python")
    command_list = [path.exists(python) and python or "python",
                    pkg_resources.resource_filename(__name__,
                                 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():
      command_list.append("--%s=%s" % (k, v))

    return command_list

  def _startTimeout(self):
    """start the Monitor"""
    self.monitor = MonitorTimeout(openoffice, self.timeout)
    self.monitor.start()
    return

  def _stopTimeout(self):
    """stop the Monitor"""
    self.monitor.terminate()
    return

  def _subprocess(self, command_list):
    """Run one procedure"""
    if monitor_sleeping_time is not None:
      monitor_sleeping_time.touch()
    try:
      self._startTimeout()
      process = Popen(command_list, stdout=PIPE, stderr=PIPE, close_fds=True,
                      env=openoffice.environment_dict.copy())
      stdout, stderr = process.communicate()
    finally:
      self._stopTimeout()
      if pid_exists(process.pid):
        process.terminate()
    return stdout, stderr

  def _callUnoConverter(self, *feature_list, **kw):
    """ """
    if not openoffice.status():
      openoffice.start()
    command_list = self._getCommand(*feature_list, **kw)
    stdout, stderr = self._subprocess(command_list)
    if not stdout and len(re.findall("\w*Exception|\w*Error", stderr)) >= 1:
      logger.debug(stderr)
      self.document.restoreOriginal()
      openoffice.restart()
      kw['document_url'] = self.document.getUrl()
      command = self._getCommand(*feature_list, **kw)
      stdout, stderr = self._subprocess(command)
      if stderr != "":
          raise Exception(stderr)

    return stdout, stderr

  def _serializeMimemapper(self,
                           source_extension=None,
                           destination_extension=None):
    """Serialize parts of mimemapper"""
    if destination_extension is None:
      return json.dumps(dict(mimetype_by_filter_type=mimemapper._mimetype_by_filter_type))

    filter_list = []
    service_type_list = mimemapper._doc_type_list_by_extension.get(
      source_extension, mimemapper.document_service_list)
    for service_type in service_type_list:
      filter_list.append((destination_extension,
                          service_type,
                          mimemapper.getFilterName(destination_extension, service_type)))
    logger.debug("Filter List: %r" % filter_list)
    return json.dumps(dict(doc_type_list_by_extension=mimemapper._doc_type_list_by_extension,
                            filter_list=filter_list,
                            mimetype_by_filter_type=mimemapper._mimetype_by_filter_type))

  def convert(self, destination_format=None, **kw):
    """Convert a document to another format supported by the OpenOffice
    Keyword Arguments:
    destination_format -- extension of document as String
    """
    logger.debug("OooConvert: %s > %s" % (self.source_format, destination_format))
    kw['source_format'] = self.source_format
    if destination_format:
      kw['destination_format'] = destination_format
    kw['mimemapper'] = self._serializeMimemapper(self.source_format,
                                                 destination_format)
    kw['refresh'] = json.dumps(self.refresh)
    openoffice.acquire()
    try:
      stdout, stderr = self._callUnoConverter(*['convert'], **kw)
    finally:
      openoffice.release()
    url = stdout.replace('\n', '')
    self.document.reload(url)
    content = self.document.getContent(self.zip)
    self.document.trash()
    return content

  def getMetadata(self, base_document=False):
    """Returns a dictionary with all metadata of document.
    Keywords Arguments:
    base_document -- Boolean variable. if true, the document is also returned
    along with the metadata."""
    logger.debug("getMetadata")
    kw = dict(mimemapper=self._serializeMimemapper())
    if base_document:
      feature_list = ['getmetadata', 'convert']
    else:
      feature_list = ['getmetadata']
    openoffice.acquire()
    try:
      stdout, stderr = self._callUnoConverter(*feature_list, **kw)
    finally:
      openoffice.release()
    metadata = json.loads(decodestring(stdout))
    if 'document_url' in metadata:
      self.document.reload(metadata['document_url'])
      metadata['Data'] = self.document.getContent()
      del metadata['document_url']
    self.document.trash()
    return metadata

  def setMetadata(self, metadata):
    """Returns a document with new metadata.
    Keyword arguments:
    metadata -- expected an dictionary with metadata.
    """
    metadata_pickled = json.dumps(metadata)
    logger.debug("setMetadata")
    kw = dict(metadata=encodestring(metadata_pickled))
    openoffice.acquire()
    try:
      stdout, stderr = self._callUnoConverter(*['setmetadata'], **kw)
    finally:
      openoffice.release()
    doc_loaded = self.document.getContent()
    self.document.trash()
    return doc_loaded