Fix parsing for constructors and not objects (#348)

This commit is contained in:
Viktor Oreshkin 2017-10-16 21:15:22 +03:00 committed by Lonami
parent 1b71c6fbf1
commit ee01724cdb
2 changed files with 22 additions and 4 deletions

View File

@ -98,12 +98,17 @@ class TLObject:
def class_name(self):
"""Gets the class name following the Python style guidelines"""
return self.class_name_for(self.name, self.is_function)
@staticmethod
def class_name_for(typename, is_function=False):
"""Gets the class name following the Python style guidelines"""
# Courtesy of http://stackoverflow.com/a/31531797/4759433
result = re.sub(r'_([a-z])', lambda m: m.group(1).upper(), self.name)
result = re.sub(r'_([a-z])', lambda m: m.group(1).upper(),
typename)
result = result[:1].upper() + result[1:].replace('_', '')
# If it's a function, let it end with "Request" to identify them
if self.is_function:
if is_function:
result += 'Request'
return result
@ -192,6 +197,7 @@ class TLArg:
# Default values
self.is_vector = False
self.is_flag = False
self.skip_constructor_id = False
self.flag_index = -1
# Special case: some types can be inferred, which makes it
@ -234,6 +240,11 @@ class TLArg:
# Update the type to match the one inside the vector
self.type = vector_match.group(1)
# 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
# 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

View File

@ -5,7 +5,7 @@ import struct
from zlib import crc32
from collections import defaultdict
from .parser import SourceBuilder, TLParser
from .parser import SourceBuilder, TLParser, TLObject
AUTO_GEN_NOTICE = \
'"""File generated by TLObjects\' generator. All changes will be ERASED"""'
@ -129,6 +129,9 @@ class TLGenerator:
builder.writeln(
'from {}.tl.tlobject import TLObject'.format('.' * depth)
)
builder.writeln(
'from {}.tl import types'.format('.' * depth)
)
# Add the relative imports to the namespaces,
# unless we already are in a namespace.
@ -638,7 +641,11 @@ class TLGenerator:
else:
# Else it may be a custom type
if not arg.skip_constructor_id:
builder.writeln('{} = reader.tgread_object()'.format(name))
else:
builder.writeln('{} = types.{}.from_reader(reader)'.format(
name, TLObject.class_name_for(arg.type)))
# End vector and flag blocks if required (if we opened them before)
if arg.is_vector: