Move tlobject and source_builder into their correct folders

This commit is contained in:
Lonami Exo 2018-04-14 15:16:13 +02:00
parent 463fcedf27
commit fc1bc05ca1
7 changed files with 52 additions and 59 deletions

View File

@ -47,7 +47,8 @@ IMPORT_DEPTH = 2
def gen_tl(force=True):
from telethon_generator.tl_generator import TLGenerator
from telethon_generator.error_generator import generate_code
# TODO Generate errors again
#from telethon_generator.error_generator import generate_code
generator = TLGenerator(GENERATOR_DIR)
if generator.tlobjects_exist():
if not force:
@ -58,7 +59,7 @@ def gen_tl(force=True):
print('Generating TLObjects...')
generator.generate_tlobjects(SCHEME_TL, import_depth=IMPORT_DEPTH)
print('Generating errors...')
generate_code(ERROR_LIST, json_file=ERRORS_JSON, errors_desc=ERRORS_DESC)
#generate_code(ERROR_LIST, json_file=ERRORS_JSON, errors_desc=ERRORS_DESC)
print('Done.')

View File

@ -1,3 +0,0 @@
from .source_builder import SourceBuilder
from .tl_parser import TLParser
from .tl_object import TLObject

View File

@ -1,51 +0,0 @@
import re
from .tl_object import TLObject
class TLParser:
"""Class used to parse .tl files"""
@staticmethod
def parse_file(file_path, ignore_core=False):
"""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:
# Strip comments from the line
comment_index = line.find('//')
if comment_index != -1:
line = line[:comment_index]
line = line.strip()
if line:
# 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:
try:
result = TLObject.from_tl(line, is_function)
if not ignore_core or not result.is_core_type():
yield result
except ValueError as e:
if 'vector#1cb5c415' not in str(e):
raise
@staticmethod
def find_layer(file_path):
"""Finds the layer used on the specified scheme.tl file"""
layer_regex = re.compile(r'^//\s*LAYER\s*(\d+)$')
with open(file_path, encoding='utf-8') as file:
for line in file:
match = layer_regex.match(line)
if match:
return int(match.group(1))

View File

@ -1 +1,2 @@
from .errors import Error, parse_errors
from .tlobject import TLObject, parse_tl, find_layer

View File

@ -321,3 +321,47 @@ class TLArg:
return str(self)\
.replace(':date', ':int')\
.replace('?date', '?int')
def parse_tl(file_path, ignore_core=False):
"""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:
# Strip comments from the line
comment_index = line.find('//')
if comment_index != -1:
line = line[:comment_index]
line = line.strip()
if line:
# 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:
try:
result = TLObject.from_tl(line, is_function)
if not ignore_core or not result.is_core_type():
yield result
except ValueError as e:
if 'vector#1cb5c415' not in str(e):
raise
def find_layer(file_path):
"""Finds the layer used on the specified scheme.tl file"""
layer_regex = re.compile(r'^//\s*LAYER\s*(\d+)$')
with open(file_path, encoding='utf-8') as file:
for line in file:
match = layer_regex.match(line)
if match:
return int(match.group(1))

View File

@ -5,7 +5,8 @@ import struct
from zlib import crc32
from collections import defaultdict
from .parser import SourceBuilder, TLParser, TLObject
from .source_builder import SourceBuilder
from .parsers import TLObject, parse_tl, find_layer
AUTO_GEN_NOTICE = \
'"""File generated by TLObjects\' generator. All changes will be ERASED"""'
@ -59,7 +60,7 @@ class TLGenerator:
os.makedirs(self._get_file('types'), exist_ok=True)
# Step 0: Cache the parsed file on a tuple
tlobjects = tuple(TLParser.parse_file(scheme_file, ignore_core=True))
tlobjects = tuple(parse_tl(scheme_file, ignore_core=True))
# Step 1: Group everything by {namespace: [tlobjects]} so we can
# easily generate __init__.py files with all the TLObjects on them.
@ -98,7 +99,7 @@ class TLGenerator:
builder.writeln()
# Create a constant variable to indicate which layer this is
builder.writeln('LAYER = {}', TLParser.find_layer(scheme_file))
builder.writeln('LAYER = {}', find_layer(scheme_file))
builder.writeln()
# Then create the dictionary containing constructor_id: class