2016-08-26 20:12:20 +03:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
class TLObject:
|
2016-11-23 23:03:58 +03:00
|
|
|
""".tl core types IDs (such as vector, booleans, etc.)"""
|
|
|
|
CORE_TYPES = (0x1cb5c415, 0xbc799737, 0x997275b5, 0x3fedd339)
|
|
|
|
|
2016-11-30 00:29:42 +03:00
|
|
|
def __init__(self, fullname, object_id, args, result, is_function):
|
2016-08-26 20:12:20 +03:00
|
|
|
"""
|
|
|
|
Initializes a new TLObject, given its properties.
|
|
|
|
Usually, this will be called from `from_tl` instead
|
|
|
|
:param fullname: The fullname of the TL object (namespace.name)
|
|
|
|
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?
|
|
|
|
"""
|
|
|
|
# The name can or not have a namespace
|
|
|
|
if '.' in fullname:
|
|
|
|
self.namespace = fullname.split('.')[0]
|
|
|
|
self.name = fullname.split('.')[1]
|
|
|
|
else:
|
|
|
|
self.namespace = None
|
|
|
|
self.name = fullname
|
|
|
|
|
|
|
|
# The ID should be an hexadecimal string
|
2016-11-30 00:29:42 +03:00
|
|
|
self.id = int(object_id, base=16)
|
2016-08-26 20:12:20 +03:00
|
|
|
self.args = args
|
|
|
|
self.result = result
|
|
|
|
self.is_function = is_function
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_tl(tl, is_function):
|
|
|
|
"""Returns a TL object from the given TL scheme line"""
|
|
|
|
|
|
|
|
# Regex to match the whole line
|
|
|
|
match = re.match(r'''
|
|
|
|
^ # We want to match from the beginning to the end
|
|
|
|
([\w.]+) # The .tl object can contain alpha_name or namespace.alpha_name
|
|
|
|
\# # After the name, comes the ID of the object
|
|
|
|
([0-9a-f]+) # The constructor ID is in hexadecimal form
|
|
|
|
|
|
|
|
(?:\s # After that, we want to match its arguments (name:type)
|
2017-06-10 14:39:37 +03:00
|
|
|
{? # For handling the start of the '{X:Type}' case
|
2016-08-26 20:12:20 +03:00
|
|
|
\w+ # The argument name will always be an alpha-only name
|
|
|
|
: # Then comes the separator between name:type
|
|
|
|
[\w\d<>#.?!]+ # The type is slightly more complex, since it's alphanumeric and it can
|
|
|
|
# also have Vector<type>, flags:# and flags.0?default, plus :!X as type
|
2017-06-10 14:39:37 +03:00
|
|
|
}? # For handling the end of the '{X:Type}' case
|
2016-08-26 20:12:20 +03:00
|
|
|
)* # Match 0 or more arguments
|
|
|
|
\s # Leave a space between the arguments and the equal
|
|
|
|
=
|
|
|
|
\s # Leave another space between the equal and the result
|
|
|
|
([\w\d<>#.?]+) # The result can again be as complex as any argument type
|
|
|
|
;$ # Finally, the line should always end with ;
|
|
|
|
''', tl, re.IGNORECASE | re.VERBOSE)
|
|
|
|
|
|
|
|
# Sub-regex to match the arguments (sadly, it cannot be embedded in the first regex)
|
|
|
|
args_match = re.findall(r'''
|
2017-05-21 14:59:16 +03:00
|
|
|
({)? # We may or may not capture the opening brace
|
2016-08-26 20:12:20 +03:00
|
|
|
(\w+) # First we capture any alpha name with length 1 or more
|
|
|
|
: # Which is separated from its type by a colon
|
|
|
|
([\w\d<>#.?!]+) # The type is slightly more complex, since it's alphanumeric and it can
|
|
|
|
# also have Vector<type>, flags:# and flags.0?default, plus :!X as type
|
2017-05-21 14:59:16 +03:00
|
|
|
(})? # We may or not capture the closing brace
|
2016-08-26 20:12:20 +03:00
|
|
|
''', tl, re.IGNORECASE | re.VERBOSE)
|
|
|
|
|
|
|
|
# Retrieve the matched arguments
|
2016-11-30 00:29:42 +03:00
|
|
|
args = [TLArg(name, arg_type, brace != '')
|
|
|
|
for brace, name, arg_type, _ in args_match]
|
2016-08-26 20:12:20 +03:00
|
|
|
|
|
|
|
# And initialize the TLObject
|
2016-11-30 00:29:42 +03:00
|
|
|
return TLObject(
|
|
|
|
fullname=match.group(1),
|
|
|
|
object_id=match.group(2),
|
|
|
|
args=args,
|
|
|
|
result=match.group(3),
|
|
|
|
is_function=is_function)
|
2016-08-26 20:12:20 +03:00
|
|
|
|
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)
|
|
|
|
|
2016-11-23 23:03:58 +03:00
|
|
|
def is_core_type(self):
|
|
|
|
"""Determines whether the TLObject is a "core type"
|
|
|
|
(and thus should be embedded in the generated code) or not"""
|
|
|
|
return self.id in TLObject.CORE_TYPES
|
|
|
|
|
2016-09-05 20:12:14 +03:00
|
|
|
def __repr__(self):
|
2016-11-30 00:29:42 +03:00
|
|
|
fullname = ('{}.{}'.format(self.namespace, self.name)
|
|
|
|
if self.namespace is not None else self.name)
|
2016-08-26 20:12:20 +03:00
|
|
|
|
2016-11-30 00:29:42 +03:00
|
|
|
hex_id = hex(self.id)[2:].rjust(8,
|
|
|
|
'0') # Skip 0x and add 0's for padding
|
2016-08-26 20:12:20 +03:00
|
|
|
|
2016-11-30 00:29:42 +03:00
|
|
|
return '{}#{} {} = {}'.format(
|
|
|
|
fullname, hex_id, ' '.join([str(arg) for arg in self.args]),
|
|
|
|
self.result)
|
2016-08-26 20:12:20 +03:00
|
|
|
|
2016-09-05 20:12:14 +03:00
|
|
|
def __str__(self):
|
2016-11-30 00:29:42 +03:00
|
|
|
fullname = ('{}.{}'.format(self.namespace, self.name)
|
|
|
|
if self.namespace is not None else self.name)
|
2016-09-05 20:12:14 +03:00
|
|
|
|
|
|
|
# Some arguments are not valid for being represented, such as the flag indicator or generic definition
|
|
|
|
# (these have no explicit values until used)
|
|
|
|
valid_args = [arg for arg in self.args
|
2016-09-08 17:55:46 +03:00
|
|
|
if not arg.flag_indicator and not arg.generic_definition]
|
2016-09-05 20:12:14 +03:00
|
|
|
|
2016-09-06 19:54:49 +03:00
|
|
|
args = ', '.join(['{}={{}}'.format(arg.name) for arg in valid_args])
|
2016-09-07 12:36:34 +03:00
|
|
|
|
|
|
|
# Since Python's default representation for lists is using repr(), we need to str() manually on every item
|
2016-11-30 00:29:42 +03:00
|
|
|
args_format = ', '.join(
|
|
|
|
['str(self.{})'.format(arg.name) if not arg.is_vector else
|
|
|
|
'None if not self.{0} else [str(_) for _ in self.{0}]'.format(
|
|
|
|
arg.name) for arg in valid_args])
|
2016-09-05 20:12:14 +03:00
|
|
|
|
|
|
|
return ("'({} (ID: {}) = ({}))'.format({})"
|
|
|
|
.format(fullname, hex(self.id), args, args_format))
|
|
|
|
|
|
|
|
|
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})
|
|
|
|
"""
|
2016-08-27 22:49:38 +03:00
|
|
|
if name == 'self': # This very only name is restricted
|
|
|
|
self.name = 'is_self'
|
|
|
|
else:
|
|
|
|
self.name = 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
|
|
|
|
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('!')
|
|
|
|
self.type = arg_type.lstrip(
|
|
|
|
'!') # Strip the exclamation mark always to have only the name
|
2016-08-26 20:12:20 +03:00
|
|
|
|
|
|
|
# The type may be a flag (flags.IDX?REAL_TYPE)
|
2017-06-10 14:39:37 +03:00
|
|
|
# Note that 'flags' is NOT the flags name; this is determined by a previous argument
|
|
|
|
# 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>
|
|
|
|
vector_match = re.match(r'vector<(\w+)>', self.type, re.IGNORECASE)
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
def __str__(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
|
|
|
|
|
|
|
if self.generic_definition:
|
2016-08-27 12:59:23 +03:00
|
|
|
return '{{{}:{}}}'.format(self.name, real_type)
|
2016-08-26 20:12:20 +03:00
|
|
|
else:
|
2016-08-27 12:59:23 +03:00
|
|
|
return '{}:{}'.format(self.name, real_type)
|