Esempio n. 1
0
  def getTable(self, id, format='odt'):
    """Returns the table into a new 'format' file."""
    try:
      template_path = getTemplatePath(format)
      template = ZipFile(template_path)
      content_xml = etree.fromstring(template.read('content.xml'))
      template.close()
      table_list = self.document.parsed_content.xpath(
                                '//table:table[@table:name="%s"]' % id,
                                namespaces=self.document.parsed_content.nsmap)
      if len(table_list) == 0:
        return None
      table = table_list[0]
      # Next line do this
      # <office:content><office:body><office:text><table:table>
      content_xml[-1][0].append(table)
      # XXX: Next line replace the <office:automatic-styles> tag. This include
      # a lot of unused style tags. Will be better detect the used styles and
      # include only those.
      content_xml.replace(content_xml[-2],
                          self.document.parsed_content[-2])

      odf_document = self._odfWithoutContentXml(format)
      odf_document.writestr('content.xml', etree.tostring(content_xml))
      odf_document_as_string = odf_document.fp
      odf_document.close()
      odf_document_as_string.seek(0)
      return odf_document_as_string.read()
    except Exception, e:
      logger.error(e)
      return None
Esempio n. 2
0
 def convert(self, destination_format):
   """ Convert the inputed file to output as format that were informed """
   # XXX This implementation could use ffmpeg -i pipe:0, but
   # XXX seems super unreliable currently and it generates currupted files in
   # the end
   logger.debug("FfmpegConvert: %s > %s" % (self.input.source_format, destination_format))
   output_url = mktemp(suffix=".%s" % destination_format,
                       dir=self.input.directory_name)
   command = ["ffmpeg",
              "-i",
              self.input.getUrl(),
              "-y",
              output_url]
   # XXX ffmpeg has a bug that needs this options to work with webm format
   if destination_format == "webm":
     command.insert(3, "32k")
     command.insert(3, "-ab")
   try:
     stdout, stderr = Popen(command,
                            stdout=PIPE,
                            stderr=PIPE,
                            close_fds=True,
                            env=self.environment).communicate()
     self.input.reload(output_url)
     if len(self.input.getContent()) == 0:
       logger.error(stderr.split("\n")[-2])
     return self.input.getContent()
   finally:
     self.input.trash()
Esempio n. 3
0
 def run_convert(self,
                 filename='',
                 data=None,
                 meta=None,
                 extension=None,
                 orig_format=None):
     """Method to support the old API. Wrapper getFileMetadataItemList but
 returns a dict.
 This is a Backwards compatibility provided for ERP5 Project, in order to
 keep compatibility with OpenOffice.org Daemon.
 """
     if not extension:
         extension = filename.split('.')[-1]
     try:
         response_dict = {}
         metadata_dict = self.getFileMetadataItemList(data,
                                                      extension,
                                                      base_document=True)
         response_dict['meta'] = metadata_dict
         # XXX - Backward compatibility: Previous API expects 'mime' now we
         # use 'MIMEType'
         response_dict['meta']['mime'] = response_dict['meta']['MIMEType']
         response_dict['data'] = response_dict['meta']['Data']
         response_dict['mime'] = response_dict['meta']['MIMEType']
         del response_dict['meta']['Data']
         return (200, response_dict, "")
     except Exception, e:
         import traceback
         traceback.print_exc()
         logger.error(e)
         return (402, {}, e.args[0])
Esempio n. 4
0
 def run_convert(self, filename='', data=None, meta=None, extension=None,
                 orig_format=None):
   """Method to support the old API. Wrapper getFileMetadataItemList but
   returns a dict.
   This is a Backwards compatibility provided for ERP5 Project, in order to
   keep compatibility with OpenOffice.org Daemon.
   """
   if not extension:
     extension = filename.split('.')[-1]
   try:
     response_dict = {}
     metadata_dict = self.getFileMetadataItemList(data, extension,
                                                  base_document=True)
     response_dict['meta'] = metadata_dict
     # XXX - Backward compatibility: Previous API expects 'mime' now we
     # use 'MIMEType'
     response_dict['meta']['mime'] = response_dict['meta']['MIMEType']
     response_dict['data'] = response_dict['meta']['Data']
     response_dict['mime'] = response_dict['meta']['MIMEType']
     del response_dict['meta']['Data']
     return (200, response_dict, "")
   except Exception, e:
     import traceback; traceback.print_exc()
     logger.error(e)
     return (402, {}, e.args[0])
