Exemplo n.º 1
0
def convert_hwp5file_into_odtpkg(hwp5file):
    from tempfile import TemporaryFile
    tmpfile = TemporaryFile()
    import os
    tmpfile2 = os.fdopen( os.dup(tmpfile.fileno()), 'r')

    from zipfile import ZipFile
    zf = ZipFile(tmpfile, 'w')
    from hwp5.hwp5odt import ODTPackage
    odtpkg = ODTPackage(zf)
    try:
        from hwp5.hwp5odt import Converter
        import hwp5.plat

        if haveXSLTTransformer():
            xslt = xslt_with_libreoffice
        else:
            # we use default xslt
            xslt = hwp5.plat.get_xslt()

        # convert without RelaxNG validation
        convert = Converter(xslt)

        # Embed images: see #32 - https://github.com/mete0r/pyhwp/issues/32
        convert(hwp5file, odtpkg, embedimage=True)
    finally:
        odtpkg.close()

    tmpfile2.seek(0)
    odtpkg_stream = InputStreamFromFileLike(tmpfile2)
    odtpkg_storage = StorageFromInputStream(odtpkg_stream)
    return odtpkg_storage
Exemplo n.º 2
0
def convert_hwp5file_into_odtpkg(hwp5file):
    from tempfile import TemporaryFile
    tmpfile = TemporaryFile()
    import os
    tmpfile2 = os.fdopen(os.dup(tmpfile.fileno()), 'r')

    from zipfile import ZipFile
    zf = ZipFile(tmpfile, 'w')
    from hwp5.hwp5odt import ODTPackage
    odtpkg = ODTPackage(zf)
    try:
        from hwp5.hwp5odt import Converter
        import hwp5.plat

        if haveXSLTTransformer():
            xslt = xslt_with_libreoffice
        else:
            # we use default xslt
            xslt = hwp5.plat.get_xslt()

        # convert without RelaxNG validation
        convert = Converter(xslt)

        # Embed images: see #32 - https://github.com/mete0r/pyhwp/issues/32
        convert(hwp5file, odtpkg, embedimage=True)
    finally:
        odtpkg.close()

    tmpfile2.seek(0)
    odtpkg_stream = InputStreamFromFileLike(tmpfile2)
    odtpkg_storage = StorageFromInputStream(odtpkg_stream)
    return odtpkg_storage
Exemplo n.º 3
0
    def test_basic(self):
        import uno
        from unokit.adapters import InputStreamFromFileLike
        from hwp5_uno import StorageFromInputStream
        from hwp5.hwp5odt import ODTPackage

        zipname = self.id()+'.zip'

        pkg = ODTPackage(zipname)
        try:
            from StringIO import StringIO
            data = StringIO('hello')
            pkg.insert_stream(data, 'abc.txt', 'text/plain')
        finally:
            pkg.close()

        with file(zipname, 'rb') as f:
            inputstream = InputStreamFromFileLike(f, dontclose=True)
            storage = StorageFromInputStream(inputstream)
            try:
                self.assertTrue(uno.getTypeByName('com.sun.star.embed.XStorage')
                                in storage.Types)
                self.assertEqual(set(['abc.txt']), set(storage.ElementNames))
            finally:
                storage.dispose()
Exemplo n.º 4
0
    def test_convert_bindata(self):
        from hwp5.hwp5odt import ODTPackage

        hwp5file = example('sample-5017.hwp')
        try:
            f = hwp5file['BinData']['BIN0002.jpg'].open()
            try:
                data1 = f.read()
            finally:
                f.close()

            odtpkg = ODTPackage(self.odt_path)
            try:
                self.convert(hwp5file, odtpkg)
            finally:
                odtpkg.close()
        finally:
            hwp5file.close()

        from zipfile import ZipFile
        zf = ZipFile(self.odt_path)
        data2 = zf.read('bindata/BIN0002.jpg')

        self.assertEquals(data1, data2)