Telethon/parser/source_builder.py

47 lines
1.6 KiB
Python
Raw Normal View History

class SourceBuilder:
"""This class should be used to build .py source files"""
2016-08-28 14:43:00 +03:00
def __init__(self, out_stream, indent_size=4):
self.current_indent = 0
self.on_new_line = False
self.indent_size = indent_size
2016-08-28 14:43:00 +03:00
self.out_stream = out_stream
def indent(self):
2016-08-28 14:43:00 +03:00
"""Indents the current source code line by the current indentation level"""
self.write(' ' * (self.current_indent * self.indent_size))
def write(self, string):
2016-08-28 14:43:00 +03:00
"""Writes a string into the source code, applying indentation if required"""
if self.on_new_line:
self.on_new_line = False # We're not on a new line anymore
if string.strip(): # If the string was not empty, indent; Else it probably was a new line
self.indent()
self.out_stream.write(string)
def writeln(self, string=''):
2016-08-28 14:43:00 +03:00
"""Writes a string into the source code _and_ appends a new line, applying indentation if required"""
self.write(string + '\n')
self.on_new_line = True
# If we're writing a block, increment indent for the next time
if string and string[-1] == ':':
self.current_indent += 1
def end_block(self):
2016-08-28 14:43:00 +03:00
"""Ends an indentation block, leaving an empty line afterwards"""
self.current_indent -= 1
self.writeln()
def __str__(self):
self.out_stream.seek(0)
return self.out_stream.read()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.out_stream.flush()
self.out_stream.close()