Mercurial > genshi > genshi-test
comparison genshi/core.py @ 932:e53161c2773c
Merge r1140 from 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 | Fri, 18 Mar 2011 09:08:12 +0000 |
parents | 8cef75b02ac1 |
children |
comparison
equal
deleted
inserted
replaced
931:ade3abe742e9 | 932:e53161c2773c |
---|---|
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): |
727 return self | 733 return self |
728 | 734 |
729 def __getnewargs__(self): | 735 def __getnewargs__(self): |
730 return (self.lstrip('{'),) | 736 return (self.lstrip('{'),) |
731 | 737 |
732 def __repr__(self): | 738 if sys.version_info[0] == 2: |
733 return '%s(%s)' % (type(self).__name__, stringrepr(self.lstrip('{'))) | 739 # Only use stringrepr in python 2 |
740 def __repr__(self): | |
741 return '%s(%s)' % (type(self).__name__, stringrepr(self.lstrip('{'))) | |
742 else: | |
743 def __repr__(self): | |
744 return '%s(%r)' % (type(self).__name__, self.lstrip('{')) |