Commit d28ddc7e authored by Dag Sverre Seljebotn's avatar Dag Sverre Seljebotn

StringIOTree.insert

parent eaa6d477
...@@ -7,8 +7,7 @@ class StringIOTree(object): ...@@ -7,8 +7,7 @@ class StringIOTree(object):
def __init__(self, stream=None): def __init__(self, stream=None):
self.prepended_children = [] self.prepended_children = []
if stream is None: stream = StringIO() self.stream = stream # if set to None, it will be constructed on first write
self.stream = stream
def getvalue(self): def getvalue(self):
return ("".join([x.getvalue() for x in self.prepended_children]) + return ("".join([x.getvalue() for x in self.prepended_children]) +
...@@ -19,20 +18,44 @@ class StringIOTree(object): ...@@ -19,20 +18,44 @@ class StringIOTree(object):
needs to happen.""" needs to happen."""
for child in self.prepended_children: for child in self.prepended_children:
child.copyto(target) child.copyto(target)
if self.stream:
target.write(self.stream.getvalue()) target.write(self.stream.getvalue())
def write(self, what): def write(self, what):
if not self.stream:
self.stream = StringIO()
self.stream.write(what) self.stream.write(what)
def commit(self):
# Save what we have written until now so that the buffer
# itself is empty -- this makes it ready for insertion
if self.stream:
self.prepended_children.append(StringIOTree(self.stream))
self.stream = None
def insert(self, iotree):
"""
Insert a StringIOTree (and all of its contents) at this location.
Further writing to self appears after what is inserted.
"""
self.commit()
self.prepended_children.append(iotree)
def insertion_point(self): def insertion_point(self):
"""
Returns a new StringIOTree, which is left behind at the current position
(it what is written to the result will appear right before whatever is
next written to self).
Calling getvalue() or copyto() on the result will only return the
contents written to it.
"""
# Save what we have written until now # Save what we have written until now
# (would it be more efficient to check with len(self.stream.getvalue())? # This is so that getvalue on the result doesn't include it.
# leaving it out for now) self.commit()
self.prepended_children.append(StringIOTree(self.stream))
# Construct the new forked object to return # Construct the new forked object to return
other = StringIOTree() other = StringIOTree()
self.prepended_children.append(other) self.prepended_children.append(other)
self.stream = StringIO()
return other return other
__doc__ = r""" __doc__ = r"""
...@@ -65,12 +88,16 @@ beta ...@@ -65,12 +88,16 @@ beta
gamma gamma
<BLANKLINE> <BLANKLINE>
>>> i = StringIOTree()
>>> d.insert(i)
>>> i.write('inserted\n')
>>> out = StringIO() >>> out = StringIO()
>>> a.copyto(out) >>> a.copyto(out)
>>> print out.getvalue() >>> print out.getvalue()
first first
second second
alpha alpha
inserted
beta beta
gamma gamma
third third
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment