Edgewall Software

Changeset 804

Show
Ignore:
Timestamp:
03/12/2008 09:46:34 PM (8 months ago)
Author:
cmlenz
Message:

The Stream.render now accepts an optional out parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.

Location:
trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/ChangeLog

    r798 r804  
    4747 * Assigning to a variable named `data` in a Python code block no longer 
    4848   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. 
    4952 
    5053 
  • trunk/genshi/core.py

    r801 r804  
    150150        return reduce(operator.or_, (self,) + filters) 
    151151 
    152     def render(self, method=None, encoding='utf-8', **kwargs): 
     152    def render(self, method=None, encoding='utf-8', out=None, **kwargs): 
    153153        """Return a string representation of the stream. 
    154154         
     
    162162        :param encoding: how the output string should be encoded; if set to 
    163163                         `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 
    165170        :rtype: `basestring` 
     171         
    166172        :see: XMLSerializer, XHTMLSerializer, HTMLSerializer, TextSerializer 
     173        :note: Changed in 0.5: added the `out` parameter 
    167174        """ 
    168175        from genshi.output import encode 
     
    170177            method = self.serializer or 'xml' 
    171178        generator = self.serialize(method=method, **kwargs) 
    172         return encode(generator, method=method, encoding=encoding) 
     179        return encode(generator, method=method, encoding=encoding, out=out) 
    173180 
    174181    def select(self, path, namespaces=None, variables=None): 
  • trunk/genshi/output.py

    r788 r804  
    3131__docformat__ = 'restructuredtext en' 
    3232 
    33 def encode(iterator, method='xml', encoding='utf-8'): 
     33def encode(iterator, method='xml', encoding='utf-8', out=None): 
    3434    """Encode serializer output into a string. 
    3535     
     
    4040    :param encoding: how the output string should be encoded; if set to `None`, 
    4141                     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     
    4349    :since: version 0.4.1 
    44     """ 
    45     output = u''.join(list(iterator)) 
     50    :note: Changed in 0.5: added the `out` parameter 
     51    """ 
    4652    if encoding is not None: 
    4753        errors = 'replace' 
    4854        if method != 'text' and not isinstance(method, TextSerializer): 
    4955            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)) 
    5263 
    5364def get_serializer(method='xml', **kwargs): 
  • trunk/genshi/tests/core.py

    r782 r804  
    1515import pickle 
    1616from StringIO import StringIO 
     17try: 
     18    from cStringIO import StringIO as cStringIO 
     19except ImportError: 
     20    cStringIO = StringIO 
    1721import unittest 
    1822 
     
    3539        xml = XML('<li>Über uns</li>') 
    3640        self.assertEqual('<li>&#220;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()) 
    3753 
    3854    def test_pickle(self):