Esempio n. 5
0
 def getChapterItem(self, chapter_id):
   """Return the chapter in the form of (title, level)."""
   chapter_list = self._getChapterList()
   try:
     chapter = chapter_list[chapter_id].encode('utf-8')
     return [chapter, chapter_id]
   except IndexError:
     msg = "Unable to find chapter %s at chapter list." % chapter_id
     logger.error(msg)
     return None
Esempio n. 6
0
 def getAllowedTargetItemList(self, content_type):
   """Wrapper getAllowedExtensionList but returns a dict.
   This is a Backwards compatibility provided for ERP5 Project, in order to
   keep compatibility with OpenOffice.org Daemon.
   """
   response_dict = {}
   try:
     extension_list = self.getAllowedExtensionList({"mimetype": content_type})
     response_dict['response_data'] = extension_list
     return (200, response_dict, '')
   except Exception, e:
     logger.error(e)
     return (402, {}, e.args[0])
Esempio n. 7
0
 def getAllowedTargetItemList(self, content_type):
     """Wrapper getAllowedExtensionList but returns a dict.
 This is a Backwards compatibility provided for ERP5 Project, in order to
 keep compatibility with OpenOffice.org Daemon.
 """
     response_dict = {}
     try:
         extension_list = self.getAllowedExtensionList(
             {"mimetype": content_type})
         response_dict['response_data'] = extension_list
         return (200, response_dict, '')
     except Exception, e:
         logger.error(e)
         return (402, {}, e.args[0])
Esempio n. 8
0
 def run_setmetadata(self, filename='', data=None, meta=None,
                     extension=None, orig_format=None):
   """Wrapper updateFileMetadata but returns a dict.
   This is a Backwards compatibility provided for ERP5 Project, in order to
   keep compatibility with OpenOffice.org Daemon.
   """
   if not extension:
     extension = filename.split('.')[-1]
   response_dict = {}
   try:
     response_dict['data'] = self.updateFileMetadata(data, extension, meta)
     return (200, response_dict, '')
   except Exception, e:
     logger.error(e)
     return (402, {}, e.args[0])
Esempio n. 9
0
 def getAllowedTargetItemList(self, content_type):
   """Wrapper getAllowedExtensionList but returns a dict.
   This is a Backwards compatibility provided for ERP5 Project, in order to
   keep compatibility with OpenOffice.org Daemon.
   """
   response_dict = {}
   try:
     mimetype_list = self.getAllowedConversionFormatList(content_type)
     extension_list = []
     for m, t in mimetype_list:
       ext = BBB_guess_extension(m, t)
       if ext:
         extension_list.append((ext.lstrip('.'), t))
     response_dict['response_data'] = extension_list
     return (200, response_dict, '')
   except Exception, e:
     logger.error(e)
     return (402, {}, e.args[0])
Esempio n. 10
0
 def run_getmetadata(self, filename='', data=None, meta=None,
                     extension=None, orig_format=None):
   """Wrapper for getFileMetadataItemList.
   This is a Backwards compatibility provided for ERP5 Project, in order to
   keep compatibility with OpenOffice.org Daemon.
   """
   if not extension:
     extension = filename.split('.')[-1]
   response_dict = {}
   try:
     response_dict['meta'] = self.getFileMetadataItemList(data, extension)
     # XXX - Backward compatibility: Previous API expects 'title' now
     # we use 'Title'"
     response_dict['meta']['title'] = response_dict['meta']['Title']
     return (200, response_dict, '')
   except Exception, e:
     logger.error(e)
     return (402, {}, e.args[0])
Esempio n. 11
0
 def getAllowedTargetItemList(self, content_type):
     """Wrapper getAllowedExtensionList but returns a dict.
 This is a Backwards compatibility provided for ERP5 Project, in order to
 keep compatibility with OpenOffice.org Daemon.
 """
     response_dict = {}
     try:
         mimetype_list = self.getAllowedConversionFormatList(content_type)
         extension_list = []
         for m, t in mimetype_list:
             ext = BBB_guess_extension(m, t)
             if ext:
                 extension_list.append((ext.lstrip('.'), t))
         response_dict['response_data'] = extension_list
         return (200, response_dict, '')
     except Exception, e:
         logger.error(e)
         return (402, {}, e.args[0])
