Fix to avoid imports every from_reader() is called

This commit is contained in:
singer 2017-10-20 15:19:01 +03:00
parent b072caae09
commit 2833736484

View File

@ -6,6 +6,7 @@ from zlib import crc32
from collections import defaultdict from collections import defaultdict
from .parser import SourceBuilder, TLParser, TLObject from .parser import SourceBuilder, TLParser, TLObject
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"""'
@ -140,6 +141,12 @@ class TLGenerator:
'from {}.tl.tlobject import TLObject'.format('.' * depth) 'from {}.tl.tlobject import TLObject'.format('.' * depth)
) )
# No need to import base in base.py
if ns:
builder.writeln(
'from . import base'
)
# Import 'get_input_*' utils # Import 'get_input_*' utils
# TODO Support them on types too # TODO Support them on types too
if 'functions' in out_dir: if 'functions' in out_dir:
@ -343,7 +350,7 @@ class TLGenerator:
builder.writeln('def from_reader(reader):') builder.writeln('def from_reader(reader):')
for arg in tlobject.args: for arg in tlobject.args:
TLGenerator.write_read_code( TLGenerator.write_read_code(
builder, arg, tlobject.args, name='_' + arg.name builder, arg, tlobject, name='_' + arg.name
) )
builder.writeln('return {}({})'.format( builder.writeln('return {}({})'.format(
@ -550,15 +557,14 @@ class TLGenerator:
return True # Something was written return True # Something was written
@staticmethod @staticmethod
def write_read_code(builder, arg, args, name): def write_read_code(builder, arg, tlobject, name):
""" """
Writes the read code for the given argument, setting the Writes the read code for the given argument, setting the
arg.name variable to its read value. arg.name variable to its read value.
:param builder: The source code builder :param builder: The source code builder
:param arg: The argument to write :param arg: The argument to write
:param args: All the other arguments in TLObject same on_send. :param tlobject: The TLObject for which from_reader() method is written
This is required to determine the flags value
:param name: The name of the argument. Defaults to "self.argname" :param name: The name of the argument. Defaults to "self.argname"
This argument is an option because it's required when This argument is an option because it's required when
writing Vectors<> writing Vectors<>
@ -595,7 +601,7 @@ class TLGenerator:
builder.writeln('for _ in range(reader.read_int()):') builder.writeln('for _ in range(reader.read_int()):')
# Temporary disable .is_vector, not to enter this if again # Temporary disable .is_vector, not to enter this if again
arg.is_vector = False arg.is_vector = False
TLGenerator.write_read_code(builder, arg, args, name='_x') TLGenerator.write_read_code(builder, arg, tlobject, name='_x')
builder.writeln('{}.append(_x)'.format(name)) builder.writeln('{}.append(_x)'.format(name))
arg.is_vector = True arg.is_vector = True
@ -644,9 +650,8 @@ class TLGenerator:
if not arg.skip_constructor_id: if not arg.skip_constructor_id:
builder.writeln('{} = reader.tgread_object()'.format(name)) builder.writeln('{} = reader.tgread_object()'.format(name))
else: else:
builder.writeln('from . import {}'.format(TLObject.class_name_for(arg.type))) builder.writeln('{} = {}{}.from_reader(reader)'.format(
builder.writeln('{} = {}.from_reader(reader)'.format( name, 'base.' if tlobject.namespace else '', TLObject.class_name_for(arg.type)))
name, TLObject.class_name_for(arg.type)))
# End vector and flag blocks if required (if we opened them before) # End vector and flag blocks if required (if we opened them before)
if arg.is_vector: if arg.is_vector: