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 t(self, name, sname): if sname is None: sname = name if py3k: [uname, bname] = [sname, sname.encode('UTF-8')] else: [uname, bname] = [sname.decode('UTF-8'), sname] sym = Symbol(name) x = Expression(sym) assert_is(x, Expression(x)) # __repr__(x) assert_repr(x, 'Expression({sym!r})'.format(sym=sym)) # value: v = x.value assert_equal(type(v), Symbol) assert_equal(v, sym) # lvalue: v = x.lvalue assert_equal(type(v), Symbol) assert_equal(v, sym) # __str__(): assert_equal(str(x), sname) assert_repr(x, repr(Expression.from_string(sname))) # __unicode__(): assert_equal(unicode(x), uname) assert_repr(x, repr(Expression.from_string(uname))) # __eq__(), __ne__(): assert_equal(x, Expression(sym)) assert_not_equal(x, Expression(name)) assert_not_equal(x, sym) # __hash__(): assert_equal(hash(x), hash(bname.strip(b'|'))) # pickle: assert_pickle_equal(x) return x
def t(self, name, sname=None): if sname is None: sname = name if py3k: [uname, bname] = [sname, sname.encode('UTF-8')] else: [uname, bname] = [sname.decode('UTF-8'), sname] symbol = Symbol(name) assert_equal(type(symbol), Symbol) assert_equal(symbol, Symbol(name)) assert_is(symbol, Symbol(name)) assert_equal(str(symbol), sname) assert_equal(unicode(symbol), uname) assert_not_equal(symbol, bname) assert_not_equal(symbol, uname) assert_equal(hash(symbol), hash(bname)) assert_pickle_equal(symbol) return symbol
def test_identity(self): assert_is(TEXT_ZONE_PAGE, get_text_zone_type(Symbol('page'))) assert_is(TEXT_ZONE_COLUMN, get_text_zone_type(Symbol('column'))) assert_is(TEXT_ZONE_REGION, get_text_zone_type(Symbol('region'))) assert_is(TEXT_ZONE_PARAGRAPH, get_text_zone_type(Symbol('para'))) assert_is(TEXT_ZONE_LINE, get_text_zone_type(Symbol('line'))) assert_is(TEXT_ZONE_WORD, get_text_zone_type(Symbol('word'))) assert_is(TEXT_ZONE_CHARACTER, get_text_zone_type(Symbol('char')))
def test_string_expressions(): x = Expression('eggs') assert_repr(x, "Expression('eggs')") assert_is(x, Expression(x)) assert_equal(x.value, 'eggs') assert_equal(x.lvalue, 'eggs') assert_equal(str(x), '"eggs"') assert_repr(x, repr(Expression.from_string(str(x)))) assert_equal(x, Expression('eggs')) assert_not_equal(x, Expression(Symbol('eggs'))) assert_not_equal(x, 'eggs') assert_equal(hash(x), hash('eggs')) assert_pickle_equal(x)
class test_expression_writer_ascii(): expr = Expression([Symbol('eggs'), Symbol('ham')]) repr = urepr = '(eggs ham)' def test_stringio_7(self): fp = StringIO() self.expr.print_into(fp) assert_equal(fp.getvalue(), self.repr) def test_stringio_8(self): fp = StringIO() self.expr.print_into(fp, escape_unicode=False) assert_equal(fp.getvalue(), self.urepr) def test_bytesio_7(self): fp = io.BytesIO() self.expr.print_into(fp) assert_equal(fp.getvalue(), b(self.repr)) def test_bytesio_8(self): fp = io.BytesIO() self.expr.print_into(fp, escape_unicode=False) assert_equal(fp.getvalue(), b(self.urepr)) def test_file_io_text_7(self): with tempfile.TemporaryFile(mode='w+t') as fp: self.expr.print_into(fp) fp.seek(0) assert_equal(fp.read(), self.repr) def test_file_io_text_8(self): if py3k: fp = tempfile.TemporaryFile(mode='w+t', encoding='UTF-16-LE') else: fp = tempfile.TemporaryFile(mode='w+t') with fp: self.expr.print_into(fp, escape_unicode=False) fp.seek(0) assert_equal(fp.read(), self.urepr) def test_codecs_io_text_7(self): tmpdir = tempfile.mkdtemp() try: path = os.path.join(tmpdir, 'tmp') with codecs.open(path, mode='w+', encoding='UTF-16-LE') as fp: self.expr.print_into(fp) fp.seek(0) assert_equal(fp.read(), self.repr) finally: shutil.rmtree(tmpdir) def test_codecs_io_text_8(self): tmpdir = tempfile.mkdtemp() try: path = os.path.join(tmpdir, 'tmp') with codecs.open(path, mode='w+', encoding='UTF-16-LE') as fp: self.expr.print_into(fp, escape_unicode=False) fp.seek(0) assert_equal(fp.read(), u(self.urepr)) finally: shutil.rmtree(tmpdir) def test_file_io_binary_7(self): with tempfile.TemporaryFile(mode='w+b') as fp: self.expr.print_into(fp) fp.seek(0) assert_equal(fp.read(), b(self.repr)) def test_file_io_binary_8(self): with tempfile.TemporaryFile(mode='w+b') as fp: self.expr.print_into(fp, escape_unicode=False) fp.seek(0) assert_equal(fp.read(), b(self.urepr)) def test_as_string_7(self): s = self.expr.as_string() assert_equal(s, self.repr) def test_as_string_8(self): s = self.expr.as_string(escape_unicode=False) assert_equal(s, self.urepr)
def test_inequality(self): assert_less( Symbol('eggs'), Symbol('ham'), )
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 set_metadata(self, meta): self._add('set-meta') for key, value in meta.iteritems(): value = unicode(value) self._add('%s\t%s' % (Expression(Symbol(key)), Expression(value))) self._add('.')