You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.3 KiB

4 years ago
  1. # cython: auto_pickle=False
  2. r"""
  3. Implements a buffer with insertion points. When you know you need to
  4. "get back" to a place and write more later, simply call insertion_point()
  5. at that spot and get a new StringIOTree object that is "left behind".
  6. EXAMPLE:
  7. >>> a = StringIOTree()
  8. >>> _= a.write('first\n')
  9. >>> b = a.insertion_point()
  10. >>> _= a.write('third\n')
  11. >>> _= b.write('second\n')
  12. >>> a.getvalue().split()
  13. ['first', 'second', 'third']
  14. >>> c = b.insertion_point()
  15. >>> d = c.insertion_point()
  16. >>> _= d.write('alpha\n')
  17. >>> _= b.write('gamma\n')
  18. >>> _= c.write('beta\n')
  19. >>> b.getvalue().split()
  20. ['second', 'alpha', 'beta', 'gamma']
  21. >>> i = StringIOTree()
  22. >>> d.insert(i)
  23. >>> _= i.write('inserted\n')
  24. >>> out = StringIO()
  25. >>> a.copyto(out)
  26. >>> out.getvalue().split()
  27. ['first', 'second', 'alpha', 'inserted', 'beta', 'gamma', 'third']
  28. """
  29. from __future__ import absolute_import #, unicode_literals
  30. try:
  31. # Prefer cStringIO since io.StringIO() does not support writing 'str' in Py2.
  32. from cStringIO import StringIO
  33. except ImportError:
  34. from io import StringIO
  35. class StringIOTree(object):
  36. """
  37. See module docs.
  38. """
  39. def __init__(self, stream=None):
  40. self.prepended_children = []
  41. if stream is None:
  42. stream = StringIO()
  43. self.stream = stream
  44. self.write = stream.write
  45. self.markers = []
  46. def getvalue(self):
  47. content = [x.getvalue() for x in self.prepended_children]
  48. content.append(self.stream.getvalue())
  49. return "".join(content)
  50. def copyto(self, target):
  51. """Potentially cheaper than getvalue as no string concatenation
  52. needs to happen."""
  53. for child in self.prepended_children:
  54. child.copyto(target)
  55. stream_content = self.stream.getvalue()
  56. if stream_content:
  57. target.write(stream_content)
  58. def commit(self):
  59. # Save what we have written until now so that the buffer
  60. # itself is empty -- this makes it ready for insertion
  61. if self.stream.tell():
  62. self.prepended_children.append(StringIOTree(self.stream))
  63. self.prepended_children[-1].markers = self.markers
  64. self.markers = []
  65. self.stream = StringIO()
  66. self.write = self.stream.write
  67. def insert(self, iotree):
  68. """
  69. Insert a StringIOTree (and all of its contents) at this location.
  70. Further writing to self appears after what is inserted.
  71. """
  72. self.commit()
  73. self.prepended_children.append(iotree)
  74. def insertion_point(self):
  75. """
  76. Returns a new StringIOTree, which is left behind at the current position
  77. (it what is written to the result will appear right before whatever is
  78. next written to self).
  79. Calling getvalue() or copyto() on the result will only return the
  80. contents written to it.
  81. """
  82. # Save what we have written until now
  83. # This is so that getvalue on the result doesn't include it.
  84. self.commit()
  85. # Construct the new forked object to return
  86. other = StringIOTree()
  87. self.prepended_children.append(other)
  88. return other
  89. def allmarkers(self):
  90. children = self.prepended_children
  91. return [m for c in children for m in c.allmarkers()] + self.markers