From 15fd5390ae50c035cac43e023d264627f74e1478 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 4 Sep 2017 17:18:33 +0200 Subject: [PATCH] Optimize imports --- telethon/crypto/rsa.py | 1 + telethon/extensions/tcp_client.py | 10 ++--- telethon/network/connection.py | 4 -- telethon/network/mtproto_sender.py | 8 ++-- telethon/telegram_bare_client.py | 29 +++++---------- telethon/telegram_client.py | 59 ++++++++++++------------------ telethon/tl/tlobject.py | 1 - 7 files changed, 41 insertions(+), 71 deletions(-) diff --git a/telethon/crypto/rsa.py b/telethon/crypto/rsa.py index d8b95855..4e8d3384 100644 --- a/telethon/crypto/rsa.py +++ b/telethon/crypto/rsa.py @@ -4,6 +4,7 @@ try: import rsa import rsa.core except ImportError: + rsa = None raise ImportError('Missing module "rsa", please install via pip.') from ..extensions import BinaryWriter diff --git a/telethon/extensions/tcp_client.py b/telethon/extensions/tcp_client.py index bb6dbe81..9a37c685 100644 --- a/telethon/extensions/tcp_client.py +++ b/telethon/extensions/tcp_client.py @@ -1,12 +1,8 @@ # Python rough implementation of a C# TCP client -import socket -import time -from datetime import datetime, timedelta -from io import BytesIO, BufferedWriter -from threading import Event, Lock import errno - -from ..errors import ReadCancelledError +import socket +from datetime import timedelta +from io import BytesIO, BufferedWriter class TcpClient: diff --git a/telethon/network/connection.py b/telethon/network/connection.py index c3cb5a9f..1426ce78 100644 --- a/telethon/network/connection.py +++ b/telethon/network/connection.py @@ -118,10 +118,6 @@ class Connection: def close(self): self.conn.close() - def get_client_delay(self): - """Gets the client read delay""" - return self.conn.delay - # region Receive message implementations def recv(self): diff --git a/telethon/network/mtproto_sender.py b/telethon/network/mtproto_sender.py index 8154d4f5..a8817a06 100644 --- a/telethon/network/mtproto_sender.py +++ b/telethon/network/mtproto_sender.py @@ -1,14 +1,14 @@ import gzip -from threading import RLock, Thread +import logging +from threading import RLock from .. import helpers as utils from ..crypto import AES -from ..errors import BadMessageError, InvalidDCError, rpc_message_to_error +from ..errors import BadMessageError, rpc_message_to_error +from ..extensions import BinaryReader, BinaryWriter from ..tl.all_tlobjects import tlobjects from ..tl.types import MsgsAck -from ..extensions import BinaryReader, BinaryWriter -import logging logging.getLogger(__name__).addHandler(logging.NullHandler()) diff --git a/telethon/telegram_bare_client.py b/telethon/telegram_bare_client.py index 647e6c22..bf6a0fc8 100644 --- a/telethon/telegram_bare_client.py +++ b/telethon/telegram_bare_client.py @@ -1,43 +1,32 @@ import logging -from time import sleep from datetime import timedelta from hashlib import md5 -from os import path from io import BytesIO +from os import path -# Import some externalized utilities to work with the Telegram types and more from . import helpers as utils +from .crypto import rsa, CdnDecrypter from .errors import ( RPCError, FloodWaitError, FileMigrateError, TypeNotFoundError ) from .network import authenticator, MtProtoSender, Connection, ConnectionMode -from .utils import get_appropriated_part_size -from .crypto import rsa, CdnDecrypter - -# For sending and receiving requests from .tl import TLObject, Session from .tl.all_tlobjects import layer from .tl.functions import ( InitConnectionRequest, InvokeWithLayerRequest, PingRequest ) - -# Initial request -from .tl.functions.help import GetConfigRequest from .tl.functions.auth import ( ImportAuthorizationRequest, ExportAuthorizationRequest ) - -# Easier access for working with media -from .tl.functions.upload import ( - GetFileRequest, GetCdnFileRequest, ReuploadCdnFileRequest, - SaveBigFilePartRequest, SaveFilePartRequest +from .tl.functions.help import ( + GetCdnConfigRequest, GetConfigRequest +) +from .tl.functions.upload import ( + GetFileRequest, SaveBigFilePartRequest, SaveFilePartRequest ) - -from .tl.functions.help import GetCdnConfigRequest - -# All the types we need to work with from .tl.types import InputFile, InputFileBig -from .tl.types.upload import FileCdnRedirect, CdnFileReuploadNeeded +from .tl.types.upload import FileCdnRedirect +from .utils import get_appropriated_part_size class TelegramBareClient: diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index fc0999f1..2f934af8 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -1,52 +1,41 @@ import os from datetime import datetime, timedelta from mimetypes import guess_type -from threading import Event, RLock, Thread -from time import sleep, time +from threading import RLock, Thread from . import TelegramBareClient -from .network import ConnectionMode - -# Import some externalized utilities to work with the Telegram types and more from . import helpers as utils -from .errors import (RPCError, UnauthorizedError, InvalidParameterError, - ReadCancelledError, PhoneCodeEmptyError, - PhoneMigrateError, NetworkMigrateError, UserMigrateError, - PhoneCodeExpiredError, PhoneCodeHashEmptyError, - PhoneCodeInvalidError, InvalidChecksumError) - -# For sending and receiving requests +from .errors import ( + RPCError, UnauthorizedError, InvalidParameterError, PhoneCodeEmptyError, + PhoneMigrateError, NetworkMigrateError, UserMigrateError, + PhoneCodeExpiredError, PhoneCodeHashEmptyError, PhoneCodeInvalidError +) +from .network import ConnectionMode from .tl import Session, TLObject - -# Required to get the password salt -from .tl.functions.account import GetPasswordRequest - -# Logging in and out -from .tl.functions.auth import (CheckPasswordRequest, LogOutRequest, - SendCodeRequest, SignInRequest, - SignUpRequest, ImportBotAuthorizationRequest) - -# Easier access to common methods +from .tl.functions.account import ( + GetPasswordRequest +) +from .tl.functions.auth import ( + CheckPasswordRequest, LogOutRequest, SendCodeRequest, SignInRequest, + SignUpRequest, ImportBotAuthorizationRequest +) +from .tl.functions.contacts import ( + GetContactsRequest, ResolveUsernameRequest +) from .tl.functions.messages import ( GetDialogsRequest, GetHistoryRequest, ReadHistoryRequest, SendMediaRequest, - SendMessageRequest) - -from .tl.functions.contacts import GetContactsRequest, ResolveUsernameRequest - -# For .get_me() and ensuring we're authorized -from .tl.functions.users import GetUsersRequest - -# So the server doesn't stop sending updates to us -from .tl.functions import PingRequest - -# All the types we need to work with + SendMessageRequest +) +from .tl.functions.users import ( + GetUsersRequest +) from .tl.types import ( DocumentAttributeAudio, DocumentAttributeFilename, InputDocumentFileLocation, InputFileLocation, InputMediaUploadedDocument, InputMediaUploadedPhoto, InputPeerEmpty, Message, MessageMediaContact, MessageMediaDocument, MessageMediaPhoto, - InputUserSelf, UserProfilePhoto, ChatPhoto) - + InputUserSelf, UserProfilePhoto, ChatPhoto +) from .utils import find_user_or_chat, get_extension diff --git a/telethon/tl/tlobject.py b/telethon/tl/tlobject.py index 5e18c57d..db18b9d3 100644 --- a/telethon/tl/tlobject.py +++ b/telethon/tl/tlobject.py @@ -1,4 +1,3 @@ -from datetime import datetime, timedelta from threading import Event