<s> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from io import StringIO <EOL> class StringIOTree ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream = None ) : <EOL> self . prepended_children = [ ] <EOL> if stream is None : <EOL> stream = StringIO ( ) <EOL> self . stream = stream <EOL> self . write = stream . write <EOL> self . markers = [ ] <EOL> def getvalue ( self ) : <EOL> content = [ x . getvalue ( ) for x in self . prepended_children ] <EOL> content . append ( self . stream . getvalue ( ) ) <EOL> return "<STR_LIT>" . join ( content ) <EOL> def copyto ( self , target ) : <EOL> """<STR_LIT>""" <EOL> for child in self . prepended_children : <EOL> child . copyto ( target ) <EOL> stream_content = self . stream . getvalue ( ) <EOL> if stream_content : <EOL> target . write ( stream_content ) <EOL> def commit ( self ) : <EOL> if self . stream . tell ( ) : <EOL> self . prepended_children . append ( StringIOTree ( self . stream ) ) <EOL> self . prepended_children [ - <NUM_LIT:1> ] . markers = self . markers <EOL> self . markers = [ ] <EOL> self . stream = StringIO ( ) <EOL> self . write = self . stream . write <EOL> def insert ( self , iotree ) : <EOL> """<STR_LIT>""" <EOL> self . commit ( ) <EOL> self . prepended_children . append ( iotree ) <EOL> def insertion_point ( self ) : <EOL> """<STR_LIT>""" <EOL> self . commit ( ) <EOL> other = StringIOTree ( ) <EOL> self . prepended_children . append ( other ) <EOL> return other <EOL> def allmarkers ( self ) : <EOL> children = self . prepended_children <EOL> return [ m for c in children for m in c . allmarkers ( ) ] + self . markers <EOL> __doc__ = r"""<STR_LIT>""" </s>
