Example #1
0
 def test2(self):
     x = Expression([[1, 2], 3, [4, 5, Symbol('baz')], ['quux']])
     assert_repr(
         x, "Expression([[1, 2], 3, [4, 5, Symbol('baz')], ['quux']])")
     y = Expression(x)
     assert_repr(y, repr(x))
     assert_false(x is y)
     assert_equal(x.value, ((1, 2), 3, (4, 5, Symbol('baz')), ('quux', )))
     assert_equal(x.lvalue, [[1, 2], 3, [4, 5, Symbol('baz')], ['quux']])
     assert_equal(str(x), '((1 2) 3 (4 5 baz) ("quux"))')
     assert_repr(x, repr(Expression.from_string(str(x))))
     assert_equal(len(x), 4)
     assert_equal(bool(x), True)
     assert_equal(tuple(x), (Expression(
         (1, 2)), Expression(3), Expression(
             (4, 5, Symbol('baz'))), Expression(('quux', ))))
     with assert_raises_str(TypeError, 'key must be an integer or a slice'):
         x[object()]
     assert_equal(x[1], Expression(3))
     assert_equal(x[-1][0], Expression('quux'))
     with assert_raises_str(IndexError, 'list index of out range'):
         x[6]
     with assert_raises_str(IndexError, 'list index of out range'):
         x[-6]
     assert_equal(x[:].value, x.value)
     assert_equal(x[:].lvalue, x.lvalue)
     assert_repr(x[1:], "Expression([3, [4, 5, Symbol('baz')], ['quux']])")
     assert_repr(x[-2:], "Expression([[4, 5, Symbol('baz')], ['quux']])")
     x[-2:] = 4, 5, 6
     assert_repr(x, 'Expression([[1, 2], 3, 4, 5, 6])')
     x[0] = 2
     assert_repr(x, 'Expression([2, 3, 4, 5, 6])')
     x[:] = (1, 3, 5)
     assert_repr(x, 'Expression([1, 3, 5])')
     x[3:] = 7,
     assert_repr(x, 'Expression([1, 3, 5, 7])')
     with assert_raises_str(NotImplementedError,
                            'only [n:] slices are supported'):
         x[object():]
     with assert_raises_str(NotImplementedError,
                            'only [n:] slices are supported'):
         x[:2]
     with assert_raises_str(NotImplementedError,
                            'only [n:] slices are supported'):
         x[object():] = []
     with assert_raises_str(NotImplementedError,
                            'only [n:] slices are supported'):
         x[:2] = []
     with assert_raises_str(TypeError, 'can only assign a list expression'):
         x[:] = 0
     assert_equal(x, Expression((1, 3, 5, 7)))
     assert_not_equal(x, Expression((2, 4, 6)))
     assert_not_equal(x, (1, 3, 5, 7))
     with assert_raises_str(TypeError, "unhashable type: 'ListExpression'"):
         hash(x)
