def test_jobs(): with assert_raises_str(TypeError, "cannot create 'djvu.decode.Job' instances"): Job() with assert_raises_str(TypeError, "cannot create 'djvu.decode.DocumentDecodingJob' instances"): DocumentDecodingJob()
def test_limits(self): assert_equal(Expression((1 << 29) - 1).value, (1 << 29) - 1) assert_equal(Expression(-1 << 29).value, -1 << 29) with assert_raises_str(ValueError, 'value not in range(-2 ** 29, 2 ** 29)'): Expression(1 << 29) with assert_raises_str(ValueError, 'value not in range(-2 ** 29, 2 ** 29)'): Expression((-1 << 29) - 1)
def test_jobs(): with assert_raises_str(TypeError, "cannot create 'djvu.decode.Job' instances"): Job() with assert_raises_str( TypeError, "cannot create 'djvu.decode.DocumentDecodingJob' instances"): DocumentDecodingJob()
def test_index(self): expr = Expression(()) with assert_raises_str(ValueError, 'value not in list'): expr.index(Expression(42)) lst = [1, 2, 3] expr = Expression(lst) for x in lst: i = lst.index(x) j = expr.index(Expression(x)) assert_equal(i, j) with assert_raises_str(ValueError, 'value not in list'): expr.index(Expression(max(lst) + 1))
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)
def test_context_cache(): context = Context() assert_equal(context.cache_size, 10 << 20) for n in -100, 0, 1 << 31: with assert_raises_str(ValueError, '0 < cache_size < (2 ** 31) must be satisfied'): context.cache_size = n with assert_raises_str(ValueError, '0 < cache_size < (2 ** 31) must be satisfied'): context.cache_size = 0 n = 1 while n < (1 << 31): context.cache_size = n assert_equal(context.cache_size, n) n = (n + 1) * 2 - 1 context.clear_cache()
def test_comparison1(self): assert_not_equal(TEXT_ZONE_PAGE, '') assert_not_equal(TEXT_ZONE_PAGE, 42) with assert_raises_str( TypeError, 'cannot compare text zone type with other object'): TEXT_ZONE_PAGE < 42 with assert_raises_str( TypeError, 'cannot compare text zone type with other object'): TEXT_ZONE_PAGE <= 42 with assert_raises_str( TypeError, 'cannot compare text zone type with other object'): TEXT_ZONE_PAGE > 42 with assert_raises_str( TypeError, 'cannot compare text zone type with other object'): TEXT_ZONE_PAGE >= 42
def test(self): context = Context() document = context.new_document(FileUri(images + 'test1.djvu')) message = document.get_message() assert_equal(type(message), DocInfoMessage) thumbnail = document.pages[0].thumbnail assert_equal(thumbnail.status, JobOK) assert_equal(thumbnail.calculate(), JobOK) message = document.get_message() assert_equal(type(message), ThumbnailMessage) assert_equal(message.thumbnail.page.n, 0) (w, h, r), pixels = thumbnail.render((5, 5), PixelFormatGrey(), dry_run=True) assert_equal((w, h, r), (5, 3, 5)) assert_is(pixels, None) (w, h, r), pixels = thumbnail.render((5, 5), PixelFormatGrey()) assert_equal((w, h, r), (5, 3, 5)) assert_equal( pixels[:15], b'\xFF\xEB\xA7\xF2\xFF\xFF\xBF\x86\xBE\xFF\xFF\xE7\xD6\xE7\xFF') buffer = array.array('B', b'\0') with assert_raises_str(ValueError, 'Image buffer is too small (25 > 1)'): (w, h, r), pixels = thumbnail.render((5, 5), PixelFormatGrey(), buffer=buffer) buffer = array.array('B', b'\0' * 25) (w, h, r), pixels = thumbnail.render((5, 5), PixelFormatGrey(), buffer=buffer) assert_is(pixels, buffer) s = array_tobytes(buffer[:15]) assert_equal( s, b'\xFF\xEB\xA7\xF2\xFF\xFF\xBF\x86\xBE\xFF\xFF\xE7\xD6\xE7\xFF')
def test(self): context = Context() document = context.new_document(FileUri(images + 'test1.djvu')) message = document.get_message() assert_equal(type(message), DocInfoMessage) thumbnail = document.pages[0].thumbnail assert_equal(thumbnail.status, JobOK) assert_equal(thumbnail.calculate(), JobOK) message = document.get_message() assert_equal(type(message), ThumbnailMessage) assert_equal(message.thumbnail.page.n, 0) (w, h, r), pixels = thumbnail.render((5, 5), PixelFormatGrey(), dry_run=True) assert_equal((w, h, r), (5, 3, 5)) assert_is(pixels, None) (w, h, r), pixels = thumbnail.render((5, 5), PixelFormatGrey()) assert_equal((w, h, r), (5, 3, 5)) assert_equal(pixels[:15], b'\xFF\xEB\xA7\xF2\xFF\xFF\xBF\x86\xBE\xFF\xFF\xE7\xD6\xE7\xFF') buffer = array.array('B', b'\0') with assert_raises_str(ValueError, 'Image buffer is too small (25 > 1)'): (w, h, r), pixels = thumbnail.render((5, 5), PixelFormatGrey(), buffer=buffer) buffer = array.array('B', b'\0' * 25) (w, h, r), pixels = thumbnail.render((5, 5), PixelFormatGrey(), buffer=buffer) assert_is(pixels, buffer) s = array_tobytes(buffer[:15]) assert_equal(s, b'\xFF\xEB\xA7\xF2\xFF\xFF\xBF\x86\xBE\xFF\xFF\xE7\xD6\xE7\xFF')
def test_remove(self): expr = Expression([0, 1, 2, 3, 4, 5, 6]) expr.remove(Expression(0)) assert_equal(expr, Expression([1, 2, 3, 4, 5, 6])) with assert_raises_str(IndexError, 'item not in list'): expr.remove(Expression(0)) expr.remove(Expression(6)) assert_equal(expr, Expression([1, 2, 3, 4, 5])) expr.remove(Expression(5)) assert_equal(expr, Expression([1, 2, 3, 4])) expr.remove(Expression(3)) assert_equal(expr, Expression([1, 2, 4])) expr.remove(Expression(2)) assert_equal(expr, Expression([1, 4])) expr.remove(Expression(4)) expr.remove(Expression(1)) with assert_raises_str(IndexError, 'item not in list'): expr.remove(Expression(-1))
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)
def test_extend(self): lst = [] expr = Expression(()) for ext in [1], [], [2, 3]: lst.extend(ext) expr.extend(ext) assert_equal(expr, Expression(lst)) assert_equal(expr.lvalue, lst) with assert_raises_str(TypeError, "'int' object is not iterable"): expr.extend(0)
def test_inplace_add(self): lst = [] expr0 = expr = Expression(()) for ext in [], [1], [], [2, 3]: lst += ext expr += ext assert_equal(expr, Expression(lst)) assert_equal(expr.lvalue, lst) assert_is(expr, expr0) with assert_raises_str(TypeError, "'int' object is not iterable"): expr += 0
def test_pop(self): expr = Expression([0, 1, 2, 3, 4, 5, 6]) assert_equal(expr.pop(0), Expression(0)) assert_equal(expr, Expression([1, 2, 3, 4, 5, 6])) with assert_raises_str(IndexError, 'pop index of out range'): expr.pop(6) assert_equal(expr.pop(5), Expression(6)) assert_equal(expr, Expression([1, 2, 3, 4, 5])) assert_equal(expr.pop(-1), Expression(5)) assert_equal(expr, Expression([1, 2, 3, 4])) assert_equal(expr.pop(-2), Expression(3)) assert_equal(expr, Expression([1, 2, 4])) assert_equal(expr.pop(1), Expression(2)) assert_equal(expr, Expression([1, 4])) expr.pop() expr.pop() with assert_raises_str(IndexError, 'pop from empty list'): expr.pop() for i in range(-2, 3): with assert_raises_str(IndexError, 'pop from empty list'): expr.pop(i)
def test_delitem(self): expr = Expression([0, 1, 2, 3, 4, 5, 6]) del expr[0] assert_equal(expr, Expression([1, 2, 3, 4, 5, 6])) with assert_raises_str(IndexError, 'pop index of out range'): expr.pop(6) del expr[5] assert_equal(expr, Expression([1, 2, 3, 4, 5])) del expr[-1] assert_equal(expr, Expression([1, 2, 3, 4])) del expr[-2] assert_equal(expr, Expression([1, 2, 4])) del expr[1] assert_equal(expr, Expression([1, 4])) del expr[1:] assert_equal(expr, Expression([1])) del expr[:] assert_equal(expr, Expression([])) for i in range(-2, 3): with assert_raises_str(IndexError, 'pop from empty list'): del expr[i]
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')
def test(self): context = Context() document = context.new_document('dummy://dummy.djvu') message = document.get_message() assert_equal(type(message), NewStreamMessage) assert_equal(message.name, 'dummy.djvu') assert_equal(message.uri, 'dummy://dummy.djvu') assert_equal(type(message.stream), Stream) with assert_raises(NotAvailable): document.outline.sexpr with assert_raises(NotAvailable): document.annotations.sexpr with assert_raises(NotAvailable): document.pages[0].text.sexpr with assert_raises(NotAvailable): document.pages[0].annotations.sexpr try: with open(images + 'test1.djvu', 'rb') as fp: message.stream.write(fp.read()) finally: message.stream.close() with assert_raises_str(IOError, 'I/O operation on closed file'): message.stream.write(b'eggs') message = document.get_message() assert_equal(type(message), DocInfoMessage) outline = document.outline outline.wait() x = outline.sexpr assert_equal(x, Expression([])) anno = document.annotations anno.wait() x = anno.sexpr assert_equal(x, Expression([])) text = document.pages[0].text text.wait() x = text.sexpr assert_equal(x, Expression([])) anno = document.pages[0].annotations anno.wait() x = anno.sexpr assert_equal(x, Expression([]))
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)
def test_bad_new(self): with assert_raises_str( TypeError, "cannot create 'djvu.decode.Document' instances"): Document()
def test_bad_new(self): with assert_raises_str( TypeError, "cannot create 'djvu.decode.PageJob' instances"): PageJob()
def test_bad_new(self): with assert_raises_str( TypeError, "cannot create 'djvu.decode.PixelFormat' instances"): PixelFormat()
def test_bad_io(self): expr = Expression(23) with assert_raises_str(AttributeError, "'int' object has no attribute 'write'"): expr.print_into(42)
def test_bad_new(self): with assert_raises_str(TypeError, "cannot create 'djvu.decode.PixelFormat' instances"): PixelFormat()
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)
def test_bad_args(self): with assert_raises_str(ValueError, 'need more than 2 values to unpack'): AffineTransform((1, 2), (3, 4, 5))
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' )
def test_bad_new(self): with assert_raises_str( TypeError, "cannot create 'djvu.decode.Message' instances"): Message()
def test_bad_new(self): with assert_raises_str(TypeError, "Argument 'document' has incorrect type (expected djvu.decode.Document, got NoneType)"): Stream(None, 42)
def test(self): context = Context() document = context.new_document(FileUri(images + 'test0.djvu')) assert_equal(type(document), Document) message = document.get_message() assert_equal(type(message), DocInfoMessage) anno = DocumentAnnotations(document, shared=False) assert_equal(type(anno), DocumentAnnotations) anno.wait() x = anno.sexpr assert_equal(x, Expression([])) anno = document.annotations assert_equal(type(anno), DocumentAnnotations) anno.wait() assert_is(anno.background_color, None) assert_is(anno.horizontal_align, None) assert_is(anno.vertical_align, None) assert_is(anno.mode, None) assert_is(anno.zoom, None) expected_metadata = [ Symbol('metadata'), [Symbol('ModDate'), '2015-08-17 19:54:57+02:00'], [Symbol('CreationDate'), '2015-08-17 19:54:57+02:00'], [Symbol('Producer'), 'pdfTeX-1.40.16'], [Symbol('Creator'), 'LaTeX with hyperref package'], [Symbol('Author'), 'Jakub Wilk'] ] expected_xmp = [ Symbol('xmp'), '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">' '<rdf:Description rdf:about="">' '<xmpMM:History xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/"><rdf:Seq><rdf:li xmlns:stEvt="http://ns.adobe.com/xap/1.0/sType/ResourceEvent#" stEvt:action="converted" stEvt:parameters="from application/pdf to image/vnd.djvu" stEvt:softwareAgent="pdf2djvu 0.8.1 (DjVuLibre 3.5.27, Poppler 0.26.5, GraphicsMagick++ 1.3.21, GNOME XSLT 1.1.28, GNOME XML 2.9.2, PStreams 0.8.0)" stEvt:when="2015-08-17T17:54:58+00:00"/></rdf:Seq></xmpMM:History>' '<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jakub Wilk</dc:creator>' '<dc:format xmlns:dc="http://purl.org/dc/elements/1.1/">image/vnd.djvu</dc:format>' '<pdf:Producer xmlns:pdf="http://ns.adobe.com/pdf/1.3/">pdfTeX-1.40.16</pdf:Producer>' '<xmp:CreatorTool xmlns:xmp="http://ns.adobe.com/xap/1.0/">LaTeX with hyperref package</xmp:CreatorTool>' '<xmp:CreateDate xmlns:xmp="http://ns.adobe.com/xap/1.0/">2015-08-17T19:54:57+02:00</xmp:CreateDate>' '<xmp:ModifyDate xmlns:xmp="http://ns.adobe.com/xap/1.0/">2015-08-17T19:54:57+02:00</xmp:ModifyDate>' '<xmp:MetadataDate xmlns:xmp="http://ns.adobe.com/xap/1.0/">2015-08-17T17:54:58+00:00</xmp:MetadataDate>' '</rdf:Description>' '</rdf:RDF>\n' ] assert_equal( anno.sexpr, Expression([expected_metadata, expected_xmp]) ) metadata = anno.metadata assert_equal(type(metadata), Metadata) hyperlinks = anno.hyperlinks assert_equal(type(hyperlinks), Hyperlinks) assert_equal(len(hyperlinks), 0) assert_equal(list(hyperlinks), []) outline = document.outline assert_equal(type(outline), DocumentOutline) outline.wait() assert_equal(outline.sexpr, Expression( [Symbol('bookmarks'), ['Lorem ipsum', '#p0001.djvu'], ['Hyperlinks', '#p0002.djvu', ['local', '#p0002.djvu'], ['remote', '#p0002.djvu'] ] ] )) page = document.pages[1] anno = page.annotations assert_equal(type(anno), PageAnnotations) anno.wait() assert_is(anno.background_color, None) assert_is(anno.horizontal_align, None) assert_is(anno.vertical_align, None) assert_is(anno.mode, None) assert_is(anno.zoom, None) expected_hyperlinks = [ [Symbol('maparea'), '#p0001.djvu', '', [Symbol('rect'), 520, 2502, 33, 42], [Symbol('border'), Symbol('#ff0000')]], [Symbol('maparea'), 'http://jwilk.net/', '', [Symbol('rect'), 458, 2253, 516, 49], [Symbol('border'), Symbol('#00ffff')]] ] assert_equal( anno.sexpr, Expression([expected_metadata, expected_xmp] + expected_hyperlinks) ) page_metadata = anno.metadata assert_equal(type(page_metadata), Metadata) assert_equal(page_metadata.keys(), metadata.keys()) assert_equal([page_metadata[k] == metadata[k] for k in metadata], [True, True, True, True, True]) hyperlinks = anno.hyperlinks assert_equal(type(hyperlinks), Hyperlinks) assert_equal(len(hyperlinks), 2) assert_equal( list(hyperlinks), [Expression(h) for h in expected_hyperlinks] ) text = page.text assert_equal(type(text), PageText) text.wait() text_s = text.sexpr text_s_detail = [PageText(page, details).sexpr for details in (TEXT_DETAILS_PAGE, TEXT_DETAILS_COLUMN, TEXT_DETAILS_REGION, TEXT_DETAILS_PARAGRAPH, TEXT_DETAILS_LINE, TEXT_DETAILS_WORD, TEXT_DETAILS_CHARACTER, TEXT_DETAILS_ALL)] assert_equal(text_s_detail[0], text_s_detail[1]) assert_equal(text_s_detail[1], text_s_detail[2]) assert_equal(text_s_detail[2], text_s_detail[3]) assert_equal( text_s_detail[0], Expression( [Symbol('page'), 0, 0, 2550, 3300, '2 Hyperlinks \n' '2.1 local \n' + u('→1 \n') + '2.2 remote \nhttp://jwilk.net/ \n' '2 \n' ] ) ) assert_equal( text_s_detail[4], Expression( [Symbol('page'), 0, 0, 2550, 3300, [Symbol('line'), 462, 2712, 910, 2777, '2 Hyperlinks '], [Symbol('line'), 462, 2599, 714, 2641, '2.1 local '], [Symbol('line'), 464, 2505, 544, 2540, u('→1 ')], [Symbol('line'), 462, 2358, 772, 2400, '2.2 remote '], [Symbol('line'), 463, 2256, 964, 2298, 'http://jwilk.net/ '], [Symbol('line'), 1260, 375, 1282, 409, '2 '] ] ) ) assert_equal(text_s_detail[5], text_s) assert_equal(text_s_detail[6], text_s) assert_equal(text_s_detail[7], text_s) assert_equal( text_s, Expression( [Symbol('page'), 0, 0, 2550, 3300, [Symbol('line'), 462, 2712, 910, 2777, [Symbol('word'), 462, 2727, 495, 2776, '2'], [Symbol('word'), 571, 2712, 910, 2777, 'Hyperlinks']], [Symbol('line'), 462, 2599, 714, 2641, [Symbol('word'), 462, 2599, 532, 2641, '2.1'], [Symbol('word'), 597, 2599, 714, 2640, 'local']], [Symbol('line'), 464, 2505, 544, 2540, [Symbol('word'), 464, 2505, 544, 2540, u('→1')]], [Symbol('line'), 462, 2358, 772, 2400, [Symbol('word'), 462, 2358, 535, 2400, '2.2'], [Symbol('word'), 598, 2358, 772, 2397, 'remote']], [Symbol('line'), 463, 2256, 964, 2298, [Symbol('word'), 463, 2256, 964, 2298, 'http://jwilk.net/']], [Symbol('line'), 1260, 375, 1282, 409, [Symbol('word'), 1260, 375, 1282, 409, '2']] ] ) ) with assert_raises_str(TypeError, 'details must be a symbol or none'): PageText(page, 'eggs') with assert_raises_str(ValueError, 'details must be equal to TEXT_DETAILS_PAGE, or TEXT_DETAILS_COLUMN, or TEXT_DETAILS_REGION, or TEXT_DETAILS_PARAGRAPH, or TEXT_DETAILS_LINE, or TEXT_DETAILS_WORD, or TEXT_DETAILS_CHARACTER or TEXT_DETAILS_ALL'): PageText(page, Symbol('eggs'))
def test(self): context = Context() document = context.new_document(FileUri(images + 'test0.djvu')) assert_equal(type(document), Document) message = document.get_message() assert_equal(type(message), DocInfoMessage) anno = DocumentAnnotations(document, shared=False) assert_equal(type(anno), DocumentAnnotations) anno.wait() x = anno.sexpr assert_equal(x, Expression([])) anno = document.annotations assert_equal(type(anno), DocumentAnnotations) anno.wait() assert_is(anno.background_color, None) assert_is(anno.horizontal_align, None) assert_is(anno.vertical_align, None) assert_is(anno.mode, None) assert_is(anno.zoom, None) expected_metadata = [ Symbol('metadata'), [Symbol('ModDate'), '2015-08-17 19:54:57+02:00'], [Symbol('CreationDate'), '2015-08-17 19:54:57+02:00'], [Symbol('Producer'), 'pdfTeX-1.40.16'], [Symbol('Creator'), 'LaTeX with hyperref package'], [Symbol('Author'), 'Jakub Wilk'] ] expected_xmp = [ Symbol('xmp'), '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">' '<rdf:Description rdf:about="">' '<xmpMM:History xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/"><rdf:Seq><rdf:li xmlns:stEvt="http://ns.adobe.com/xap/1.0/sType/ResourceEvent#" stEvt:action="converted" stEvt:parameters="from application/pdf to image/vnd.djvu" stEvt:softwareAgent="pdf2djvu 0.8.1 (DjVuLibre 3.5.27, Poppler 0.26.5, GraphicsMagick++ 1.3.21, GNOME XSLT 1.1.28, GNOME XML 2.9.2, PStreams 0.8.0)" stEvt:when="2015-08-17T17:54:58+00:00"/></rdf:Seq></xmpMM:History>' '<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jakub Wilk</dc:creator>' '<dc:format xmlns:dc="http://purl.org/dc/elements/1.1/">image/vnd.djvu</dc:format>' '<pdf:Producer xmlns:pdf="http://ns.adobe.com/pdf/1.3/">pdfTeX-1.40.16</pdf:Producer>' '<xmp:CreatorTool xmlns:xmp="http://ns.adobe.com/xap/1.0/">LaTeX with hyperref package</xmp:CreatorTool>' '<xmp:CreateDate xmlns:xmp="http://ns.adobe.com/xap/1.0/">2015-08-17T19:54:57+02:00</xmp:CreateDate>' '<xmp:ModifyDate xmlns:xmp="http://ns.adobe.com/xap/1.0/">2015-08-17T19:54:57+02:00</xmp:ModifyDate>' '<xmp:MetadataDate xmlns:xmp="http://ns.adobe.com/xap/1.0/">2015-08-17T17:54:58+00:00</xmp:MetadataDate>' '</rdf:Description>' '</rdf:RDF>\n' ] assert_equal(anno.sexpr, Expression([expected_metadata, expected_xmp])) metadata = anno.metadata assert_equal(type(metadata), Metadata) hyperlinks = anno.hyperlinks assert_equal(type(hyperlinks), Hyperlinks) assert_equal(len(hyperlinks), 0) assert_equal(list(hyperlinks), []) outline = document.outline assert_equal(type(outline), DocumentOutline) outline.wait() assert_equal( outline.sexpr, Expression([ Symbol('bookmarks'), ['Lorem ipsum', '#p0001.djvu'], [ 'Hyperlinks', '#p0002.djvu', ['local', '#p0002.djvu'], ['remote', '#p0002.djvu'] ] ])) page = document.pages[1] anno = page.annotations assert_equal(type(anno), PageAnnotations) anno.wait() assert_is(anno.background_color, None) assert_is(anno.horizontal_align, None) assert_is(anno.vertical_align, None) assert_is(anno.mode, None) assert_is(anno.zoom, None) expected_hyperlinks = [[ Symbol('maparea'), '#p0001.djvu', '', [Symbol('rect'), 520, 2502, 33, 42], [Symbol('border'), Symbol('#ff0000')] ], [ Symbol('maparea'), 'http://jwilk.net/', '', [Symbol('rect'), 458, 2253, 516, 49], [Symbol('border'), Symbol('#00ffff')] ]] assert_equal( anno.sexpr, Expression([expected_metadata, expected_xmp] + expected_hyperlinks)) page_metadata = anno.metadata assert_equal(type(page_metadata), Metadata) assert_equal(page_metadata.keys(), metadata.keys()) assert_equal([page_metadata[k] == metadata[k] for k in metadata], [True, True, True, True, True]) hyperlinks = anno.hyperlinks assert_equal(type(hyperlinks), Hyperlinks) assert_equal(len(hyperlinks), 2) assert_equal(list(hyperlinks), [Expression(h) for h in expected_hyperlinks]) text = page.text assert_equal(type(text), PageText) text.wait() text_s = text.sexpr text_s_detail = [ PageText(page, details).sexpr for details in (TEXT_DETAILS_PAGE, TEXT_DETAILS_COLUMN, TEXT_DETAILS_REGION, TEXT_DETAILS_PARAGRAPH, TEXT_DETAILS_LINE, TEXT_DETAILS_WORD, TEXT_DETAILS_CHARACTER, TEXT_DETAILS_ALL) ] assert_equal(text_s_detail[0], text_s_detail[1]) assert_equal(text_s_detail[1], text_s_detail[2]) assert_equal(text_s_detail[2], text_s_detail[3]) assert_equal( text_s_detail[0], Expression([ Symbol('page'), 0, 0, 2550, 3300, '2 Hyperlinks \n' '2.1 local \n' + u('→1 \n') + '2.2 remote \nhttp://jwilk.net/ \n' '2 \n' ])) assert_equal( text_s_detail[4], Expression([ Symbol('page'), 0, 0, 2550, 3300, [Symbol('line'), 462, 2712, 910, 2777, '2 Hyperlinks '], [Symbol('line'), 462, 2599, 714, 2641, '2.1 local '], [Symbol('line'), 464, 2505, 544, 2540, u('→1 ')], [Symbol('line'), 462, 2358, 772, 2400, '2.2 remote '], [Symbol('line'), 463, 2256, 964, 2298, 'http://jwilk.net/ '], [Symbol('line'), 1260, 375, 1282, 409, '2 '] ])) assert_equal(text_s_detail[5], text_s) assert_equal(text_s_detail[6], text_s) assert_equal(text_s_detail[7], text_s) assert_equal( text_s, Expression([ Symbol('page'), 0, 0, 2550, 3300, [ Symbol('line'), 462, 2712, 910, 2777, [Symbol('word'), 462, 2727, 495, 2776, '2'], [Symbol('word'), 571, 2712, 910, 2777, 'Hyperlinks'] ], [ Symbol('line'), 462, 2599, 714, 2641, [Symbol('word'), 462, 2599, 532, 2641, '2.1'], [Symbol('word'), 597, 2599, 714, 2640, 'local'] ], [ Symbol('line'), 464, 2505, 544, 2540, [Symbol('word'), 464, 2505, 544, 2540, u('→1')] ], [ Symbol('line'), 462, 2358, 772, 2400, [Symbol('word'), 462, 2358, 535, 2400, '2.2'], [Symbol('word'), 598, 2358, 772, 2397, 'remote'] ], [ Symbol('line'), 463, 2256, 964, 2298, [ Symbol('word'), 463, 2256, 964, 2298, 'http://jwilk.net/' ] ], [ Symbol('line'), 1260, 375, 1282, 409, [Symbol('word'), 1260, 375, 1282, 409, '2'] ] ])) with assert_raises_str(TypeError, 'details must be a symbol or none'): PageText(page, 'eggs') with assert_raises_str( ValueError, 'details must be equal to TEXT_DETAILS_PAGE, or TEXT_DETAILS_COLUMN, or TEXT_DETAILS_REGION, or TEXT_DETAILS_PARAGRAPH, or TEXT_DETAILS_LINE, or TEXT_DETAILS_WORD, or TEXT_DETAILS_CHARACTER or TEXT_DETAILS_ALL' ): PageText(page, Symbol('eggs'))
def test_bad_new(self): with assert_raises_str(TypeError, "cannot create 'djvu.decode.PageJob' instances"): PageJob()
def test_bad_new(self): with assert_raises_str(TypeError, "cannot create 'djvu.decode.Document' instances"): Document()
def test_bad_io(self): with assert_raises_str(AttributeError, "'int' object has no attribute 'read'"): Expression.from_stream(42)
def test_bad_new(self): with assert_raises_str( TypeError, "Argument 'document' has incorrect type (expected djvu.decode.Document, got NoneType)" ): Stream(None, 42)
def test_bad_new(self): with assert_raises_str(TypeError, "cannot create 'djvu.decode.Message' instances"): Message()