diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index ee637268..00c1c70a 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -31,7 +31,7 @@ __copyright__ = "Copyright (C) 2017-2018 Dan Tès . +import logging from collections import OrderedDict from datetime import datetime from io import BytesIO @@ -23,13 +24,23 @@ from json import JSONEncoder, dumps from ..all import objects +log = logging.getLogger(__name__) + class Object: all = {} @staticmethod def read(b: BytesIO, *args): - return Object.all[int.from_bytes(b.read(4), "little")].read(b, *args) + constructor_id = int.from_bytes(b.read(4), "little") + + try: + return Object.all[constructor_id].read(b, *args) + except KeyError: + log.error("Unknown constructor found: {}. Full data: {}".format( + hex(constructor_id), + b.getvalue().hex()) + ) def write(self, *args) -> bytes: pass diff --git a/pyrogram/client/types/messages_and_media/message_entity.py b/pyrogram/client/types/messages_and_media/message_entity.py index db2eee3e..f8f41734 100644 --- a/pyrogram/client/types/messages_and_media/message_entity.py +++ b/pyrogram/client/types/messages_and_media/message_entity.py @@ -26,9 +26,9 @@ class MessageEntity(Object): Args: type (``str``): Type of the entity. - Can be "mention" (@username), "hashtag", "cashtag", "bot_command", "url", "email", "bold" (bold text), - italic (italic text), "code" (monowidth string), "pre" (monowidth block), "text_link" (for clickable text - URLs), "text_mention" (for users without usernames). + Can be "mention" (@username), "hashtag", "cashtag", "bot_command", "url", "email", "phone_number", "bold" + (bold text), italic (italic text), "code" (monowidth string), "pre" (monowidth block), "text_link" + (for clickable text URLs), "text_mention" (for users without usernames). offset (``int``): Offset in UTF-16 code units to the start of the entity. diff --git a/setup.py b/setup.py index 00f9be63..5df52747 100644 --- a/setup.py +++ b/setup.py @@ -16,10 +16,12 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import os import re +import shutil from sys import argv -from setuptools import setup, find_packages +from setuptools import setup, find_packages, Command from compiler.api import compiler as api_compiler from compiler.docs import compiler as docs_compiler @@ -31,24 +33,125 @@ def read(file: str) -> list: return [i.strip() for i in r] -if len(argv) > 1 and argv[1] != "sdist": - api_compiler.start() - docs_compiler.start() +def get_version(): + with open("pyrogram/__init__.py", encoding="utf-8") as f: + return re.findall(r"__version__ = \"(.+)\"", f.read())[0] + + +def get_readme(): + # PyPI doesn"t like raw html + with open("README.rst", encoding="utf-8") as f: + readme = re.sub(r"\.\. \|.+\| raw:: html(?:\s{4}.+)+\n\n", "", f.read()) + return re.sub(r"\|header\|", "|logo|\n\n|description|\n\n|scheme| |tgcrypto|", readme) + + +class Clean(Command): + DIST = [ + "./build", + "./dist", + "./Pyrogram.egg-info" + ] + + API = [ + "pyrogram/api/errors/exceptions", + "pyrogram/api/functions", + "pyrogram/api/types", + "pyrogram/api/all.py", + ] + + DOCS = [ + "docs/source/functions", + "docs/source/types", + "docs/build" + ] + + ALL = DIST + API + DOCS + + description = "Clean generated files" + + user_options = [ + ("dist", None, "Clean distribution files"), + ("api", None, "Clean generated API files"), + ("docs", None, "Clean generated docs files"), + ("all", None, "Clean all generated files"), + ] + + def __init__(self, dist, **kw): + super().__init__(dist, **kw) + + self.dist = None + self.api = None + self.docs = None + self.all = None + + def initialize_options(self): + pass + + def finalize_options(self): + pass + + def run(self): + paths = set() + + if self.dist: + paths.update(Clean.DIST) + + if self.api: + paths.update(Clean.API) + + if self.docs: + paths.update(Clean.DOCS) + + if self.all: + paths.update(Clean.ALL) + + for path in sorted(list(paths)): + try: + shutil.rmtree(path) if os.path.isdir(path) else os.remove(path) + except OSError: + print("skipping {}".format(path)) + else: + print("removing {}".format(path)) + + +class Build(Command): + description = "Build Pyrogram files" + + user_options = [ + ("api", None, "Build API files"), + ("docs", None, "Build docs files"), + ] + + def __init__(self, dist, **kw): + super().__init__(dist, **kw) + + self.api = None + self.docs = None + + def initialize_options(self): + pass + + def finalize_options(self): + pass + + def run(self): + if self.api: + error_compiler.start() + api_compiler.start() + + if self.docs: + docs_compiler.start() + + +if len(argv) > 1 and argv[1] in ["bdist_wheel", "install"]: error_compiler.start() - -with open("pyrogram/__init__.py", encoding="utf-8") as f: - version = re.findall(r"__version__ = \"(.+)\"", f.read())[0] - -# PyPI doesn't like raw html -with open("README.rst", encoding="utf-8") as f: - readme = re.sub(r"\.\. \|.+\| raw:: html(?:\s{4}.+)+\n\n", "", f.read()) - readme = re.sub(r"\|header\|", "|logo|\n\n|description|\n\n|scheme| |tgcrypto|", readme) + api_compiler.start() setup( name="Pyrogram", - version=version, + version=get_version(), description="Telegram MTProto API Client Library for Python", - long_description=readme, + long_description=get_readme(), url="https://github.com/pyrogram", download_url="https://github.com/pyrogram/pyrogram/releases/latest", author="Dan Tès", @@ -85,5 +188,9 @@ setup( packages=find_packages(exclude=["compiler*"]), zip_safe=False, install_requires=read("requirements.txt"), - extras_require={"tgcrypto": ["tgcrypto>=1.0.4"]} + extras_require={"tgcrypto": ["tgcrypto>=1.0.4"]}, + cmdclass={ + "clean": Clean, + "build": Build, + } )