diff --git a/telethon/crypto/auth_key.py b/telethon/crypto/auth_key.py index 17a7f8ca..7dce3fa3 100644 --- a/telethon/crypto/auth_key.py +++ b/telethon/crypto/auth_key.py @@ -1,7 +1,7 @@ import struct from hashlib import sha1 -from .. import helpers as utils +from ..helpers import calc_msg_key from ..extensions import BinaryReader @@ -20,4 +20,4 @@ class AuthKey: """ new_nonce = new_nonce.to_bytes(32, 'little', signed=True) data = new_nonce + struct.pack(' 10 * 1024 * 1024 part_count = (file_size + part_size - 1) // part_size - file_id = utils.generate_random_long() + file_id = generate_random_long() hash_md5 = md5() stream = open(file, 'rb') if isinstance(file, str) else BytesIO(file) @@ -689,7 +690,7 @@ class TelegramBareClient: if isinstance(file, str): # Ensure that we'll be able to download the media - utils.ensure_parent_dir_exists(file) + ensure_parent_dir_exists(file) f = open(file, 'wb') else: f = file diff --git a/telethon_generator/tl_generator.py b/telethon_generator/tl_generator.py index 8fc6bb2d..4838087c 100644 --- a/telethon_generator/tl_generator.py +++ b/telethon_generator/tl_generator.py @@ -6,6 +6,7 @@ from zlib import crc32 from collections import defaultdict from .parser import SourceBuilder, TLParser, TLObject + AUTO_GEN_NOTICE = \ '"""File generated by TLObjects\' generator. All changes will be ERASED"""' @@ -117,8 +118,18 @@ class TLGenerator: def _write_init_py(out_dir, depth, namespace_tlobjects, type_constructors): # namespace_tlobjects: {'namespace', [TLObject]} os.makedirs(out_dir, exist_ok=True) + + # Generate __init__.py with relative imports to the namespaces + with open(os.path.join(out_dir, '__init__.py'), 'w', encoding='utf-8') as f, \ + SourceBuilder(f) as builder: + builder.writeln(AUTO_GEN_NOTICE) + builder.writeln('from . import {}'.format(', '.join( + x for x in namespace_tlobjects.keys() if x + ))) + builder.writeln('from .base import *') + for ns, tlobjects in namespace_tlobjects.items(): - file = os.path.join(out_dir, ns + '.py' if ns else '__init__.py') + file = os.path.join(out_dir, ns + '.py' if ns else 'base.py') with open(file, 'w', encoding='utf-8') as f, \ SourceBuilder(f) as builder: builder.writeln(AUTO_GEN_NOTICE) @@ -129,16 +140,12 @@ 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. - if not ns: - builder.writeln('from . import {}'.format(', '.join( - x for x in namespace_tlobjects.keys() if x - ))) + # No need to import base in base.py + if ns: + builder.writeln( + 'from . import base' + ) # Import 'get_input_*' utils # TODO Support them on types too @@ -343,7 +350,7 @@ class TLGenerator: builder.writeln('def from_reader(reader):') for arg in tlobject.args: TLGenerator.write_read_code( - builder, arg, tlobject.args, name='_' + arg.name + builder, arg, tlobject, name='_' + arg.name ) builder.writeln('return {}({})'.format( @@ -550,15 +557,14 @@ class TLGenerator: return True # Something was written @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 arg.name variable to its read value. :param builder: The source code builder :param arg: The argument to write - :param args: All the other arguments in TLObject same on_send. - This is required to determine the flags value + :param tlobject: The TLObject for which from_reader() method is written :param name: The name of the argument. Defaults to "self.argname" This argument is an option because it's required when writing Vectors<> @@ -595,7 +601,7 @@ class TLGenerator: builder.writeln('for _ in range(reader.read_int()):') # Temporary disable .is_vector, not to enter this if again 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)) arg.is_vector = True @@ -644,8 +650,8 @@ class TLGenerator: 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))) + builder.writeln('{} = {}{}.from_reader(reader)'.format( + name, 'base.' if tlobject.namespace else '', TLObject.class_name_for(arg.type))) # End vector and flag blocks if required (if we opened them before) if arg.is_vector: