__revision__ = "$Id: TemplateIO.py,v 1.6 2001/08/23 19:08:44 nascheme Exp $"

class TemplateIO:
    """Collect output for PTL scripts.
    
    Behaves similar to the StringIO class except that str() is called on
    input and None objects are not added to the output.
    """

    def __init__(self):
        self.data = []

    def __iadd__(self, other):
        if other is not None:
            self.data.append(other)
        return self

    def __add__(self, other):
        result = TemplateIO()
        result.data = self.data[:]
        result.data.append(other)
        return result
            
    def __radd__(self, other):
        result = TemplateIO()
        result.data = [other]
        result.data.extend(self.data)
        return result
        
    def __str__(self):
        return ''.join(map(str, self.data))

    def __repr__(self):
        return ("<%s at %x: %d chunks>" %
                (self.__class__.__name__, id(self), len(self.data)))
