diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /contrib/tools/cython/Cython/StringIOTree.py | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/tools/cython/Cython/StringIOTree.py')
-rw-r--r-- | contrib/tools/cython/Cython/StringIOTree.py | 134 |
1 files changed, 67 insertions, 67 deletions
diff --git a/contrib/tools/cython/Cython/StringIOTree.py b/contrib/tools/cython/Cython/StringIOTree.py index 097bbc10ec..d8239efeda 100644 --- a/contrib/tools/cython/Cython/StringIOTree.py +++ b/contrib/tools/cython/Cython/StringIOTree.py @@ -39,70 +39,70 @@ try: from cStringIO import StringIO except ImportError: from io import StringIO - - -class StringIOTree(object): - """ - See module docs. - """ - - def __init__(self, stream=None): - self.prepended_children = [] - if stream is None: - stream = StringIO() - self.stream = stream - self.write = stream.write - self.markers = [] - - def getvalue(self): - content = [x.getvalue() for x in self.prepended_children] - content.append(self.stream.getvalue()) - return "".join(content) - - def copyto(self, target): - """Potentially cheaper than getvalue as no string concatenation - needs to happen.""" - for child in self.prepended_children: - child.copyto(target) - stream_content = self.stream.getvalue() - if stream_content: - target.write(stream_content) - - 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.tell(): - self.prepended_children.append(StringIOTree(self.stream)) - self.prepended_children[-1].markers = self.markers - self.markers = [] - self.stream = StringIO() - self.write = self.stream.write - - 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): - """ - 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 - # This is so that getvalue on the result doesn't include it. - self.commit() - # Construct the new forked object to return - other = StringIOTree() - self.prepended_children.append(other) - return other - - def allmarkers(self): - children = self.prepended_children - return [m for c in children for m in c.allmarkers()] + self.markers + + +class StringIOTree(object): + """ + See module docs. + """ + + def __init__(self, stream=None): + self.prepended_children = [] + if stream is None: + stream = StringIO() + self.stream = stream + self.write = stream.write + self.markers = [] + + def getvalue(self): + content = [x.getvalue() for x in self.prepended_children] + content.append(self.stream.getvalue()) + return "".join(content) + + def copyto(self, target): + """Potentially cheaper than getvalue as no string concatenation + needs to happen.""" + for child in self.prepended_children: + child.copyto(target) + stream_content = self.stream.getvalue() + if stream_content: + target.write(stream_content) + + 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.tell(): + self.prepended_children.append(StringIOTree(self.stream)) + self.prepended_children[-1].markers = self.markers + self.markers = [] + self.stream = StringIO() + self.write = self.stream.write + + 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): + """ + 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 + # This is so that getvalue on the result doesn't include it. + self.commit() + # Construct the new forked object to return + other = StringIOTree() + self.prepended_children.append(other) + return other + + def allmarkers(self): + children = self.prepended_children + return [m for c in children for m in c.allmarkers()] + self.markers |