Esempio n. 1
0
 def f(conn):
     with Office(lo_path) as lo:
         conv_file = NamedTemporaryFile(delete=False,
                                        suffix='.%s' % output_format)
         with lo.documentLoad(str(dest_file.name)) as doc:
             doc.saveAs(conv_file.name)
         os.unlink(dest_file.name)
         conn.send(conv_file.name)
         conn.close()
Esempio n. 2
0
 def test_multiple_calls(self):
     with Office(self.lo_path) as lo:
         with lo.documentLoad(self.test_doc) as doc:
             doc.saveAs(self.test_out_docx, fmt="docx", options="SkipImages")
             doc.saveAs(self.test_out_pdf)
             os.unlink(self.test_out_docx)
             os.unlink(self.test_out_pdf)
         with lo.documentLoad(self.test_doc) as doc:
             doc.saveAs(self.test_out_pdf)
             os.unlink(self.test_out_pdf)
Esempio n. 3
0
def _convert_subprocess(filename, format, result_queue):
    """
    Subprocess helper to convert a file via LOKit.

    We need it until LO doesn't crash randomly after conversion, terminating
    the calling process as well.
    """
    lo_path = getattr(settings, 'TEMPLATED_DOCS_LIBREOFFICE_PATH',
                      '/usr/lib/libreoffice/program/')

    with Office(lo_path) as lo:
        conv_file = NamedTemporaryFile(delete=False, suffix='.%s' % format)
        with lo.documentLoad(filename) as doc:
            doc.saveAs(str(conv_file.name))
        os.unlink(filename)
    result_queue.put(conv_file.name)
Esempio n. 4
0
def process_document(path, options, meta):
    current_task = get_current_job()
    with Office(app.config["LIBREOFFICE_PATH"]
                ) as office:  # acquire libreoffice lock
        with office.documentLoad(
                path) as original_document:  # open original document
            with TemporaryDirectory(
            ) as tmp_dir:  # create temp dir where output'll be stored

                # make formats, mainly PDF
                for fmt in options[
                        "formats"]:  # iterate over requested formats
                    current_format = app.config["SUPPORTED_FORMATS"][fmt]
                    name = current_task.id
                    output_path = os.path.join(tmp_dir, name)
                    original_document.saveAs(output_path,
                                             fmt=current_format["fmt"])

                # make thumbnails
                if options.get("thumbnails", None):
                    is_created = False
                    if meta["mimetype"] == "application/pdf":
                        pdf_path = path
                    elif "pdf" in options["formats"]:
                        pdf_path = os.path.join(tmp_dir, "pdf")
                    else:
                        pdf_tmp_file = NamedTemporaryFile()
                        pdf_path = pdf_tmp_file.name
                        original_document.saveAs(pdf_tmp_file.name, fmt="pdf")
                        is_created = True
                    image = Image(filename=pdf_path,
                                  resolution=app.config["THUMBNAILS_DPI"])
                    if is_created:
                        pdf_tmp_file.close()
                    thumbnails = make_thumbnails(image, tmp_dir,
                                                 options["thumbnails"]["size"])

                # make zip
                result_path, result_url = make_zip_archive(
                    current_task.id, tmp_dir)

        remove_file.schedule(
            datetime.timedelta(seconds=app.config["RESULT_FILE_TTL"]),
            result_path)
    return result_url
Esempio n. 5
0
def _convert_file(filename, format, result_queue=None, options=None):
    """Helper function to convert a file via LOKit.

    This function can be called via subprocess so that in the event of LO
    crashing randomly after conversion, it will not terminate the parent
    process. This is assisted by inserting the result into a Queue object.
    """
    lo_path = getattr(settings, 'TEMPLATED_DOCS_LIBREOFFICE_PATH',
                      '/usr/lib/libreoffice/program/')

    with Office(lo_path) as lo:
        conv_file = NamedTemporaryFile(delete=False, suffix='.%s' % format)
        with lo.documentLoad(filename) as doc:
            doc.saveAs(str(conv_file.name), options=options)
        os.unlink(filename)

    if result_queue is not None:
        result_queue.put(conv_file.name)
    else:
        return conv_file.name
Esempio n. 6
0
 def test_filter_and_options(self):
     with Office(self.lo_path) as lo:
         with lo.documentLoad(self.test_doc) as doc:
             doc.saveAs(self.test_out_docx, fmt="docx", options="SkipImages")
             os.unlink(self.test_out_docx)
Esempio n. 7
0
 def test_wrong_options(self):
     # TODO: 4.4 used to fail, need to investigate what's going on
     with Office(self.lo_path) as lo:
         with lo.documentLoad(self.test_doc) as doc:
             self.assertRaises(LoKitExportError, doc.saveAs, self.test_out_rtf, options="foobar")
Esempio n. 8
0
 def test_wrong_filter(self):
     with Office(self.lo_path) as lo:
         with lo.documentLoad(self.test_doc) as doc:
             self.assertRaises(LoKitExportError, doc.saveAs, self.test_out_rtf, fmt="foobar")
Esempio n. 9
0
 def test_no_output_file(self):
     with Office(self.lo_path) as lo:
         with lo.documentLoad(self.test_doc) as doc:
             self.assertRaises(LoKitExportError, doc.saveAs, "")
Esempio n. 10
0
 def test_no_input_file(self):
     with Office(self.lo_path) as lo:
         self.assertRaises(LoKitImportError, lo.documentLoad, "foo")
Esempio n. 11
0
 def test_init(self):
     with Office(self.lo_path) as lo:
         self.assertIsNotNone(lo)