From c9f90faf3d2015f3bf6af4aa2483b3f116505557 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 5 Dec 2017 12:29:15 +0100 Subject: [PATCH] Add primitive types --- pyrogram/api/__init__.py | 17 ++++++++ pyrogram/api/core/__init__.py | 17 ++++++++ pyrogram/api/core/primitives/__init__.py | 24 +++++++++++ pyrogram/api/core/primitives/bool.py | 53 +++++++++++++++++++++++ pyrogram/api/core/primitives/bytes.py | 54 ++++++++++++++++++++++++ pyrogram/api/core/primitives/double.py | 31 ++++++++++++++ pyrogram/api/core/primitives/int.py | 49 +++++++++++++++++++++ pyrogram/api/core/primitives/string.py | 30 +++++++++++++ pyrogram/api/core/primitives/vector.py | 44 +++++++++++++++++++ 9 files changed, 319 insertions(+) create mode 100644 pyrogram/api/__init__.py create mode 100644 pyrogram/api/core/__init__.py create mode 100644 pyrogram/api/core/primitives/__init__.py create mode 100644 pyrogram/api/core/primitives/bool.py create mode 100644 pyrogram/api/core/primitives/bytes.py create mode 100644 pyrogram/api/core/primitives/double.py create mode 100644 pyrogram/api/core/primitives/int.py create mode 100644 pyrogram/api/core/primitives/string.py create mode 100644 pyrogram/api/core/primitives/vector.py diff --git a/pyrogram/api/__init__.py b/pyrogram/api/__init__.py new file mode 100644 index 00000000..b9ee0a98 --- /dev/null +++ b/pyrogram/api/__init__.py @@ -0,0 +1,17 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . diff --git a/pyrogram/api/core/__init__.py b/pyrogram/api/core/__init__.py new file mode 100644 index 00000000..b9ee0a98 --- /dev/null +++ b/pyrogram/api/core/__init__.py @@ -0,0 +1,17 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . diff --git a/pyrogram/api/core/primitives/__init__.py b/pyrogram/api/core/primitives/__init__.py new file mode 100644 index 00000000..159d85ae --- /dev/null +++ b/pyrogram/api/core/primitives/__init__.py @@ -0,0 +1,24 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from .bool import Bool, BoolTrue, BoolFalse +from .bytes import Bytes +from .double import Double +from .int import Int, Long, Int128, Int256 +from .string import String +from .vector import Vector diff --git a/pyrogram/api/core/primitives/bool.py b/pyrogram/api/core/primitives/bool.py new file mode 100644 index 00000000..1e011543 --- /dev/null +++ b/pyrogram/api/core/primitives/bool.py @@ -0,0 +1,53 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from io import BytesIO + +from ..object import Object + + +class BoolFalse(Object): + ID = 0xbc799737 + value = False + + @classmethod + def read(cls, *args) -> bool: + return cls.value + + def __new__(cls) -> bytes: + return int.to_bytes(cls.ID, 4, "little") + + +class BoolTrue(BoolFalse): + ID = 0x997275b5 + value = True + + +class Bool(Object): + @classmethod + def read(cls, b: BytesIO) -> bool: + value = int.from_bytes(b.read(4), "little") + + return ( + True if value == BoolTrue.ID + else False if value == BoolFalse.ID + else None + ) + + def __new__(cls, value: bool) -> BoolTrue or BoolFalse: + return BoolTrue() if value else BoolFalse() diff --git a/pyrogram/api/core/primitives/bytes.py b/pyrogram/api/core/primitives/bytes.py new file mode 100644 index 00000000..43ec4234 --- /dev/null +++ b/pyrogram/api/core/primitives/bytes.py @@ -0,0 +1,54 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from io import BytesIO + +from ..object import Object + + +class Bytes(Object): + @staticmethod + def read(b: BytesIO) -> bytes: + length = int.from_bytes(b.read(1), "little") + + if length <= 253: + x = b.read(length) + b.read(-(length + 1) % 4) + else: + length = int.from_bytes(b.read(3), "little") + x = b.read(length) + b.read(-length % 4) + + return x + + def __new__(cls, value: bytes) -> bytes: + length = len(value) + + if length <= 253: + return ( + bytes([length]) + + value + + bytes(-(length + 1) % 4) + ) + else: + return ( + bytes([254]) + + int.to_bytes(length, 3, "little") + + value + + bytes(-length % 4) + ) diff --git a/pyrogram/api/core/primitives/double.py b/pyrogram/api/core/primitives/double.py new file mode 100644 index 00000000..e4a7ac48 --- /dev/null +++ b/pyrogram/api/core/primitives/double.py @@ -0,0 +1,31 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from io import BytesIO +from struct import unpack, pack + +from ..object import Object + + +class Double(Object): + @staticmethod + def read(b: BytesIO) -> float: + return unpack("d", b.read(8))[0] + + def __new__(cls, value: float) -> bytes: + return pack("d", value) diff --git a/pyrogram/api/core/primitives/int.py b/pyrogram/api/core/primitives/int.py new file mode 100644 index 00000000..cfc8f7ee --- /dev/null +++ b/pyrogram/api/core/primitives/int.py @@ -0,0 +1,49 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from io import BytesIO + +from ..object import Object + + +class Int(Object): + SIZE = 4 + + @classmethod + def read(cls, b: BytesIO, signed: bool = True) -> int: + return int.from_bytes(b.read(cls.SIZE), "little", signed=signed) + + def __new__(cls, value: int, signed: bool = True) -> bytes: + return int.to_bytes(value, cls.SIZE, "little", signed=signed) + + +class Long(Int): + SIZE = 8 + + # TODO: PyCharm can't infer types when overriding parent's __new__ and is showing unnecessary warnings. + # Add this to shut warnings down + def __new__(cls, *args): + return super().__new__(cls, *args) + + +class Int128(Int): + SIZE = 16 + + +class Int256(Int): + SIZE = 32 diff --git a/pyrogram/api/core/primitives/string.py b/pyrogram/api/core/primitives/string.py new file mode 100644 index 00000000..ed2d9270 --- /dev/null +++ b/pyrogram/api/core/primitives/string.py @@ -0,0 +1,30 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from io import BytesIO + +from . import Bytes + + +class String(Bytes): + @staticmethod + def read(b: BytesIO) -> str: + return super(String, String).read(b).decode() + + def __new__(cls, value: str) -> bytes: + return super().__new__(cls, value.encode()) diff --git a/pyrogram/api/core/primitives/vector.py b/pyrogram/api/core/primitives/vector.py new file mode 100644 index 00000000..cdb90091 --- /dev/null +++ b/pyrogram/api/core/primitives/vector.py @@ -0,0 +1,44 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from io import BytesIO + +from . import Int +from ..object import Object + + +class Vector(Object): + ID = 0x1cb5c415 + + @staticmethod + def read(b: BytesIO, t: Object = None) -> list: + return [ + t.read(b) if t + else Object.read(b) + for _ in range(Int.read(b)) + ] + + def __new__(cls, value: list, t: Object = None) -> bytes: + return b"".join( + [Int(cls.ID), Int(len(value))] + + [ + t(i) if t + else i.write() + for i in value + ] + )