Exemplo n.º 1
0
 def test_pickle(self):
     markup = Markup('foo')
     buf = BytesIO()
     pickle.dump(markup, buf, 2)
     buf.seek(0)
     expected_foo = "u'foo'" if IS_PYTHON2 else "'foo'"
     self.assertEqual("<Markup %s>" % expected_foo, repr(pickle.load(buf)))
Exemplo n.º 2
0
 def test_pickle(self):
     expr = Expression('1 < 2')
     buf = BytesIO()
     pickle.dump(expr, buf, 2)
     buf.seek(0)
     unpickled = pickle.load(buf)
     assert unpickled.evaluate({}) is True
Exemplo n.º 3
0
 def test_pickle(self):
     attrs = Attrs([("attr1", "foo"), ("attr2", "bar")])
     buf = BytesIO()
     pickle.dump(attrs, buf, 2)
     buf.seek(0)
     unpickled = pickle.load(buf)
     self.assertEquals("Attrs([('attr1', 'foo'), ('attr2', 'bar')])", repr(unpickled))
Exemplo n.º 4
0
 def test_pickle(self):
     expr = Expression('1 < 2')
     buf = BytesIO()
     pickle.dump(expr, buf, 2)
     buf.seek(0)
     unpickled = pickle.load(buf)
     assert unpickled.evaluate({}) is True
Exemplo n.º 5
0
 def test_pickle(self):
     xml = XML('<li>Foo</li>')
     buf = BytesIO()
     pickle.dump(xml, buf, 2)
     buf.seek(0)
     xml = pickle.load(buf)
     self.assertEquals('<li>Foo</li>', xml.render(encoding=None))
Exemplo n.º 6
0
 def test_pickle(self):
     xml = XML('<li>Foo</li>')
     buf = BytesIO()
     pickle.dump(xml, buf, 2)
     buf.seek(0)
     xml = pickle.load(buf)
     self.assertEquals('<li>Foo</li>', xml.render(encoding=None))
Exemplo n.º 7
0
 def test_pickle(self):
     stream = XML('<root>$var</root>')
     tmpl = MarkupTemplate(stream)
     buf = BytesIO()
     pickle.dump(tmpl, buf, 2)
     buf.seek(0)
     unpickled = pickle.load(buf)
     self.assertEqual('<root>42</root>', str(unpickled.generate(var=42)))
Exemplo n.º 8
0
 def test_pickle(self):
     stream = XML('<root>$var</root>')
     tmpl = MarkupTemplate(stream)
     buf = BytesIO()
     pickle.dump(tmpl, buf, 2)
     buf.seek(0)
     unpickled = pickle.load(buf)
     self.assertEqual('<root>42</root>', str(unpickled.generate(var=42)))
Exemplo n.º 9
0
 def test_pickle(self):
     attrs = Attrs([("attr1", "foo"), ("attr2", "bar")])
     buf = BytesIO()
     pickle.dump(attrs, buf, 2)
     buf.seek(0)
     unpickled = pickle.load(buf)
     self.assertEquals("Attrs([('attr1', 'foo'), ('attr2', 'bar')])",
                       repr(unpickled))
Exemplo n.º 10
0
 def test_pickle(self):
     ns = Namespace("http://www.example.org/namespace")
     buf = BytesIO()
     pickle.dump(ns, buf, 2)
     buf.seek(0)
     unpickled = pickle.load(buf)
     self.assertEquals("Namespace('http://www.example.org/namespace')", repr(unpickled))
     self.assertEquals("http://www.example.org/namespace", unpickled.uri)
Exemplo n.º 11
0
 def test_pickle(self):
     qname = QName("http://www.example.org/namespace}elem")
     buf = BytesIO()
     pickle.dump(qname, buf, 2)
     buf.seek(0)
     unpickled = pickle.load(buf)
     self.assertEquals("{http://www.example.org/namespace}elem", unpickled)
     self.assertEquals("http://www.example.org/namespace", unpickled.namespace)
     self.assertEquals("elem", unpickled.localname)
Exemplo n.º 12
0
 def test_pickle(self):
     suite = Suite('foo = 42')
     buf = BytesIO()
     pickle.dump(suite, buf, 2)
     buf.seek(0)
     unpickled = pickle.load(buf)
     data = {}
     unpickled.execute(data)
     self.assertEqual(42, data['foo'])
Exemplo n.º 13
0
 def test_pickle(self):
     suite = Suite('foo = 42')
     buf = BytesIO()
     pickle.dump(suite, buf, 2)
     buf.seek(0)
     unpickled = pickle.load(buf)
     data = {}
     unpickled.execute(data)
     self.assertEqual(42, data['foo'])
