mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-24 18:33:44 +03:00
1974569927
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.
31 lines
1.0 KiB
Python
31 lines
1.0 KiB
Python
import re
|
|
from tl.tlobject import TLObject
|
|
|
|
|
|
class TLParser:
|
|
"""Class used to parse .tl files"""
|
|
|
|
@staticmethod
|
|
def parse_file(file_path):
|
|
"""This method yields TLObjects from a given .tl file"""
|
|
|
|
with open(file_path, encoding='utf-8') as file:
|
|
# Start by assuming that the next found line won't be a function (and will hence be a type)
|
|
is_function = False
|
|
|
|
# Read all the lines from the .tl file
|
|
for line in file:
|
|
line = line.strip()
|
|
|
|
# Ensure that the line is not a comment
|
|
if line and not line.startswith('//'):
|
|
|
|
# Check whether the line is a type change (types ⋄ functions) or not
|
|
match = re.match('---(\w+)---', line)
|
|
if match:
|
|
following_types = match.group(1)
|
|
is_function = following_types == 'functions'
|
|
|
|
else:
|
|
yield TLObject.from_tl(line, is_function)
|