2016-08-26 20:12:20 +03:00
|
|
|
import re
|
2018-06-13 11:48:35 +03:00
|
|
|
import struct
|
2017-06-30 12:28:54 +03:00
|
|
|
from zlib import crc32
|
2016-08-26 20:12:20 +03:00
|
|
|
|
2018-04-14 18:22:39 +03:00
|
|
|
from ..utils import snake_to_camel_case
|
|
|
|
|
2018-04-14 17:05:09 +03:00
|
|
|
CORE_TYPES = (
|
|
|
|
0xbc799737, # boolFalse#bc799737 = Bool;
|
|
|
|
0x997275b5, # boolTrue#997275b5 = Bool;
|
|
|
|
0x3fedd339, # true#3fedd339 = True;
|
|
|
|
0x1cb5c415, # vector#1cb5c415 {t:Type} # [ t ] = Vector t;
|
|
|
|
)
|
2016-08-26 20:12:20 +03:00
|
|
|
|
2018-05-12 17:12:42 +03:00
|
|
|
# https://github.com/telegramdesktop/tdesktop/blob/4bf66cb6e93f3965b40084771b595e93d0b11bcd/Telegram/SourceFiles/codegen/scheme/codegen_scheme.py#L57-L62
|
|
|
|
WHITELISTED_MISMATCHING_IDS = {
|
|
|
|
# 0 represents any layer
|
2018-06-24 12:37:54 +03:00
|
|
|
0: {'ipPortSecret', 'accessPointRule', 'help.configSimple'}
|
2018-05-12 17:12:42 +03:00
|
|
|
}
|
2018-06-24 12:37:54 +03:00
|
|
|
for i in range(77, 82):
|
|
|
|
WHITELISTED_MISMATCHING_IDS[i] = {'channel'}
|
2018-05-12 17:12:42 +03:00
|
|
|
|
2016-11-23 23:03:58 +03:00
|
|
|
|
2018-04-14 17:05:09 +03:00
|
|
|
class TLObject:
|
2018-05-12 17:12:42 +03:00
|
|
|
def __init__(self, fullname, object_id, args, result, is_function, layer):
|
2016-08-26 20:12:20 +03:00
|
|
|
"""
|
|
|
|
Initializes a new TLObject, given its properties.
|
2018-04-14 17:05:09 +03:00
|
|
|
|
2016-08-26 20:12:20 +03:00
|
|
|
:param fullname: The fullname of the TL object (namespace.name)
|
2018-04-14 17:05:09 +03:00
|
|
|
The namespace can be omitted.
|
2016-11-30 00:29:42 +03:00
|
|
|
:param object_id: The hexadecimal string representing the object ID
|
2016-08-26 20:12:20 +03:00
|
|
|
:param args: The arguments, if any, of the TL object
|
|
|
|
:param result: The result type of the TL object
|
|
|
|
:param is_function: Is the object a function or a type?
|
2018-05-12 17:12:42 +03:00
|
|
|
:param layer: The layer this TLObject belongs to.
|
2016-08-26 20:12:20 +03:00
|
|
|
"""
|
|
|
|
# The name can or not have a namespace
|
2018-04-14 17:05:09 +03:00
|
|
|
self.fullname = fullname
|
2016-08-26 20:12:20 +03:00
|
|
|
if '.' in fullname:
|
2018-04-14 17:05:09 +03:00
|
|
|
self.namespace, self.name = fullname.split('.', maxsplit=1)
|
2016-08-26 20:12:20 +03:00
|
|
|
else:
|
2018-04-14 17:05:09 +03:00
|
|
|
self.namespace, self.name = None, fullname
|
2016-08-26 20:12:20 +03:00
|
|
|
|
|
|
|
self.args = args
|
|
|
|
self.result = result
|
|
|
|
self.is_function = is_function
|
2018-06-06 18:35:06 +03:00
|
|
|
self.bot_usable = None
|
2018-04-14 18:22:39 +03:00
|
|
|
self.id = None
|
2017-06-30 12:28:54 +03:00
|
|
|
if object_id is None:
|
|
|
|
self.id = self.infer_id()
|
|
|
|
else:
|
|
|
|
self.id = int(object_id, base=16)
|
2018-05-12 17:12:42 +03:00
|
|
|
whitelist = WHITELISTED_MISMATCHING_IDS[0] |\
|
|
|
|
WHITELISTED_MISMATCHING_IDS.get(layer, set())
|
|
|
|
|
|
|
|
if self.fullname not in whitelist:
|
|
|
|
assert self.id == self.infer_id(),\
|
|
|
|
'Invalid inferred ID for ' + repr(self)
|
2017-06-30 12:28:54 +03:00
|
|
|
|
2018-04-14 18:22:39 +03:00
|
|
|
self.class_name = snake_to_camel_case(
|
|
|
|
self.name, suffix='Request' if self.is_function else '')
|
2017-09-18 22:00:06 +03:00
|
|
|
|
2018-04-14 21:28:25 +03:00
|
|
|
self.real_args = list(a for a in self.sorted_args() if not
|
|
|
|
(a.flag_indicator or a.generic_definition))
|
|
|
|
|
2017-06-11 20:16:59 +03:00
|
|
|
def sorted_args(self):
|
|
|
|
"""Returns the arguments properly sorted and ready to plug-in
|
|
|
|
into a Python's method header (i.e., flags and those which
|
|
|
|
can be inferred will go last so they can default =None)
|
|
|
|
"""
|
|
|
|
return sorted(self.args,
|
|
|
|
key=lambda x: x.is_flag or x.can_be_inferred)
|
|
|
|
|
2017-06-30 12:28:54 +03:00
|
|
|
def __repr__(self, ignore_id=False):
|
2018-04-14 18:22:39 +03:00
|
|
|
if self.id is None or ignore_id:
|
2017-06-30 12:28:54 +03:00
|
|
|
hex_id = ''
|
|
|
|
else:
|
2018-04-14 18:22:39 +03:00
|
|
|
hex_id = '#{:08x}'.format(self.id)
|
2017-06-30 12:28:54 +03:00
|
|
|
|
|
|
|
if self.args:
|
|
|
|
args = ' ' + ' '.join([repr(arg) for arg in self.args])
|
|
|
|
else:
|
|
|
|
args = ''
|
2016-08-26 20:12:20 +03:00
|
|
|
|
2018-04-14 17:05:09 +03:00
|
|
|
return '{}{}{} = {}'.format(self.fullname, hex_id, args, self.result)
|
2017-06-30 12:28:54 +03:00
|
|
|
|
|
|
|
def infer_id(self):
|
|
|
|
representation = self.__repr__(ignore_id=True)
|
|
|
|
representation = representation\
|
|
|
|
.replace(':bytes ', ':string ')\
|
|
|
|
.replace('?bytes ', '?string ')\
|
|
|
|
.replace('<', ' ').replace('>', '')\
|
|
|
|
.replace('{', '').replace('}', '')
|
|
|
|
|
|
|
|
representation = re.sub(
|
|
|
|
r' \w+:flags\.\d+\?true',
|
|
|
|
r'',
|
|
|
|
representation
|
|
|
|
)
|
|
|
|
return crc32(representation.encode('ascii'))
|
2016-08-26 20:12:20 +03:00
|
|
|
|
2018-06-13 11:48:35 +03:00
|
|
|
def to_dict(self):
|
|
|
|
return {
|
|
|
|
'id':
|
|
|
|
str(struct.unpack('i', struct.pack('I', self.id))[0]),
|
|
|
|
'method' if self.is_function else 'predicate':
|
|
|
|
self.fullname,
|
|
|
|
'params':
|
|
|
|
[x.to_dict() for x in self.args if not x.generic_definition],
|
|
|
|
'type':
|
|
|
|
self.result
|
|
|
|
}
|
|
|
|
|
2016-09-05 20:12:14 +03:00
|
|
|
|
2016-08-26 20:12:20 +03:00
|
|
|
class TLArg:
|
2016-11-30 00:29:42 +03:00
|
|
|
def __init__(self, name, arg_type, generic_definition):
|
2016-08-26 20:12:20 +03:00
|
|
|
"""
|
|
|
|
Initializes a new .tl argument
|
|
|
|
:param name: The name of the .tl argument
|
2016-11-30 00:29:42 +03:00
|
|
|
:param arg_type: The type of the .tl argument
|
2016-08-26 20:12:20 +03:00
|
|
|
:param generic_definition: Is the argument a generic definition?
|
|
|
|
(i.e. {X:Type})
|
|
|
|
"""
|
2018-04-14 17:05:09 +03:00
|
|
|
self.name = 'is_self' if name == 'self' else name
|
2016-08-26 20:12:20 +03:00
|
|
|
|
2016-08-27 12:59:23 +03:00
|
|
|
# Default values
|
|
|
|
self.is_vector = False
|
2016-08-26 20:12:20 +03:00
|
|
|
self.is_flag = False
|
2017-10-16 21:15:22 +03:00
|
|
|
self.skip_constructor_id = False
|
2016-08-26 20:12:20 +03:00
|
|
|
self.flag_index = -1
|
|
|
|
|
2017-06-11 20:16:59 +03:00
|
|
|
# Special case: some types can be inferred, which makes it
|
|
|
|
# less annoying to type. Currently the only type that can
|
|
|
|
# be inferred is if the name is 'random_id', to which a
|
|
|
|
# random ID will be assigned if left as None (the default)
|
|
|
|
self.can_be_inferred = name == 'random_id'
|
|
|
|
|
2016-08-26 20:12:20 +03:00
|
|
|
# The type can be an indicator that other arguments will be flags
|
2016-11-30 00:29:42 +03:00
|
|
|
if arg_type == '#':
|
2016-08-26 20:12:20 +03:00
|
|
|
self.flag_indicator = True
|
|
|
|
self.type = None
|
|
|
|
self.is_generic = False
|
|
|
|
else:
|
|
|
|
self.flag_indicator = False
|
2016-11-30 00:29:42 +03:00
|
|
|
self.is_generic = arg_type.startswith('!')
|
2017-09-18 22:00:06 +03:00
|
|
|
# Strip the exclamation mark always to have only the name
|
|
|
|
self.type = arg_type.lstrip('!')
|
2016-08-26 20:12:20 +03:00
|
|
|
|
|
|
|
# The type may be a flag (flags.IDX?REAL_TYPE)
|
2018-04-14 17:05:09 +03:00
|
|
|
# Note that 'flags' is NOT the flags name; this
|
|
|
|
# is determined by a previous argument
|
2017-06-10 14:39:37 +03:00
|
|
|
# However, we assume that the argument will always be called 'flags'
|
2016-08-26 20:12:20 +03:00
|
|
|
flag_match = re.match(r'flags.(\d+)\?([\w<>.]+)', self.type)
|
|
|
|
if flag_match:
|
|
|
|
self.is_flag = True
|
|
|
|
self.flag_index = int(flag_match.group(1))
|
2017-06-10 14:39:37 +03:00
|
|
|
# Update the type to match the exact type, not the "flagged" one
|
|
|
|
self.type = flag_match.group(2)
|
2016-08-26 20:12:20 +03:00
|
|
|
|
2016-08-27 12:59:23 +03:00
|
|
|
# Then check if the type is a Vector<REAL_TYPE>
|
2017-10-16 20:19:16 +03:00
|
|
|
vector_match = re.match(r'[Vv]ector<([\w\d.]+)>', self.type)
|
2016-08-27 12:59:23 +03:00
|
|
|
if vector_match:
|
|
|
|
self.is_vector = True
|
2017-05-19 10:41:22 +03:00
|
|
|
|
|
|
|
# If the type's first letter is not uppercase, then
|
|
|
|
# it is a constructor and we use (read/write) its ID
|
|
|
|
# as pinpointed on issue #81.
|
|
|
|
self.use_vector_id = self.type[0] == 'V'
|
|
|
|
|
|
|
|
# Update the type to match the one inside the vector
|
|
|
|
self.type = vector_match.group(1)
|
2016-08-27 12:59:23 +03:00
|
|
|
|
2017-10-16 21:15:22 +03:00
|
|
|
# See use_vector_id. An example of such case is ipPort in
|
|
|
|
# help.configSpecial
|
|
|
|
if self.type.split('.')[-1][0].islower():
|
|
|
|
self.skip_constructor_id = True
|
|
|
|
|
2016-09-11 11:35:02 +03:00
|
|
|
# The name may contain "date" in it, if this is the case and the type is "int",
|
|
|
|
# we can safely assume that this should be treated as a "date" object.
|
|
|
|
# Note that this is not a valid Telegram object, but it's easier to work with
|
2017-05-19 10:30:30 +03:00
|
|
|
if self.type == 'int' and (
|
|
|
|
re.search(r'(\b|_)date\b', name) or
|
|
|
|
name in ('expires', 'expires_at', 'was_online')):
|
2016-09-11 11:35:02 +03:00
|
|
|
self.type = 'date'
|
|
|
|
|
2016-08-26 20:12:20 +03:00
|
|
|
self.generic_definition = generic_definition
|
|
|
|
|
2018-04-14 21:28:25 +03:00
|
|
|
def type_hint(self):
|
2018-03-12 12:58:56 +03:00
|
|
|
type = self.type
|
|
|
|
if '.' in type:
|
|
|
|
type = type.split('.')[1]
|
|
|
|
result = {
|
|
|
|
'int': 'int',
|
|
|
|
'long': 'int',
|
|
|
|
'int128': 'int',
|
|
|
|
'int256': 'int',
|
|
|
|
'string': 'str',
|
|
|
|
'date': 'Optional[datetime]', # None date = 0 timestamp
|
|
|
|
'bytes': 'bytes',
|
|
|
|
'true': 'bool',
|
|
|
|
}.get(type, "Type{}".format(type))
|
|
|
|
if self.is_vector:
|
|
|
|
result = 'List[{}]'.format(result)
|
|
|
|
if self.is_flag and type != 'date':
|
|
|
|
result = 'Optional[{}]'.format(result)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2018-06-13 11:48:35 +03:00
|
|
|
def real_type(self):
|
2016-08-27 12:59:23 +03:00
|
|
|
# Find the real type representation by updating it as required
|
|
|
|
real_type = self.type
|
2017-05-13 06:01:39 +03:00
|
|
|
if self.flag_indicator:
|
|
|
|
real_type = '#'
|
|
|
|
|
2016-08-27 12:59:23 +03:00
|
|
|
if self.is_vector:
|
2017-05-19 10:41:22 +03:00
|
|
|
if self.use_vector_id:
|
|
|
|
real_type = 'Vector<{}>'.format(real_type)
|
|
|
|
else:
|
|
|
|
real_type = 'vector<{}>'.format(real_type)
|
2016-08-27 12:59:23 +03:00
|
|
|
|
|
|
|
if self.is_generic:
|
|
|
|
real_type = '!{}'.format(real_type)
|
|
|
|
|
|
|
|
if self.is_flag:
|
|
|
|
real_type = 'flags.{}?{}'.format(self.flag_index, real_type)
|
2016-08-26 20:12:20 +03:00
|
|
|
|
2018-06-13 11:48:35 +03:00
|
|
|
return real_type
|
|
|
|
|
|
|
|
def __str__(self):
|
2016-08-26 20:12:20 +03:00
|
|
|
if self.generic_definition:
|
2018-06-13 11:48:35 +03:00
|
|
|
return '{{{}:{}}}'.format(self.name, self.real_type())
|
2016-08-26 20:12:20 +03:00
|
|
|
else:
|
2018-06-13 11:48:35 +03:00
|
|
|
return '{}:{}'.format(self.name, self.real_type())
|
2017-06-30 12:28:54 +03:00
|
|
|
|
|
|
|
def __repr__(self):
|
2018-04-14 17:05:09 +03:00
|
|
|
return str(self).replace(':date', ':int').replace('?date', '?int')
|
2018-04-14 16:16:13 +03:00
|
|
|
|
2018-06-13 11:48:35 +03:00
|
|
|
def to_dict(self):
|
|
|
|
return {
|
|
|
|
'name': self.name.replace('is_self', 'self'),
|
|
|
|
'type': re.sub(r'\bdate$', 'int', self.real_type())
|
|
|
|
}
|
|
|
|
|
2018-04-14 16:16:13 +03:00
|
|
|
|
2018-05-12 17:12:42 +03:00
|
|
|
def _from_line(line, is_function, layer):
|
2018-04-14 17:05:09 +03:00
|
|
|
match = re.match(
|
|
|
|
r'^([\w.]+)' # 'name'
|
|
|
|
r'(?:#([0-9a-fA-F]+))?' # '#optionalcode'
|
|
|
|
r'(?:\s{?\w+:[\w\d<>#.?!]+}?)*' # '{args:.0?type}'
|
|
|
|
r'\s=\s' # ' = '
|
|
|
|
r'([\w\d<>#.?]+);$', # '<result.type>;'
|
|
|
|
line
|
|
|
|
)
|
|
|
|
if match is None:
|
|
|
|
# Probably "vector#1cb5c415 {t:Type} # [ t ] = Vector t;"
|
|
|
|
raise ValueError('Cannot parse TLObject {}'.format(line))
|
|
|
|
|
|
|
|
args_match = re.findall(
|
|
|
|
r'({)?'
|
|
|
|
r'(\w+)'
|
|
|
|
r':'
|
|
|
|
r'([\w\d<>#.?!]+)'
|
|
|
|
r'}?',
|
|
|
|
line
|
|
|
|
)
|
|
|
|
return TLObject(
|
|
|
|
fullname=match.group(1),
|
|
|
|
object_id=match.group(2),
|
|
|
|
result=match.group(3),
|
|
|
|
is_function=is_function,
|
2018-05-12 17:12:42 +03:00
|
|
|
layer=layer,
|
2018-04-14 17:05:09 +03:00
|
|
|
args=[TLArg(name, arg_type, brace != '')
|
|
|
|
for brace, name, arg_type in args_match]
|
|
|
|
)
|
|
|
|
|
2018-04-14 16:16:13 +03:00
|
|
|
|
2018-06-13 11:48:35 +03:00
|
|
|
def parse_tl(file_path, layer, invalid_bot_methods=None):
|
2018-04-14 17:05:09 +03:00
|
|
|
"""This method yields TLObjects from a given .tl file."""
|
2018-06-06 18:35:06 +03:00
|
|
|
if invalid_bot_methods is None:
|
|
|
|
invalid_bot_methods = set()
|
|
|
|
|
2018-04-14 16:16:13 +03:00
|
|
|
with open(file_path, encoding='utf-8') as file:
|
|
|
|
is_function = False
|
|
|
|
for line in file:
|
|
|
|
comment_index = line.find('//')
|
|
|
|
if comment_index != -1:
|
|
|
|
line = line[:comment_index]
|
|
|
|
|
|
|
|
line = line.strip()
|
2018-04-14 17:05:09 +03:00
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
|
|
|
|
match = re.match('---(\w+)---', line)
|
|
|
|
if match:
|
|
|
|
following_types = match.group(1)
|
|
|
|
is_function = following_types == 'functions'
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2018-05-12 17:12:42 +03:00
|
|
|
result = _from_line(line, is_function, layer=layer)
|
2018-06-13 11:48:35 +03:00
|
|
|
result.bot_usable = result.fullname not in invalid_bot_methods
|
|
|
|
yield result
|
2018-04-14 17:05:09 +03:00
|
|
|
except ValueError as e:
|
|
|
|
if 'vector#1cb5c415' not in str(e):
|
|
|
|
raise
|
2018-04-14 16:16:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
def find_layer(file_path):
|
2018-04-14 17:05:09 +03:00
|
|
|
"""Finds the layer used on the specified scheme.tl file."""
|
2018-04-14 16:16:13 +03:00
|
|
|
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))
|