From 7adb4f09d6c3a35b0c187213f173f78bfc697f32 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 9 Jun 2017 16:13:39 +0200 Subject: [PATCH] Slightly reorganise the project structure --- telethon/crypto/auth_key.py | 2 +- telethon/crypto/rsa.py | 2 +- telethon/extensions/__init__.py | 9 +++++++++ telethon/{utils => extensions}/binary_reader.py | 0 telethon/{utils => extensions}/binary_writer.py | 0 telethon/{network => extensions}/tcp_client.py | 0 telethon/helpers.py | 1 + telethon/network/__init__.py | 1 - telethon/network/authenticator.py | 2 +- telethon/network/mtproto_plain_sender.py | 2 +- telethon/network/mtproto_sender.py | 2 +- telethon/network/tcp_transport.py | 4 ++-- telethon/{utils/tl_utils.py => utils.py} | 10 +++++----- telethon/utils/__init__.py | 3 --- telethon_tests/network_test.py | 3 ++- telethon_tests/utils_test.py | 2 +- 16 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 telethon/extensions/__init__.py rename telethon/{utils => extensions}/binary_reader.py (100%) rename telethon/{utils => extensions}/binary_writer.py (100%) rename telethon/{network => extensions}/tcp_client.py (100%) rename telethon/{utils/tl_utils.py => utils.py} (92%) delete mode 100644 telethon/utils/__init__.py diff --git a/telethon/crypto/auth_key.py b/telethon/crypto/auth_key.py index 1a4f4c71..0ce021a4 100644 --- a/telethon/crypto/auth_key.py +++ b/telethon/crypto/auth_key.py @@ -1,7 +1,7 @@ from hashlib import sha1 from .. import helpers as utils -from ..utils import BinaryReader, BinaryWriter +from ..extensions import BinaryReader, BinaryWriter class AuthKey: diff --git a/telethon/crypto/rsa.py b/telethon/crypto/rsa.py index 258d350f..59b75c84 100644 --- a/telethon/crypto/rsa.py +++ b/telethon/crypto/rsa.py @@ -1,7 +1,7 @@ import os from hashlib import sha1 -from ..utils import BinaryWriter +from ..extensions import BinaryWriter class RSAServerKey: diff --git a/telethon/extensions/__init__.py b/telethon/extensions/__init__.py new file mode 100644 index 00000000..420e4bda --- /dev/null +++ b/telethon/extensions/__init__.py @@ -0,0 +1,9 @@ +""" +Several extensions Python is missing, such as a proper class to handle a TCP +communication with support for cancelling the operation, and an utility class +to work with arbitrary binary data in a more comfortable way (writing ints, +strings, bytes, etc.) +""" +from .binary_writer import BinaryWriter +from .binary_reader import BinaryReader +from .tcp_client import TcpClient \ No newline at end of file diff --git a/telethon/utils/binary_reader.py b/telethon/extensions/binary_reader.py similarity index 100% rename from telethon/utils/binary_reader.py rename to telethon/extensions/binary_reader.py diff --git a/telethon/utils/binary_writer.py b/telethon/extensions/binary_writer.py similarity index 100% rename from telethon/utils/binary_writer.py rename to telethon/extensions/binary_writer.py diff --git a/telethon/network/tcp_client.py b/telethon/extensions/tcp_client.py similarity index 100% rename from telethon/network/tcp_client.py rename to telethon/extensions/tcp_client.py diff --git a/telethon/helpers.py b/telethon/helpers.py index 4c5b74fa..4fe81754 100644 --- a/telethon/helpers.py +++ b/telethon/helpers.py @@ -1,3 +1,4 @@ +"""Various helpers not related to the Telegram API itself""" from hashlib import sha1, sha256 import os diff --git a/telethon/network/__init__.py b/telethon/network/__init__.py index c9c23c26..d08ab0fc 100644 --- a/telethon/network/__init__.py +++ b/telethon/network/__init__.py @@ -1,5 +1,4 @@ from .mtproto_plain_sender import MtProtoPlainSender -from .tcp_client import TcpClient from .authenticator import do_authentication from .mtproto_sender import MtProtoSender from .tcp_transport import TcpTransport diff --git a/telethon/network/authenticator.py b/telethon/network/authenticator.py index 4285ee7f..d13dfc9a 100644 --- a/telethon/network/authenticator.py +++ b/telethon/network/authenticator.py @@ -5,7 +5,7 @@ from hashlib import sha1 from .. import helpers as utils from ..crypto import AES, RSA, AuthKey, Factorization from ..network import MtProtoPlainSender -from ..utils import BinaryReader, BinaryWriter +from ..extensions import BinaryReader, BinaryWriter def do_authentication(transport): diff --git a/telethon/network/mtproto_plain_sender.py b/telethon/network/mtproto_plain_sender.py index fc341772..7fae3c20 100644 --- a/telethon/network/mtproto_plain_sender.py +++ b/telethon/network/mtproto_plain_sender.py @@ -1,7 +1,7 @@ import random import time -from ..utils import BinaryReader, BinaryWriter +from ..extensions import BinaryReader, BinaryWriter class MtProtoPlainSender: diff --git a/telethon/network/mtproto_sender.py b/telethon/network/mtproto_sender.py index 4ec228dd..f3a7ed01 100644 --- a/telethon/network/mtproto_sender.py +++ b/telethon/network/mtproto_sender.py @@ -8,7 +8,7 @@ from ..errors import (BadMessageError, FloodWaitError, RPCError, InvalidDCError) from ..tl.all_tlobjects import tlobjects from ..tl.types import MsgsAck -from ..utils import BinaryReader, BinaryWriter +from ..extensions import BinaryReader, BinaryWriter import logging logging.getLogger(__name__).addHandler(logging.NullHandler()) diff --git a/telethon/network/tcp_transport.py b/telethon/network/tcp_transport.py index 512ac1d5..9b35455b 100644 --- a/telethon/network/tcp_transport.py +++ b/telethon/network/tcp_transport.py @@ -2,8 +2,8 @@ from binascii import crc32 from datetime import timedelta from ..errors import InvalidChecksumError -from ..network import TcpClient -from ..utils import BinaryWriter +from ..extensions import TcpClient +from ..extensions import BinaryWriter class TcpTransport: diff --git a/telethon/utils/tl_utils.py b/telethon/utils.py similarity index 92% rename from telethon/utils/tl_utils.py rename to telethon/utils.py index 6bb8e23e..cacc60fd 100644 --- a/telethon/utils/tl_utils.py +++ b/telethon/utils.py @@ -1,10 +1,10 @@ -"""Utilities for working with TLObjects. - We have these because some TLObjects differ in very little things, - for example, some may have an `user_id` attribute and other a `chat_id` but, - after all, both are the same attribute, IDs.""" +""" +Utilities for working with the Telegram API itself (such as handy methods +to convert between an entity like an User, Chat, etc. into its Input version) +""" from mimetypes import add_type, guess_extension -from ..tl.types import ( +from .tl.types import ( Channel, Chat, ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, MessageMediaDocument, MessageMediaPhoto, PeerChannel, PeerChat, PeerUser, User, UserProfilePhoto) diff --git a/telethon/utils/__init__.py b/telethon/utils/__init__.py deleted file mode 100644 index c8f99ae5..00000000 --- a/telethon/utils/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from .binary_writer import BinaryWriter -from .binary_reader import BinaryReader -from .tl_utils import * diff --git a/telethon_tests/network_test.py b/telethon_tests/network_test.py index 90ef410a..ab364a1f 100644 --- a/telethon_tests/network_test.py +++ b/telethon_tests/network_test.py @@ -4,7 +4,8 @@ import threading import unittest import telethon.network.authenticator as authenticator -from telethon.network import TcpClient, TcpTransport +from telethon.extensions import TcpClient +from telethon.network import TcpTransport def run_server_echo_thread(port): diff --git a/telethon_tests/utils_test.py b/telethon_tests/utils_test.py index 3721fe56..5c53740c 100644 --- a/telethon_tests/utils_test.py +++ b/telethon_tests/utils_test.py @@ -1,6 +1,6 @@ import os import unittest -from telethon.utils import BinaryReader, BinaryWriter +from telethon.extensions import BinaryReader, BinaryWriter class UtilsTests(unittest.TestCase):