Exemplo n.º 14
0
 def test_pickle(self):
     ns = Namespace('http://www.example.org/namespace')
     buf = BytesIO()
     pickle.dump(ns, buf, 2)
     buf.seek(0)
     unpickled = pickle.load(buf)
     self.assertEquals("Namespace('http://www.example.org/namespace')",
                       repr(unpickled))
     self.assertEquals('http://www.example.org/namespace', unpickled.uri)
Exemplo n.º 15
0
 def test_pickle(self):
     qname = QName('http://www.example.org/namespace}elem')
     buf = BytesIO()
     pickle.dump(qname, buf, 2)
     buf.seek(0)
     unpickled = pickle.load(buf)
     self.assertEquals('{http://www.example.org/namespace}elem', unpickled)
     self.assertEquals('http://www.example.org/namespace',
                       unpickled.namespace)
     self.assertEquals('elem', unpickled.localname)
Exemplo n.º 16
0
def HTML(text, encoding=None):
    """Parse the given HTML source and return a markup stream.
    
    Unlike with `HTMLParser`, the returned stream is reusable, meaning it can be
    iterated over multiple times:
    
    >>> html = HTML('<body><h1>Foo</h1></body>', encoding='utf-8')
    >>> print(html)
    <body><h1>Foo</h1></body>
    >>> print((html.select('h1')))
    <h1>Foo</h1>
    >>> print((html.select('h1/text()')))
    Foo
    
    :param text: the HTML source
    :return: the parsed XML event stream
    :raises ParseError: if the HTML text is not well-formed, and error recovery
                        fails
    """
    if isinstance(text, str):
        # If it's unicode text the encoding should be set to None.
        # The option to pass in an incorrect encoding is for ease
        # of writing doctests that work in both Python 2.x and 3.x.
        return Stream(list(HTMLParser(StringIO(text), encoding=None)))
    return Stream(list(HTMLParser(BytesIO(text), encoding=encoding)))
Exemplo n.º 17
0
 def test_latin1_encoded_xmldecl(self):
     text = """<?xml version="1.0" encoding="iso-8859-1" ?>
     <div>\xf6</div>
     """.encode('iso-8859-1')
     events = list(XMLParser(BytesIO(text)))
     kind, data, pos = events[2]
     self.assertEqual(Stream.TEXT, kind)
     self.assertEqual('\xf6', data)
Exemplo n.º 18
0
 def test_out_of_order_tags3(self):
     text = '<span><b>Foobar</i>'.encode('utf-8')
     events = list(HTMLParser(BytesIO(text), encoding='utf-8'))
     self.assertEqual(5, len(events))
     self.assertEqual((Stream.START, ('span', ())), events[0][:2])
     self.assertEqual((Stream.START, ('b', ())), events[1][:2])
     self.assertEqual((Stream.TEXT, 'Foobar'), events[2][:2])
     self.assertEqual((Stream.END, 'b'), events[3][:2])
     self.assertEqual((Stream.END, 'span'), events[4][:2])
Exemplo n.º 19
0
 def test_out_of_order_tags2(self):
     text = '<span class="baz"><b><i>Foobar</span></b></i>'.encode('utf-8')
     events = list(HTMLParser(BytesIO(text), encoding='utf-8'))
     self.assertEqual(7, len(events))
     self.assertEqual((Stream.START, ('span', Attrs([('class', 'baz')]))),
                      events[0][:2])
     self.assertEqual((Stream.START, ('b', ())), events[1][:2])
     self.assertEqual((Stream.START, ('i', ())), events[2][:2])
     self.assertEqual((Stream.TEXT, 'Foobar'), events[3][:2])
     self.assertEqual((Stream.END, 'i'), events[4][:2])
     self.assertEqual((Stream.END, 'b'), events[5][:2])
     self.assertEqual((Stream.END, 'span'), events[6][:2])
Exemplo n.º 20
0
    def __init__(self,
                 source,
                 filepath=None,
                 filename=None,
                 loader=None,
                 encoding=None,
                 lookup='strict',
                 allow_exec=True):
        """Initialize a template from either a string, a file-like object, or
        an already parsed markup stream.
        
        :param source: a string, file-like object, or markup stream to read the
                       template from
        :param filepath: the absolute path to the template file
        :param filename: the path to the template file relative to the search
                         path
        :param loader: the `TemplateLoader` to use for loading included
                       templates
        :param encoding: the encoding of the `source`
        :param lookup: the variable lookup mechanism; either "strict" (the
                       default), "lenient", or a custom lookup class
        :param allow_exec: whether Python code blocks in templates should be
                           allowed
        
        :note: Changed in 0.5: Added the `allow_exec` argument
        """
        self.filepath = filepath or filename
        self.filename = filename
        self.loader = loader
        self.lookup = lookup
        self.allow_exec = allow_exec
        self._init_filters()
        self._init_loader()
        self._prepared = False

        if not isinstance(source, Stream) and not hasattr(source, 'read'):
            if isinstance(source, str):
                source = StringIO(source)
            else:
                source = BytesIO(source)
        try:
            self._stream = self._parse(source, encoding)
        except ParseError as e:
            raise TemplateSyntaxError(e.msg, self.filepath, e.lineno, e.offset)
Exemplo n.º 21
0
def HTML(text, encoding=None):
    """Parse the given HTML source and return a markup stream.
    
    Unlike with `HTMLParser`, the returned stream is reusable, meaning it can be
    iterated over multiple times:
    
    >>> html = HTML('<body><h1>Foo</h1></body>', encoding='utf-8')
    >>> print(html)
    <body><h1>Foo</h1></body>
    >>> print(html.select('h1'))
    <h1>Foo</h1>
    >>> print(html.select('h1/text()'))
    Foo
    
    :param text: the HTML source
    :return: the parsed XML event stream
    :raises ParseError: if the HTML text is not well-formed, and error recovery
                        fails
    """
    if isinstance(text, str):
        return Stream(list(HTMLParser(StringIO(text), encoding=encoding)))
    return Stream(list(HTMLParser(BytesIO(text), encoding=encoding)))
Exemplo n.º 22
0
 def _build_foreign(self, context, base, sysid, pubid):
     parser = self.expat.ExternalEntityParserCreate(context)
     parser.ParseFile(BytesIO(self._external_dtd))
     return 1
Exemplo n.º 23
0
 def test_multibyte_character_on_chunk_boundary(self):
     text = u'a' * ((4 * 1024) - 1) + u'\xe6'
     events = list(HTMLParser(BytesIO(text.encode('utf-8')),
                              encoding='utf-8'))
     self.assertEqual(1, len(events))
     self.assertEqual((Stream.TEXT, text), events[0][:2])
Exemplo n.º 24
0
 def test_render_output_stream_utf8(self):
     xml = XML("<li>Über uns</li>")
     strio = BytesIO()
     self.assertEqual(None, xml.render(encoding="utf-8", out=strio))
     self.assertEqual(u"<li>Über uns</li>".encode("utf-8"), strio.getvalue())
Exemplo n.º 25
0
 def test_pickle(self):
     markup = Markup('foo')
     buf = BytesIO()
     pickle.dump(markup, buf, 2)
     buf.seek(0)
     self.assertEquals("<Markup u'foo'>", repr(pickle.load(buf)))
Exemplo n.º 26
0
 def test_input_encoding_attribute(self):
     text = '<div title="\xf6"></div>'.encode('iso-8859-1')
     events = list(HTMLParser(BytesIO(text), encoding='iso-8859-1'))
     kind, (tag, attrib), pos = events[0]
     self.assertEqual(Stream.START, kind)
     self.assertEqual('\xf6', attrib.get('title'))
Exemplo n.º 27
0
 def test_pickle(self):
     markup = Markup('foo')
     buf = BytesIO()
     pickle.dump(markup, buf, 2)
     buf.seek(0)
     self.assertEquals("<Markup u'foo'>", repr(pickle.load(buf)))
Exemplo n.º 28
0
 def test_render_output_stream_utf8(self):
     xml = XML('<li>Über uns</li>')
     strio = BytesIO()
     self.assertEqual(None, xml.render(encoding='utf-8', out=strio))
     self.assertEqual('<li>Über uns</li>'.encode('utf-8'), strio.getvalue())
Exemplo n.º 29
0
 def test_latin1_encoded(self):
     text = '<div>\xf6</div>'.encode('iso-8859-1')
     events = list(XMLParser(BytesIO(text), encoding='iso-8859-1'))
     kind, data, pos = events[1]
     self.assertEqual(Stream.TEXT, kind)
     self.assertEqual('\xf6', data)
Exemplo n.º 30
0
 def test_input_encoding_text(self):
     text = u'<div>\xf6</div>'.encode('iso-8859-1')
     events = list(HTMLParser(BytesIO(text), encoding='iso-8859-1'))
     kind, data, pos = events[1]
     self.assertEqual(Stream.TEXT, kind)
     self.assertEqual(u'\xf6', data)
Exemplo n.º 31
0
 def test_render_output_stream_utf8(self):
     xml = XML('<li>Über uns</li>')
     strio = BytesIO()
     self.assertEqual(None, xml.render(encoding='utf-8', out=strio))
     self.assertEqual(u'<li>Über uns</li>'.encode('utf-8'), strio.getvalue())