Esempio n. 12
0
  def getParagraph(self, paragraph_id):
    """Returns the paragraph in the form of (text, class)."""
    relevant_paragraph_list = self._getRelevantParagraphList()
    try:
      paragraph = relevant_paragraph_list[paragraph_id]
    except IndexError:
      msg = "Unable to find paragraph %s at paragraph list." % paragraph_id
      logger.error(msg)
      return None

    text = ''.join(paragraph.itertext())

    if TEXT_ATTRIB_STYLENAME not in paragraph.attrib.keys():
      logger.error("Unable to find %s attribute at paragraph %s " % \
                              (TEXT_ATTRIB_STYLENAME, paragraph_id))
      return None

    p_class = paragraph.attrib[TEXT_ATTRIB_STYLENAME]
    return (text, p_class)
Esempio n. 13
0
 def run_setmetadata(self,
                     filename='',
                     data=None,
                     meta=None,
                     extension=None,
                     orig_format=None):
     """Wrapper updateFileMetadata but returns a dict.
 This is a Backwards compatibility provided for ERP5 Project, in order to
 keep compatibility with OpenOffice.org Daemon.
 """
     if not extension:
         extension = filename.split('.')[-1]
     response_dict = {}
     try:
         response_dict['data'] = self.updateFileMetadata(
             data, extension, meta)
         return (200, response_dict, '')
     except Exception, e:
         logger.error(e)
         return (402, {}, e.args[0])
Esempio n. 14
0
  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 stderr:
      first_error = stderr
      logger.error(stderr)
      self.document.restoreOriginal()
      openoffice.restart()
      kw['document_url'] = self.document.getUrl()
      command = self._getCommand(*feature_list, **kw)
      stdout, stderr = self._subprocess(command)
      if not stdout and stderr:
        second_error = "\nerror of the second run: " + stderr
        logger.error(second_error)
        raise Exception(first_error + second_error)

    return stdout, stderr
Esempio n. 15
0
 def run_generate(self,
                  filename='',
                  data=None,
                  meta=None,
                  extension=None,
                  orig_format=''):
     """Wrapper convertFile but returns a dict which includes mimetype.
 This is a Backwards compatibility provided for ERP5 Project, in order
 to keep compatibility with OpenOffice.org Daemon.
 """
     # calculate original extension required by convertFile from
     # given content_type (orig_format)
     original_extension = BBB_guess_extension(orig_format).strip('.')
     # XXX - ugly way to remove "/" and "."
     orig_format = orig_format.split('.')[-1]
     orig_format = orig_format.split('/')[-1]
     if "htm" in extension:
         zip = True
     else:
         zip = False
     try:
         response_dict = {}
         # XXX - use html format instead of xhtml
         if orig_format in ("presentation", "graphics", "spreadsheet",
                            'text') and extension == "xhtml":
             extension = 'html'
         response_dict['data'] = self.convertFile(data, original_extension,
                                                  extension, zip)
         # FIXME: Fast solution to obtain the html or pdf mimetypes
         if zip:
             response_dict['mime'] = "application/zip"
         elif extension == 'xhtml':
             response_dict['mime'] = "text/html"
         else:
             response_dict['mime'] = mimetypes.types_map.get(
                 '.%s' % extension,
                 mimetypes.types_map.get('.%s' % extension.split('.')[-1]))
         return (200, response_dict, "")
     except Exception, e:
         logger.error(e)
         return (402, response_dict, str(e))
Esempio n. 16
0
 def run_generate(self, filename='', data=None, meta=None, extension=None,
                  orig_format=''):
   """Wrapper convertFile but returns a dict which includes mimetype.
   This is a Backwards compatibility provided for ERP5 Project, in order
   to keep compatibility with OpenOffice.org Daemon.
   """
   # calculate original extension required by convertFile from
   # given content_type (orig_format)
   original_extension = BBB_guess_extension(orig_format).strip('.')
   # XXX - ugly way to remove "/" and "."
   orig_format = orig_format.split('.')[-1]
   orig_format = orig_format.split('/')[-1]
   if "htm" in extension:
     zip = True
   else:
     zip = False
   try:
     response_dict = {}
     # XXX - use html format instead of xhtml
     if orig_format in ("presentation",
                        "graphics",
                        "spreadsheet",
                        'text') and extension == "xhtml":
       extension = 'html'
     response_dict['data'] = self.convertFile(data,
                                              original_extension,
                                              extension,
                                              zip)
     # FIXME: Fast solution to obtain the html or pdf mimetypes
     if zip:
       response_dict['mime'] = "application/zip"
     elif extension == 'xhtml':
       response_dict['mime'] = "text/html"
     else:
       response_dict['mime'] = mimetypes.types_map.get('.%s' % extension,
           mimetypes.types_map.get('.%s' % extension.split('.')[-1]))
     return (200, response_dict, "")
   except Exception, e:
     import traceback;
     logger.error(traceback.format_exc())
     return (402, response_dict, str(e))
