Changeset 804
- Timestamp:
- 03/12/2008 09:46:34 PM (8 months ago)
- Location:
- trunk
- Files:
-
- 4 modified
-
ChangeLog (modified) (1 diff)
-
genshi/core.py (modified) (3 diffs)
-
genshi/output.py (modified) (2 diffs)
-
genshi/tests/core.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ChangeLog
r798 r804 47 47 * Assigning to a variable named `data` in a Python code block no longer 48 48 breaks context lookup. 49 * The `Stream.render` now accepts an optional `out` parameter that can be 50 used to pass in a writable file-like object to use for assembling the 51 output, instead of building a big string and returning it. 49 52 50 53 -
trunk/genshi/core.py
r801 r804 150 150 return reduce(operator.or_, (self,) + filters) 151 151 152 def render(self, method=None, encoding='utf-8', **kwargs):152 def render(self, method=None, encoding='utf-8', out=None, **kwargs): 153 153 """Return a string representation of the stream. 154 154 … … 162 162 :param encoding: how the output string should be encoded; if set to 163 163 `None`, this method returns a `unicode` object 164 :return: a `str` or `unicode` object 164 :param out: a file-like object that the output should be written to 165 instead of being returned as one big string; note that if 166 this is a file or socket (or similar), the `encoding` must 167 not be `None` (that is, the output must be encoded) 168 :return: a `str` or `unicode` object (depending on the `encoding` 169 parameter), or `None` if the `out` parameter is provided 165 170 :rtype: `basestring` 171 166 172 :see: XMLSerializer, XHTMLSerializer, HTMLSerializer, TextSerializer 173 :note: Changed in 0.5: added the `out` parameter 167 174 """ 168 175 from genshi.output import encode … … 170 177 method = self.serializer or 'xml' 171 178 generator = self.serialize(method=method, **kwargs) 172 return encode(generator, method=method, encoding=encoding )179 return encode(generator, method=method, encoding=encoding, out=out) 173 180 174 181 def select(self, path, namespaces=None, variables=None): -
trunk/genshi/output.py
r788 r804 31 31 __docformat__ = 'restructuredtext en' 32 32 33 def encode(iterator, method='xml', encoding='utf-8' ):33 def encode(iterator, method='xml', encoding='utf-8', out=None): 34 34 """Encode serializer output into a string. 35 35 … … 40 40 :param encoding: how the output string should be encoded; if set to `None`, 41 41 this method returns a `unicode` object 42 :return: a string or unicode object (depending on the `encoding` parameter) 42 :param out: a file-like object that the output should be written to 43 instead of being returned as one big string; note that if 44 this is a file or socket (or similar), the `encoding` must 45 not be `None` (that is, the output must be encoded) 46 :return: a `str` or `unicode` object (depending on the `encoding` 47 parameter), or `None` if the `out` parameter is provided 48 43 49 :since: version 0.4.1 44 """45 output = u''.join(list(iterator))50 :note: Changed in 0.5: added the `out` parameter 51 """ 46 52 if encoding is not None: 47 53 errors = 'replace' 48 54 if method != 'text' and not isinstance(method, TextSerializer): 49 55 errors = 'xmlcharrefreplace' 50 return output.encode(encoding, errors) 51 return output 56 _encode = lambda string: string.encode(encoding, errors) 57 else: 58 _encode = lambda string: string 59 if out is None: 60 return _encode(u''.join(list(iterator))) 61 for chunk in iterator: 62 out.write(_encode(chunk)) 52 63 53 64 def get_serializer(method='xml', **kwargs): -
trunk/genshi/tests/core.py
r782 r804 15 15 import pickle 16 16 from StringIO import StringIO 17 try: 18 from cStringIO import StringIO as cStringIO 19 except ImportError: 20 cStringIO = StringIO 17 21 import unittest 18 22 … … 35 39 xml = XML('<li>Über uns</li>') 36 40 self.assertEqual('<li>Über uns</li>', xml.render(encoding='ascii')) 41 42 def test_render_output_stream_utf8(self): 43 xml = XML('<li>Über uns</li>') 44 strio = cStringIO() 45 self.assertEqual(None, xml.render(out=strio)) 46 self.assertEqual('<li>Über uns</li>', strio.getvalue()) 47 48 def test_render_output_stream_unicode(self): 49 xml = XML('<li>Über uns</li>') 50 strio = StringIO() 51 self.assertEqual(None, xml.render(encoding=None, out=strio)) 52 self.assertEqual(u'<li>Über uns</li>', strio.getvalue()) 37 53 38 54 def test_pickle(self):
