mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-25 02:43:45 +03:00
Move tlobject and source_builder into their correct folders
This commit is contained in:
parent
463fcedf27
commit
fc1bc05ca1
5
setup.py
5
setup.py
|
@ -47,7 +47,8 @@ IMPORT_DEPTH = 2
|
||||||
|
|
||||||
def gen_tl(force=True):
|
def gen_tl(force=True):
|
||||||
from telethon_generator.tl_generator import TLGenerator
|
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)
|
generator = TLGenerator(GENERATOR_DIR)
|
||||||
if generator.tlobjects_exist():
|
if generator.tlobjects_exist():
|
||||||
if not force:
|
if not force:
|
||||||
|
@ -58,7 +59,7 @@ def gen_tl(force=True):
|
||||||
print('Generating TLObjects...')
|
print('Generating TLObjects...')
|
||||||
generator.generate_tlobjects(SCHEME_TL, import_depth=IMPORT_DEPTH)
|
generator.generate_tlobjects(SCHEME_TL, import_depth=IMPORT_DEPTH)
|
||||||
print('Generating errors...')
|
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.')
|
print('Done.')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
from .source_builder import SourceBuilder
|
|
||||||
from .tl_parser import TLParser
|
|
||||||
from .tl_object import TLObject
|
|
|
@ -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))
|
|
|
@ -1 +1,2 @@
|
||||||
from .errors import Error, parse_errors
|
from .errors import Error, parse_errors
|
||||||
|
from .tlobject import TLObject, parse_tl, find_layer
|
||||||
|
|
|
@ -321,3 +321,47 @@ class TLArg:
|
||||||
return str(self)\
|
return str(self)\
|
||||||
.replace(':date', ':int')\
|
.replace(':date', ':int')\
|
||||||
.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))
|
|
@ -5,7 +5,8 @@ import struct
|
||||||
from zlib import crc32
|
from zlib import crc32
|
||||||
from collections import defaultdict
|
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 = \
|
AUTO_GEN_NOTICE = \
|
||||||
'"""File generated by TLObjects\' generator. All changes will be ERASED"""'
|
'"""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)
|
os.makedirs(self._get_file('types'), exist_ok=True)
|
||||||
|
|
||||||
# Step 0: Cache the parsed file on a tuple
|
# 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
|
# Step 1: Group everything by {namespace: [tlobjects]} so we can
|
||||||
# easily generate __init__.py files with all the TLObjects on them.
|
# easily generate __init__.py files with all the TLObjects on them.
|
||||||
|
@ -98,7 +99,7 @@ class TLGenerator:
|
||||||
builder.writeln()
|
builder.writeln()
|
||||||
|
|
||||||
# Create a constant variable to indicate which layer this is
|
# 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()
|
builder.writeln()
|
||||||
|
|
||||||
# Then create the dictionary containing constructor_id: class
|
# Then create the dictionary containing constructor_id: class
|
||||||
|
|
Loading…
Reference in New Issue
Block a user