Example #1
0
 def test_convert_fail_status_ne_zero(self):
     # if something goes wrong, we get some status != 0
     status, result_dir = convert(
         path='NoT-An-ExIsTiNg-PaTH', out_dir=self.tmpdir)
     self.assertTrue(status != 0)
     self.assertEqual([], os.listdir(self.tmpdir))
     return
Example #2
0
 def test_convert_outdir(self, lo_server, tmpdir):
     # the outdir parameter is respected
     path = tmpdir.join("sample.txt")
     path.write("Hi there!\n")
     status, result_dir = convert(out_format="pdf", path=str(path), out_dir=str(tmpdir))
     assert status == 0
     # input and output are in the same dir
     assert sorted(os.listdir(str(tmpdir))) == ["sample.pdf", "sample.txt"]
Example #3
0
 def test_convert_outdir(self, lo_server, tmpdir):
     # the outdir parameter is respected
     path = tmpdir.join('sample.txt')
     path.write('Hi there!\n')
     status, result_dir = convert(
         out_format='pdf', path=str(path), out_dir=str(tmpdir))
     assert status == 0
     # input and output are in the same dir
     assert sorted(os.listdir(str(tmpdir))) == ['sample.pdf', 'sample.txt']
Example #4
0
 def test_simple_conversion_to_html(self, lo_server, tmpdir):
     # we can convert a simple text file to html
     path = tmpdir.join("sample.txt")
     path.write("Hi there!\n")
     status, result_dir = convert(out_format="html", path=str(path))
     assert status == 0
     assert os.listdir(result_dir) == ["sample.html"]
     result_doc = open(os.path.join(result_dir, "sample.html"), "rb").read()
     assert result_doc.startswith(b"<!DOCTYPE ")
     shutil.rmtree(result_dir)  # clean up
     return
Example #5
0
 def test_simple_conversion_to_html(self, lo_server, tmpdir):
     # we can convert a simple text file to html
     path = tmpdir.join('sample.txt')
     path.write('Hi there!\n')
     status, result_dir = convert(out_format='html', path=str(path))
     assert status == 0
     assert os.listdir(result_dir) == ['sample.html']
     result_doc = open(os.path.join(result_dir, 'sample.html'), 'rb').read()
     assert result_doc.startswith(b'<!DOCTYPE ')
     shutil.rmtree(result_dir)  # clean up
     return
Example #6
0
 def test_convert_outdir(self):
     # the outdir parameter is respected
     path = os.path.join(self.tmpdir, 'sample.txt')
     open(path, 'w').write('Hi there!\n')
     status, result_dir = convert(
         out_format='pdf', path=path, out_dir=self.tmpdir)
     self.assertEqual(0, status)
     # input and output are in the same dir
     self.assertEqual(
         ['sample.pdf', 'sample.txt'], sorted(os.listdir(self.tmpdir)))
     return
Example #7
0
 def test_convert_with_template(self, lo_server, tmpdir):
     # we can pass in templates when converting
     doc_path = tmpdir.join("sample.txt")
     doc_path.write("Hi there!\n")
     template_path = os.path.join(os.path.dirname(__file__), "input", "sample.ott")
     # convert with template applied
     status, result_dir = convert(out_format="html", path=str(doc_path), out_dir=str(tmpdir), template=template_path)
     content = tmpdir.join("sample.html").read()
     # tags that do not appear in un-templated docs
     assert '<pre class="western">' in content.lower()
     assert ("<DIV TYPE=HEADER>" in content) or ('<div title="header"' in content)
Example #8
0
 def test_simple_conversion_to_html(self):
     # we can convert a simple text file to html
     path = os.path.join(self.tmpdir, 'sample.txt')
     open(path, 'w').write('Hi there!\n')
     status, result_dir = convert(out_format='html', path=path)
     self.assertEqual(0, status)
     self.assertEqual(
        ['sample.html'], os.listdir(result_dir))
     result_doc = open(os.path.join(result_dir, 'sample.html'), 'rb').read()
     self.assertEqual(result_doc[:10], b'<!DOCTYPE ')
     shutil.rmtree(result_dir)  # clean up
     return
Example #9
0
 def test_convert_with_template(self, lo_server, tmpdir):
     # we can pass in templates when converting
     doc_path = tmpdir.join('sample.txt')
     doc_path.write('Hi there!\n')
     template_path = os.path.join(
         os.path.dirname(__file__), 'input', 'sample.ott')
     # convert with template applied
     status, result_dir = convert(
         out_format='html', path=str(doc_path), out_dir=str(tmpdir),
         template=template_path)
     content = tmpdir.join('sample.html').read()
     # tags that do not appear in un-templated docs
     assert '<pre class="western">' in content.lower()
     assert (
         '<DIV TYPE=HEADER>' in content) or (
         '<div title="header"' in content)
Example #10
0
 def test_convert_with_template(self):
     # we can pass in templates when converting
     doc_path = os.path.join(self.tmpdir, 'sample.txt')
     template_path = os.path.join(
         os.path.dirname(__file__), 'input', 'sample.ott')
     open(doc_path, 'w').write('Hi there!\n')
     result_path = os.path.join(self.tmpdir, 'sample.html')
     # convert with template applied
     status, result_dir = convert(
         out_format='html', path=doc_path, out_dir=self.tmpdir,
         template=template_path)
     content = open(result_path, 'rb').read()
     # tags that do not appear in un-templated docs
     assert '<PRE CLASS="western">' in content
     assert '<DIV TYPE=HEADER>' in content
     return
