Skip to content

Commit

Permalink
Fix parsing for constructors and not objects (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
stek29 authored and Lonami committed Oct 16, 2017
1 parent 1b71c6f commit ee01724
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
15 changes: 13 additions & 2 deletions telethon_generator/parser/tl_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,17 @@ def from_tl(tl, is_function):

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

Expand Down Expand Up @@ -192,6 +197,7 @@ def __init__(self, name, arg_type, generic_definition):
# 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
Expand Down Expand Up @@ -234,6 +240,11 @@ def __init__(self, name, arg_type, generic_definition):
# 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
Expand Down
11 changes: 9 additions & 2 deletions telethon_generator/tl_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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"""'

Expand Down Expand Up @@ -129,6 +129,9 @@ def _write_init_py(out_dir, depth, namespace_tlobjects, type_constructors):
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.
Expand Down Expand Up @@ -638,7 +641,11 @@ def write_read_code(builder, arg, args, name):

else:
# Else it may be a custom type
builder.writeln('{} = reader.tgread_object()'.format(name))
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:
Expand Down

0 comments on commit ee01724

Please sign in to comment.