comparison genshi/core.py @ 915:9fafb35032a1 experimental-py3k

add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output): * default input and output encodings changed from UTF-8 to None (i.e. unicode strings) * Namespace and QName objects do not call stringrepr in __repr__ in Python 3 since repr() returns a unicode string there. * track changes to expat parser in Python 3 (mostly it accepts bytes instead of strings)
author hodgestar
date Sun, 24 Oct 2010 22:08:11 +0000
parents 2772fa7c10a3
children f4bf11c716cc
comparison
equal deleted inserted replaced
914:c5faa881d87f 915:9fafb35032a1
15 15
16 try: 16 try:
17 reduce # builtin in Python < 3 17 reduce # builtin in Python < 3
18 except NameError: 18 except NameError:
19 from functools import reduce 19 from functools import reduce
20 import sys
20 from itertools import chain 21 from itertools import chain
21 import operator 22 import operator
22 23
23 from genshi.util import plaintext, stripentities, striptags, stringrepr 24 from genshi.util import plaintext, stripentities, striptags, stringrepr
24 25
90 to the stream, providing a syntax similar to pipes on Unix shells. 91 to the stream, providing a syntax similar to pipes on Unix shells.
91 92
92 Assume the following stream produced by the `HTML` function: 93 Assume the following stream produced by the `HTML` function:
93 94
94 >>> from genshi.input import HTML 95 >>> from genshi.input import HTML
95 >>> html = HTML('''<p onclick="alert('Whoa')">Hello, world!</p>''') 96 >>> html = HTML('''<p onclick="alert('Whoa')">Hello, world!</p>''', encoding='utf-8')
96 >>> print(html) 97 >>> print(html)
97 <p onclick="alert('Whoa')">Hello, world!</p> 98 <p onclick="alert('Whoa')">Hello, world!</p>
98 99
99 A filter such as the HTML sanitizer can be applied to that stream using 100 A filter such as the HTML sanitizer can be applied to that stream using
100 the pipe notation as follows: 101 the pipe notation as follows:
151 :return: the filtered stream 152 :return: the filtered stream
152 :rtype: `Stream` 153 :rtype: `Stream`
153 """ 154 """
154 return reduce(operator.or_, (self,) + filters) 155 return reduce(operator.or_, (self,) + filters)
155 156
156 def render(self, method=None, encoding='utf-8', out=None, **kwargs): 157 def render(self, method=None, encoding=None, out=None, **kwargs):
157 """Return a string representation of the stream. 158 """Return a string representation of the stream.
158 159
159 Any additional keyword arguments are passed to the serializer, and thus 160 Any additional keyword arguments are passed to the serializer, and thus
160 depend on the `method` parameter value. 161 depend on the `method` parameter value.
161 162
185 def select(self, path, namespaces=None, variables=None): 186 def select(self, path, namespaces=None, variables=None):
186 """Return a new stream that contains the events matching the given 187 """Return a new stream that contains the events matching the given
187 XPath expression. 188 XPath expression.
188 189
189 >>> from genshi import HTML 190 >>> from genshi import HTML
190 >>> stream = HTML('<doc><elem>foo</elem><elem>bar</elem></doc>') 191 >>> stream = HTML('<doc><elem>foo</elem><elem>bar</elem></doc>', encoding='utf-8')
191 >>> print(stream.select('elem')) 192 >>> print(stream.select('elem'))
192 <elem>foo</elem><elem>bar</elem> 193 <elem>foo</elem><elem>bar</elem>
193 >>> print(stream.select('elem/text()')) 194 >>> print(stream.select('elem/text()'))
194 foobar 195 foobar
195 196
665 __getattr__ = __getitem__ 666 __getattr__ = __getitem__
666 667
667 def __hash__(self): 668 def __hash__(self):
668 return hash(self.uri) 669 return hash(self.uri)
669 670
670 def __repr__(self): 671 if sys.version_info[0] == 2:
671 return '%s(%s)' % (type(self).__name__, stringrepr(self.uri)) 672 # Only use stringrepr in python 2
673 def __repr__(self):
674 return '%s(%s)' % (type(self).__name__, stringrepr(self.uri))
675 else:
676 def __repr__(self):
677 return '%s(%r)' % (type(self).__name__, self.uri)
672 678
673 def __str__(self): 679 def __str__(self):
674 return self.uri.encode('utf-8') 680 return self.uri.encode('utf-8')
675 681
676 def __unicode__(self): 682 def __unicode__(self):
726 return self 732 return self
727 733
728 def __getnewargs__(self): 734 def __getnewargs__(self):
729 return (self.lstrip('{'),) 735 return (self.lstrip('{'),)
730 736
731 def __repr__(self): 737 if sys.version_info[0] == 2:
732 return '%s(%s)' % (type(self).__name__, stringrepr(self.lstrip('{'))) 738 # Only use stringrepr in python 2
739 def __repr__(self):
740 return '%s(%s)' % (type(self).__name__, stringrepr(self.lstrip('{')))
741 else:
742 def __repr__(self):
743 return '%s(%r)' % (type(self).__name__, self.lstrip('{'))
Copyright (C) 2012-2017 Edgewall Software