Implemented init and write code on TLObjects Generator

The code generated by the generator now classifies the output files
in their corresponding categories, also writing their __init__(...)
with documented arguments, and the on_send(...) method
This commit is contained in:
Lonami
2016-08-27 11:59:23 +02:00
parent 1974569927
commit 06832f8108
8 changed files with 310 additions and 37 deletions

View File

@@ -1,13 +1,18 @@
from io import StringIO
class SourceBuilder:
"""This class should be used to build .py source files"""
def __init__(self, indent_size=4):
def __init__(self, out_stream=None, indent_size=4):
self.current_indent = 0
self.on_new_line = False
self.indent_size = indent_size
self.buffer = []
if out_stream is None:
self.out_stream = StringIO()
else:
self.out_stream = out_stream
def indent(self):
self.write(' ' * (self.current_indent * self.indent_size))
@@ -15,9 +20,10 @@ class SourceBuilder:
def write(self, string):
if self.on_new_line:
self.on_new_line = False # We're not on a new line anymore
self.indent()
if string.strip(): # If the string was not empty, indent; Else it probably was a new line
self.indent()
self.buffer += list(string)
self.out_stream.write(string)
def writeln(self, string=''):
self.write(string + '\n')
@@ -32,7 +38,12 @@ class SourceBuilder:
self.writeln()
def __str__(self):
if self.buffer:
return ''.join(self.buffer)
else:
return ''
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()