Example #11
0
    def process(self, path, metadata):
        basename = os.path.basename(path)
        src = os.path.join(
            copy_to_secure_location(path), basename)
        if os.path.isfile(path):
            path = os.path.dirname(path)
        shutil.rmtree(path)
        extension = self.options['oocp_output_format']
        filter_name = self.formats[extension]
        url = 'socket,host=%s,port=%d;urp;StarOffice.ComponentContext' % (
            self.options['oocp_hostname'], self.options['oocp_port'])

        filter_props = self._get_filter_props()
        status, result_path = convert(
            url=url,
            out_format=filter_name,
            filter_props=filter_props,
            path=src,
            out_dir=os.path.dirname(src),
            )
        metadata['oocp_status'] = status
        if status != 0:
            metadata['error'] = True
            metadata['error-descr'] = 'conversion problem'
            if os.path.isfile(src):
                src = os.path.dirname(src)
            shutil.rmtree(src)
            return None, metadata
        if extension == 'xhtml':
            extension = 'html'
        result_path = '%s.%s' % (os.path.splitext(src)[0], extension)

        # Remove input file if different from output
        if os.path.exists(src):
            if os.path.basename(result_path) != basename:
                os.unlink(src)
        return result_path, metadata
Example #12
0
    def process(self, path, metadata):
        basename = os.path.basename(path)
        src = os.path.join(copy_to_secure_location(path), basename)
        if os.path.isfile(path):
            path = os.path.dirname(path)
        shutil.rmtree(path)
        extension = self.options['oocp_output_format']
        filter_name = self.formats[extension]
        url = 'socket,host=%s,port=%d;urp;StarOffice.ComponentContext' % (
            self.options['oocp_hostname'], self.options['oocp_port'])

        filter_props = self._get_filter_props()
        status, result_path = convert(
            url=url,
            out_format=filter_name,
            filter_props=filter_props,
            path=src,
            out_dir=os.path.dirname(src),
        )
        metadata['oocp_status'] = status
        if status != 0:
            metadata['error'] = True
            metadata['error-descr'] = 'conversion problem'
            if os.path.isfile(src):
                src = os.path.dirname(src)
            shutil.rmtree(src)
            return None, metadata
        if extension == 'xhtml':
            extension = 'html'
        result_path = '%s.%s' % (os.path.splitext(src)[0], extension)

        # Remove input file if different from output
        if os.path.exists(src):
            if os.path.basename(result_path) != basename:
                os.unlink(src)
        return result_path, metadata
Example #13
0
 def test_convert_no_path(self, lo_server):
     # w/o a path we get no conversion
     assert (None, None) == convert()
Example #14
0
 def test_convert_no_path(self, lo_server):
     # w/o a path we get no conversion
     assert (None, None) == convert()
Example #15
0
            if dest_path is not None:
                # TODO: Here we might unpack stuff, copy to secure location etc.
                dest_path = self.prepareCacheResults(path, dest_path, extension)
                self.wfile.write('OK 200 %s' % (dest_path,))
                logger.info('200 OK. FOUND in cache. request completed')
                logger.debug('result in %s' % dest_path)
                return
        logger.debug('doc NOT FOUND in cache, start conversion.')

        ret_val = -1
        dest_path = ''
        # Copy source to safe destination...
        path = self.prepareSource(path, extension)
        path_dir = os.path.dirname(path)
        try:
            (ret_val, dest_paths) = convert(
                filter_name=filter_name, extension=extension, paths=[path])
            if len(dest_paths):
                dest_path = urlsplit(dest_paths[0])[2]
        except Exception, e:
            self.wfile.write('ERR 550 %s: %s\n' % (e.__class__, e.message))
            shutil.rmtree(path_dir)
            logger.warn('550 ERR conversion failed %s: %s' %(
                    e.__class__, e.message))
            return
        except:
            self.wfile.write('ERR 550 internal pyuno error \n')
            logger.warn('550 ERR internal pyuno error')
        if ret_val != 0:
            self.wfile.write('ERR 550 conversion not finished: %s' % (
                    ret_val,))
            logger.warn('550 ERR conversion not finished: %s' % (
Example #16
0
 def test_convert_fail_status_ne_zero(self, lo_server, tmpdir):
     # if something goes wrong, we get some status != 0
     status, result_dir = convert(path="NoT-An-ExIsTiNg-PaTH", out_dir=str(tmpdir))
     assert status != 0
     assert os.listdir(str(tmpdir)) == []
Example #17
0
 def test_convert_fail_status_ne_zero(self, lo_server, tmpdir):
     # if something goes wrong, we get some status != 0
     status, result_dir = convert(
         path='NoT-An-ExIsTiNg-PaTH', out_dir=str(tmpdir))
     assert status != 0
     assert os.listdir(str(tmpdir)) == []
Example #18
0
 def test_convert_no_path(self):
     # w/o a path we get no conversion
     self.assertEqual((None, None), convert())
     return