Esempio n. 17
0
 def run_getmetadata(self,
                     filename='',
                     data=None,
                     meta=None,
                     extension=None,
                     orig_format=None):
     """Wrapper for getFileMetadataItemList.
 This is a Backwards compatibility provided for ERP5 Project, in order to
 keep compatibility with OpenOffice.org Daemon.
 """
     if not extension:
         extension = filename.split('.')[-1]
     response_dict = {}
     try:
         response_dict['meta'] = self.getFileMetadataItemList(
             data, extension)
         # XXX - Backward compatibility: Previous API expects 'title' now
         # we use 'Title'"
         response_dict['meta']['title'] = response_dict['meta']['Title']
         return (200, response_dict, '')
     except Exception, e:
         logger.error(e)
         return (402, {}, e.args[0])
Esempio n. 18
0
def removeDirectory(path):
  """Remove directory"""
  try:
    rmtree(path)
  except OSError, msg:
    logger.error(msg)
Esempio n. 19
0
class OpenOffice(Application):
  """Object to control one OOo Instance and all features instance."""

  implements(ILockable)

  name = "openoffice"

  def __init__(self):
    """Creates the variable to save the pid, port and hostname of the object.
    The lock is a simple lock python that is used when one requisition is
    using the openoffice.
    """
    self._bin_soffice = 'soffice.bin'
    self._lock = Lock()
    self._cleanRequest()

  def _testOpenOffice(self, host, port):
    """Test if OpenOffice was started correctly"""
    logger.debug("Test OpenOffice %s - Pid %s" % (self.getAddress()[-1],
                                                  self.pid()))
    python = join(self.office_binary_path, "python")
    args = [exists(python) and python or "python",
            pkg_resources.resource_filename("cloudooo",
                                      join('handler', 'ooo',
                                           "helper", "openoffice_tester.py")),
            "--hostname=%s" % host,
            "--port=%s" % port,
            "--uno_path=%s" % self.uno_path]
    logger.debug("Testing Openoffice Instance %s" % port)
    stdout, stderr = Popen(args, stdout=PIPE,
        stderr=PIPE, close_fds=True).communicate()
    stdout_bool = convertStringToBool(stdout.replace("\n", ""))
    if stdout_bool and stderr != "":
      logger.debug("%s\n%s" % (stderr, stdout))
      return False
    else:
      logger.debug("Instance %s works" % port)
      return True

  def _cleanRequest(self):
    """Define request attribute as 0"""
    self.request = 0

  def loadSettings(self, hostname, port, path_run_dir,
                   office_binary_path, uno_path, default_language,
                   environment_dict=None, **kw):
    """Method to load the configuratio to control one OpenOffice Instance
    Keyword arguments:
    office_path -- Full Path of the OOo executable.
      e.g office_binary_path='/opt/openoffice.org3/program'
    uno_path -- Full path of the Uno Library
    """
    if environment_dict is None:
      environment_dict = {}
    Application.loadSettings(self, hostname, port, path_run_dir)
    self.office_binary_path = office_binary_path
    self.uno_path = uno_path
    self.default_language = default_language
    self.environment_dict = environment_dict

  def _startProcess(self, command, env):
    """Start OpenOffice.org process"""
    for i in range(5):
      self.stop()
      waitStopDaemon(self, self.timeout)
      self.process = Popen(command,
                           close_fds=True,
                           env=env)
      if not waitStartDaemon(self, self.timeout):
        continue
      if self._testOpenOffice(self.hostname, self.port):
        return

  def _releaseOpenOfficePort(self):
    for process in psutil.process_iter():
      try:
        if process.exe == join(self.office_binary_path, self._bin_soffice):
          for connection in process.get_connections():
            if connection.status == "LISTEN" and \
                connection.local_address[1] == self.port:
              process.terminate()
      except psutil.error.AccessDenied, e:
        pass
      except TypeError, e:
        # exception to prevent one psutil issue with zombie processes
        logger.debug(e)
      except NotImplementedError, e:
        logger.error("lsof isn't installed on this machine: " + str(e))
Esempio n. 20
0
def removeDirectory(path):
    """Remove directory"""
    try:
        rmtree(path)
    except OSError, msg:
        logger.error(msg)