Example #2
0
    def test_decode(self):
        context = Context()
        document = context.new_document(FileUri(images + 'test1.djvu'))
        message = document.get_message()
        assert_equal(type(message), DocInfoMessage)
        page_job = document.pages[0].decode()
        assert_true(page_job.is_done)
        assert_equal(type(page_job), PageJob)
        assert_true(page_job.is_done)
        assert_false(page_job.is_error)
        assert_equal(page_job.status, JobOK)
        assert_equal(page_job.width, 64)
        assert_equal(page_job.height, 48)
        assert_equal(page_job.size, (64, 48))
        assert_equal(page_job.dpi, 300)
        assert_equal(page_job.gamma, 2.2)
        assert_equal(page_job.version, 24)
        assert_equal(page_job.type, PAGE_TYPE_BITONAL)
        assert_equal((page_job.rotation, page_job.initial_rotation), (0, 0))
        with assert_raises_str(ValueError, 'rotation must be equal to 0, 90, 180, or 270'):
            page_job.rotation = 100
        page_job.rotation = 180
        assert_equal((page_job.rotation, page_job.initial_rotation), (180, 0))
        del page_job.rotation
        assert_equal((page_job.rotation, page_job.initial_rotation), (0, 0))

        with assert_raises_str(ValueError, 'page_rect width/height must be a positive integer'):
            page_job.render(RENDER_COLOR, (0, 0, -1, -1), (0, 0, 10, 10), PixelFormatRgb())

        with assert_raises_str(ValueError, 'render_rect width/height must be a positive integer'):
            page_job.render(RENDER_COLOR, (0, 0, 10, 10), (0, 0, -1, -1), PixelFormatRgb())

        with assert_raises_str(ValueError, 'render_rect must be inside page_rect'):
            page_job.render(RENDER_COLOR, (0, 0, 10, 10), (2, 2, 10, 10), PixelFormatRgb())

        with assert_raises_str(ValueError, 'row_alignment must be a positive integer'):
            page_job.render(RENDER_COLOR, (0, 0, 10, 10), (0, 0, 10, 10), PixelFormatRgb(), -1)

        with assert_raises_regex(MemoryError, r'\AUnable to allocate [0-9]+ bytes for an image memory\Z'):
            x = int((sys.maxsize // 2) ** 0.5)
            page_job.render(RENDER_COLOR, (0, 0, x, x), (0, 0, x, x), PixelFormatRgb(), 8)

        s = page_job.render(RENDER_COLOR, (0, 0, 10, 10), (0, 0, 4, 4), PixelFormatGrey(), 1)
        assert_equal(s, b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xEF\xFF\xFF\xFF\xA4\xFF\xFF\xFF\xB8')

        buffer = array.array('B', b'\0')
        with assert_raises_str(ValueError, 'Image buffer is too small (16 > 1)'):
            page_job.render(RENDER_COLOR, (0, 0, 10, 10), (0, 0, 4, 4), PixelFormatGrey(), 1, buffer)

        buffer = array.array('B', b'\0' * 16)
        assert_is(page_job.render(RENDER_COLOR, (0, 0, 10, 10), (0, 0, 4, 4), PixelFormatGrey(), 1, buffer), buffer)
        s = array_tobytes(buffer)
        assert_equal(s, b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xEF\xFF\xFF\xFF\xA4\xFF\xFF\xFF\xB8')
Example #3
0
 def test2(self):
     x = Expression([[1, 2], 3, [4, 5, Symbol('baz')], ['quux']])
     assert_repr(x, "Expression([[1, 2], 3, [4, 5, Symbol('baz')], ['quux']])")
     y = Expression(x)
     assert_repr(y, repr(x))
     assert_false(x is y)
     assert_equal(x.value, ((1, 2), 3, (4, 5, Symbol('baz')), ('quux',)))
     assert_equal(x.lvalue, [[1, 2], 3, [4, 5, Symbol('baz')], ['quux']])
     assert_equal(str(x), '((1 2) 3 (4 5 baz) ("quux"))')
     assert_repr(x, repr(Expression.from_string(str(x))))
     assert_equal(len(x), 4)
     assert_equal(bool(x), True)
     assert_equal(tuple(x), (Expression((1, 2)), Expression(3), Expression((4, 5, Symbol('baz'))), Expression(('quux',))))
     with assert_raises_str(TypeError, 'key must be an integer or a slice'):
         x[object()]
     assert_equal(x[1], Expression(3))
     assert_equal(x[-1][0], Expression('quux'))
     with assert_raises_str(IndexError, 'list index of out range'):
         x[6]
     with assert_raises_str(IndexError, 'list index of out range'):
         x[-6]
     assert_equal(x[:].value, x.value)
     assert_equal(x[:].lvalue, x.lvalue)
     assert_repr(x[1:], "Expression([3, [4, 5, Symbol('baz')], ['quux']])")
     assert_repr(x[-2:], "Expression([[4, 5, Symbol('baz')], ['quux']])")
     x[-2:] = 4, 5, 6
     assert_repr(x, 'Expression([[1, 2], 3, 4, 5, 6])')
     x[0] = 2
     assert_repr(x, 'Expression([2, 3, 4, 5, 6])')
     x[:] = (1, 3, 5)
     assert_repr(x, 'Expression([1, 3, 5])')
     x[3:] = 7,
     assert_repr(x, 'Expression([1, 3, 5, 7])')
     with assert_raises_str(NotImplementedError, 'only [n:] slices are supported'):
         x[object():]
     with assert_raises_str(NotImplementedError, 'only [n:] slices are supported'):
         x[:2]
     with assert_raises_str(NotImplementedError, 'only [n:] slices are supported'):
         x[object():] = []
     with assert_raises_str(NotImplementedError, 'only [n:] slices are supported'):
         x[:2] = []
     with assert_raises_str(TypeError, 'can only assign a list expression'):
         x[:] = 0
     assert_equal(x, Expression((1, 3, 5, 7)))
     assert_not_equal(x, Expression((2, 4, 6)))
     assert_not_equal(x, (1, 3, 5, 7))
     with assert_raises_str(TypeError, "unhashable type: 'ListExpression'"):
         hash(x)
Example #4
0
 def test_export_ps(self):
     skip_unless_command_exists('ps2ascii')
     context = Context()
     document = context.new_document(FileUri(images + 'test0.djvu'))
     message = document.get_message()
     assert_equal(type(message), DocInfoMessage)
     assert_true(document.decoding_done)
     assert_false(document.decoding_error)
     assert_equal(document.decoding_status, JobOK)
     assert_equal(document.type, DOCUMENT_TYPE_BUNDLED)
     assert_equal(len(document.pages), 2)
     assert_equal(len(document.files), 3)
     with tempfile.NamedTemporaryFile() as tmp:
         job = document.export_ps(tmp.file)
         assert_equal(type(job), SaveJob)
         assert_true(job.is_done)
         assert_false(job.is_error)
         stdout, stderr = run('ps2ascii', tmp.name, LC_ALL='C')
         assert_equal(stderr, b'')
         stdout = re.sub(br'[\x00\s]+', b' ', stdout)
         assert_equal(stdout, b' ')
     with tempfile.NamedTemporaryFile() as tmp:
         job = document.export_ps(tmp.file, pages=(0, ), text=True)
         assert_equal(type(job), SaveJob)
         assert_true(job.is_done)
         assert_false(job.is_error)
         stdout, stderr = run('ps2ascii', tmp.name, LC_ALL='C')
         assert_equal(stderr, b'')
         stdout = stdout.decode('ASCII')
         stdout = re.sub(r'[\x00\s]+', ' ', stdout)
         stdout = ' '.join(stdout.split()[:3])
         expected = '1 Lorem ipsum'
         assert_multi_line_equal(stdout, expected)
Example #5
0
 def test_export_ps(self):
     skip_unless_command_exists('ps2ascii')
     context = Context()
     document = context.new_document(FileUri(images + 'test0.djvu'))
     message = document.get_message()
     assert_equal(type(message), DocInfoMessage)
     assert_true(document.decoding_done)
     assert_false(document.decoding_error)
     assert_equal(document.decoding_status, JobOK)
     assert_equal(document.type, DOCUMENT_TYPE_BUNDLED)
     assert_equal(len(document.pages), 2)
     assert_equal(len(document.files), 3)
     with tempfile.NamedTemporaryFile() as tmp:
         job = document.export_ps(tmp.file)
         assert_equal(type(job), SaveJob)
         assert_true(job.is_done)
         assert_false(job.is_error)
         stdout, stderr = run('ps2ascii', tmp.name, LC_ALL='C')
         assert_equal(stderr, b'')
         stdout = re.sub(br'[\x00\s]+', b' ', stdout)
         assert_equal(stdout, b' ')
     with tempfile.NamedTemporaryFile() as tmp:
         job = document.export_ps(tmp.file, pages=(0,), text=True)
         assert_equal(type(job), SaveJob)
         assert_true(job.is_done)
         assert_false(job.is_error)
         stdout, stderr = run('ps2ascii', tmp.name, LC_ALL='C')
         assert_equal(stderr, b'')
         stdout = stdout.decode('ASCII')
         stdout = re.sub(r'[\x00\s]+', ' ', stdout)
         stdout = ' '.join(stdout.split()[:3])
         expected = '1 Lorem ipsum'
         assert_multi_line_equal(stdout, expected)
Example #6
0
    def test_decode(self):
        context = Context()
        document = context.new_document(FileUri(images + 'test1.djvu'))
        message = document.get_message()
        assert_equal(type(message), DocInfoMessage)
        page_job = document.pages[0].decode()
        assert_true(page_job.is_done)
        assert_equal(type(page_job), PageJob)
        assert_true(page_job.is_done)
        assert_false(page_job.is_error)
        assert_equal(page_job.status, JobOK)
        assert_equal(page_job.width, 64)
        assert_equal(page_job.height, 48)
        assert_equal(page_job.size, (64, 48))
        assert_equal(page_job.dpi, 300)
        assert_equal(page_job.gamma, 2.2)
        assert_equal(page_job.version, 24)
        assert_equal(page_job.type, PAGE_TYPE_BITONAL)
        assert_equal((page_job.rotation, page_job.initial_rotation), (0, 0))
        with assert_raises_str(ValueError,
                               'rotation must be equal to 0, 90, 180, or 270'):
            page_job.rotation = 100
        page_job.rotation = 180
        assert_equal((page_job.rotation, page_job.initial_rotation), (180, 0))
        del page_job.rotation
        assert_equal((page_job.rotation, page_job.initial_rotation), (0, 0))

        with assert_raises_str(
                ValueError,
                'page_rect width/height must be a positive integer'):
            page_job.render(RENDER_COLOR, (0, 0, -1, -1), (0, 0, 10, 10),
                            PixelFormatRgb())

        with assert_raises_str(
                ValueError,
                'render_rect width/height must be a positive integer'):
            page_job.render(RENDER_COLOR, (0, 0, 10, 10), (0, 0, -1, -1),
                            PixelFormatRgb())

        with assert_raises_str(ValueError,
                               'render_rect must be inside page_rect'):
            page_job.render(RENDER_COLOR, (0, 0, 10, 10), (2, 2, 10, 10),
                            PixelFormatRgb())

        with assert_raises_str(ValueError,
                               'row_alignment must be a positive integer'):
            page_job.render(RENDER_COLOR, (0, 0, 10, 10), (0, 0, 10, 10),
                            PixelFormatRgb(), -1)

        with assert_raises_regex(
                MemoryError,
                r'\AUnable to allocate [0-9]+ bytes for an image memory\Z'):
            x = int((sys.maxsize // 2)**0.5)
            page_job.render(RENDER_COLOR, (0, 0, x, x), (0, 0, x, x),
                            PixelFormatRgb(), 8)

        s = page_job.render(RENDER_COLOR, (0, 0, 10, 10), (0, 0, 4, 4),
                            PixelFormatGrey(), 1)
        assert_equal(
            s,
            b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xEF\xFF\xFF\xFF\xA4\xFF\xFF\xFF\xB8'
        )

        buffer = array.array('B', b'\0')
        with assert_raises_str(ValueError,
                               'Image buffer is too small (16 > 1)'):
            page_job.render(RENDER_COLOR, (0, 0, 10, 10), (0, 0, 4, 4),
                            PixelFormatGrey(), 1, buffer)

        buffer = array.array('B', b'\0' * 16)
        assert_is(
            page_job.render(RENDER_COLOR, (0, 0, 10, 10), (0, 0, 4, 4),
                            PixelFormatGrey(), 1, buffer), buffer)
        s = array_tobytes(buffer)
        assert_equal(
            s,
            b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xEF\xFF\xFF\xFF\xA4\xFF\xFF\xFF\xB8'
        )
Example #7
0
 def test_save(self):
     skip_unless_command_exists('djvudump')
     context = Context()
     original_filename = images + 'test0.djvu'
     document = context.new_document(FileUri(original_filename))
     message = document.get_message()
     assert_equal(type(message), DocInfoMessage)
     assert_true(document.decoding_done)
     assert_false(document.decoding_error)
     assert_equal(document.decoding_status, JobOK)
     assert_equal(document.type, DOCUMENT_TYPE_BUNDLED)
     assert_equal(len(document.pages), 2)
     assert_equal(len(document.files), 3)
     (stdout0, stderr0) = run('djvudump', original_filename, LC_ALL='C')
     assert_equal(stderr0, b'')
     stdout0 = stdout0.replace(b'\r\n', b'\n')
     tmpdir = tempfile.mkdtemp()
     try:
         tmp = open(os.path.join(tmpdir, 'tmp.djvu'), 'wb')
         job = document.save(tmp)
         assert_equal(type(job), SaveJob)
         assert_true(job.is_done)
         assert_false(job.is_error)
         tmp.close()
         (stdout, stderr) = run('djvudump', tmp.name, LC_ALL='C')
         assert_equal(stderr, b'')
         stdout = stdout.replace(b'\r\n', b'\n')
         assert_equal(stdout, stdout0)
     finally:
         shutil.rmtree(tmpdir)
         tmp = None
     tmpdir = tempfile.mkdtemp()
     try:
         tmp = open(os.path.join(tmpdir, 'tmp.djvu'), 'wb')
         job = document.save(tmp, pages=(0, ))
         assert_equal(type(job), SaveJob)
         assert_true(job.is_done)
         assert_false(job.is_error)
         tmp.close()
         stdout, stderr = run('djvudump', tmp.name, LC_ALL='C')
         assert_equal(stderr, b'')
         stdout = stdout.replace(b'\r\n', b'\n')
         stdout0 = stdout0.split(b'\n')
         stdout = stdout.split(b'\n')
         stdout[4] = stdout[4].replace(b' (1)', b'')
         assert_equal(len(stdout), 10)
         assert_equal(stdout[3:-1], stdout0[4:10])
         assert_equal(stdout[-1], b'')
     finally:
         shutil.rmtree(tmpdir)
         tmp = None
     tmpdir = tempfile.mkdtemp()
     try:
         tmpfname = os.path.join(tmpdir, 'index.djvu')
         job = document.save(indirect=tmpfname)
         assert_equal(type(job), SaveJob)
         assert_true(job.is_done)
         assert_false(job.is_error)
         (stdout, stderr) = run('djvudump', tmpfname, LC_ALL='C')
         assert_equal(stderr, b'')
         stdout = stdout.replace(b'\r\n', b'\n')
         stdout = stdout.split(b'\n')
         stdout0 = ([b'      shared_anno.iff -> shared_anno.iff'] + [
             b('      p{n:04}.djvu -> p{n:04}.djvu'.format(n=n))
             for n in range(1, 3)
         ])
         assert_equal(len(stdout), 7)
         assert_equal(stdout[2:-2], stdout0)
         assert_equal(stdout[-1], b'')
     finally:
         shutil.rmtree(tmpdir)
     tmpdir = tempfile.mkdtemp()
     try:
         tmpfname = os.path.join(tmpdir, 'index.djvu')
         job = document.save(indirect=tmpfname, pages=(0, ))
         assert_equal(type(job), SaveJob)
         assert_true(job.is_done)
         assert_false(job.is_error)
         (stdout, stderr) = run('djvudump', tmpfname, LC_ALL='C')
         stdout = stdout.replace(b'\r\n', b'\n')
         assert_equal(stderr, b'')
         stdout = stdout.split(b'\n')
         assert_equal(len(stdout), 5)
         assert_equal(stdout[2],
                      b'      shared_anno.iff -> shared_anno.iff')
         assert_equal(stdout[3], b'      p0001.djvu -> p0001.djvu')
         assert_equal(stdout[-1], b'')
     finally:
         shutil.rmtree(tmpdir)
Example #8
0
 def test_new_document(self):
     context = Context()
     document = context.new_document(FileUri(images + 'test1.djvu'))
     assert_equal(type(document), Document)
     message = document.get_message()
     assert_equal(type(message), DocInfoMessage)
     assert_true(document.decoding_done)
     assert_false(document.decoding_error)
     assert_equal(document.decoding_status, JobOK)
     assert_equal(document.type, DOCUMENT_TYPE_SINGLE_PAGE)
     assert_equal(len(document.pages), 1)
     assert_equal(len(document.files), 1)
     decoding_job = document.decoding_job
     assert_true(decoding_job.is_done)
     assert_false(decoding_job.is_error)
     assert_equal(decoding_job.status, JobOK)
     file = document.files[0]
     assert_is(type(file), File)
     assert_is(file.document, document)
     assert_is(file.get_info(), None)
     assert_equal(file.type, 'P')
     assert_equal(file.n_page, 0)
     page = file.page
     assert_equal(type(page), Page)
     assert_is(page.document, document)
     assert_equal(page.n, 0)
     assert_is(file.size, None)
     assert_equal(file.id, u('test1.djvu'))
     assert_equal(type(file.id), unicode)
     assert_equal(file.name, u('test1.djvu'))
     assert_equal(type(file.name), unicode)
     assert_equal(file.title, u('test1.djvu'))
     assert_equal(type(file.title), unicode)
     dump = document.files[0].dump
     assert_equal(type(dump), unicode)
     assert_equal([line for line in dump.splitlines()], [
         u('  FORM:DJVU [83] '),
         u('    INFO [10]         DjVu 64x48, v24, 300 dpi, gamma=2.2'),
         u('    Sjbz [53]         JB2 bilevel data'),
     ])
     page = document.pages[0]
     assert_equal(type(page), Page)
     assert_is(page.document, document)
     assert_is(page.get_info(), None)
     assert_equal(page.width, 64)
     assert_equal(page.height, 48)
     assert_equal(page.size, (64, 48))
     assert_equal(page.dpi, 300)
     assert_equal(page.rotation, 0)
     assert_equal(page.version, 24)
     file = page.file
     assert_equal(type(file), File)
     assert_equal(file.id, u('test1.djvu'))
     assert_equal(type(file.id), unicode)
     dump = document.files[0].dump
     assert_equal(type(dump), unicode)
     assert_equal([line for line in dump.splitlines()], [
         u('  FORM:DJVU [83] '),
         u('    INFO [10]         DjVu 64x48, v24, 300 dpi, gamma=2.2'),
         u('    Sjbz [53]         JB2 bilevel data'),
     ])
     assert_is(document.get_message(wait=False), None)
     assert_is(context.get_message(wait=False), None)
     with assert_raises_str(IndexError, 'file number out of range'):
         document.files[-1].get_info()
     assert_is(document.get_message(wait=False), None)
     assert_is(context.get_message(wait=False), None)
     with assert_raises_str(IndexError, 'page number out of range'):
         document.pages[-1]
     with assert_raises_str(IndexError, 'page number out of range'):
         document.pages[1]
     assert_is(document.get_message(wait=False), None)
     assert_is(context.get_message(wait=False), None)
Example #9
0
 def test_save(self):
     skip_unless_command_exists('djvudump')
     context = Context()
     original_filename = images + 'test0.djvu'
     document = context.new_document(FileUri(original_filename))
     message = document.get_message()
     assert_equal(type(message), DocInfoMessage)
     assert_true(document.decoding_done)
     assert_false(document.decoding_error)
     assert_equal(document.decoding_status, JobOK)
     assert_equal(document.type, DOCUMENT_TYPE_BUNDLED)
     assert_equal(len(document.pages), 2)
     assert_equal(len(document.files), 3)
     (stdout0, stderr0) = run('djvudump', original_filename, LC_ALL='C')
     assert_equal(stderr0, b'')
     stdout0 = stdout0.replace(b'\r\n', b'\n')
     tmpdir = tempfile.mkdtemp()
     try:
         tmp = open(os.path.join(tmpdir, 'tmp.djvu'), 'wb')
         job = document.save(tmp)
         assert_equal(type(job), SaveJob)
         assert_true(job.is_done)
         assert_false(job.is_error)
         tmp.close()
         (stdout, stderr) = run('djvudump', tmp.name, LC_ALL='C')
         assert_equal(stderr, b'')
         stdout = stdout.replace(b'\r\n', b'\n')
         assert_equal(stdout, stdout0)
     finally:
         shutil.rmtree(tmpdir)
         tmp = None
     tmpdir = tempfile.mkdtemp()
     try:
         tmp = open(os.path.join(tmpdir, 'tmp.djvu'), 'wb')
         job = document.save(tmp, pages=(0,))
         assert_equal(type(job), SaveJob)
         assert_true(job.is_done)
         assert_false(job.is_error)
         tmp.close()
         stdout, stderr = run('djvudump', tmp.name, LC_ALL='C')
         assert_equal(stderr, b'')
         stdout = stdout.replace(b'\r\n', b'\n')
         stdout0 = stdout0.split(b'\n')
         stdout = stdout.split(b'\n')
         stdout[4] = stdout[4].replace(b' (1)', b'')
         assert_equal(len(stdout), 10)
         assert_equal(stdout[3:-1], stdout0[4:10])
         assert_equal(stdout[-1], b'')
     finally:
         shutil.rmtree(tmpdir)
         tmp = None
     tmpdir = tempfile.mkdtemp()
     try:
         tmpfname = os.path.join(tmpdir, 'index.djvu')
         job = document.save(indirect=tmpfname)
         assert_equal(type(job), SaveJob)
         assert_true(job.is_done)
         assert_false(job.is_error)
         (stdout, stderr) = run('djvudump', tmpfname, LC_ALL='C')
         assert_equal(stderr, b'')
         stdout = stdout.replace(b'\r\n', b'\n')
         stdout = stdout.split(b'\n')
         stdout0 = (
             [b'      shared_anno.iff -> shared_anno.iff'] +
             [b('      p{n:04}.djvu -> p{n:04}.djvu'.format(n=n)) for n in range(1, 3)]
         )
         assert_equal(len(stdout), 7)
         assert_equal(stdout[2:-2], stdout0)
         assert_equal(stdout[-1], b'')
     finally:
         shutil.rmtree(tmpdir)
     tmpdir = tempfile.mkdtemp()
     try:
         tmpfname = os.path.join(tmpdir, 'index.djvu')
         job = document.save(indirect=tmpfname, pages=(0,))
         assert_equal(type(job), SaveJob)
         assert_true(job.is_done)
         assert_false(job.is_error)
         (stdout, stderr) = run('djvudump', tmpfname, LC_ALL='C')
         stdout = stdout.replace(b'\r\n', b'\n')
         assert_equal(stderr, b'')
         stdout = stdout.split(b'\n')
         assert_equal(len(stdout), 5)
         assert_equal(stdout[2], b'      shared_anno.iff -> shared_anno.iff')
         assert_equal(stdout[3], b'      p0001.djvu -> p0001.djvu')
         assert_equal(stdout[-1], b'')
     finally:
         shutil.rmtree(tmpdir)
Example #10
0
 def test_new_document(self):
     context = Context()
     document = context.new_document(FileUri(images + 'test1.djvu'))
     assert_equal(type(document), Document)
     message = document.get_message()
     assert_equal(type(message), DocInfoMessage)
     assert_true(document.decoding_done)
     assert_false(document.decoding_error)
     assert_equal(document.decoding_status, JobOK)
     assert_equal(document.type, DOCUMENT_TYPE_SINGLE_PAGE)
     assert_equal(len(document.pages), 1)
     assert_equal(len(document.files), 1)
     decoding_job = document.decoding_job
     assert_true(decoding_job.is_done)
     assert_false(decoding_job.is_error)
     assert_equal(decoding_job.status, JobOK)
     file = document.files[0]
     assert_is(type(file), File)
     assert_is(file.document, document)
     assert_is(file.get_info(), None)
     assert_equal(file.type, 'P')
     assert_equal(file.n_page, 0)
     page = file.page
     assert_equal(type(page), Page)
     assert_is(page.document, document)
     assert_equal(page.n, 0)
     assert_is(file.size, None)
     assert_equal(file.id, u('test1.djvu'))
     assert_equal(type(file.id), unicode)
     assert_equal(file.name, u('test1.djvu'))
     assert_equal(type(file.name), unicode)
     assert_equal(file.title, u('test1.djvu'))
     assert_equal(type(file.title), unicode)
     dump = document.files[0].dump
     assert_equal(type(dump), unicode)
     assert_equal(
         [line for line in dump.splitlines()], [
             u('  FORM:DJVU [83] '),
             u('    INFO [10]         DjVu 64x48, v24, 300 dpi, gamma=2.2'),
             u('    Sjbz [53]         JB2 bilevel data'),
         ]
     )
     page = document.pages[0]
     assert_equal(type(page), Page)
     assert_is(page.document, document)
     assert_is(page.get_info(), None)
     assert_equal(page.width, 64)
     assert_equal(page.height, 48)
     assert_equal(page.size, (64, 48))
     assert_equal(page.dpi, 300)
     assert_equal(page.rotation, 0)
     assert_equal(page.version, 24)
     file = page.file
     assert_equal(type(file), File)
     assert_equal(file.id, u('test1.djvu'))
     assert_equal(type(file.id), unicode)
     dump = document.files[0].dump
     assert_equal(type(dump), unicode)
     assert_equal(
         [line for line in dump.splitlines()], [
             u('  FORM:DJVU [83] '),
             u('    INFO [10]         DjVu 64x48, v24, 300 dpi, gamma=2.2'),
             u('    Sjbz [53]         JB2 bilevel data'),
         ]
     )
     assert_is(document.get_message(wait=False), None)
     assert_is(context.get_message(wait=False), None)
     with assert_raises_str(IndexError, 'file number out of range'):
         document.files[-1].get_info()
     assert_is(document.get_message(wait=False), None)
     assert_is(context.get_message(wait=False), None)
     with assert_raises_str(IndexError, 'page number out of range'):
         document.pages[-1]
     with assert_raises_str(IndexError, 'page number out of range'):
         document.pages[1]
     assert_is(document.get_message(wait=False), None)
     assert_is(context.get_message(wait=False), None)