mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-06-19 03:26:39 +00:00

A TLObject class has been added, as well as a method for tokenizen .tl files. A TLObject can be created by simply calling `TLObject.from_tl(tl_line)`, this will tokenize the .tl line and return a new TLObject with all the required properties extracted. Also added another method to load .tl files and yield TLObjects, including a SourceBuilder that can be later used to generate Python code.
39 lines
995 B
Python
39 lines
995 B
Python
|
|
class SourceBuilder:
|
|
"""This class should be used to build .py source files"""
|
|
|
|
def __init__(self, indent_size=4):
|
|
self.current_indent = 0
|
|
self.on_new_line = False
|
|
self.indent_size = indent_size
|
|
|
|
self.buffer = []
|
|
|
|
def indent(self):
|
|
self.write(' ' * (self.current_indent * self.indent_size))
|
|
|
|
def write(self, string):
|
|
if self.on_new_line:
|
|
self.on_new_line = False # We're not on a new line anymore
|
|
self.indent()
|
|
|
|
self.buffer += list(string)
|
|
|
|
def writeln(self, string=''):
|
|
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):
|
|
self.current_indent -= 1
|
|
self.writeln()
|
|
|
|
def __str__(self):
|
|
if self.buffer:
|
|
return ''.join(self.buffer)
|
|
else:
|
|
return ''
|