Add primitive types
This commit is contained in:
parent
5332a5f36d
commit
c9f90faf3d
17
pyrogram/api/__init__.py
Normal file
17
pyrogram/api/__init__.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||||
|
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
17
pyrogram/api/core/__init__.py
Normal file
17
pyrogram/api/core/__init__.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||||
|
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
24
pyrogram/api/core/primitives/__init__.py
Normal file
24
pyrogram/api/core/primitives/__init__.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||||
|
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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
|
53
pyrogram/api/core/primitives/bool.py
Normal file
53
pyrogram/api/core/primitives/bool.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||||
|
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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()
|
54
pyrogram/api/core/primitives/bytes.py
Normal file
54
pyrogram/api/core/primitives/bytes.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||||
|
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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)
|
||||||
|
)
|
31
pyrogram/api/core/primitives/double.py
Normal file
31
pyrogram/api/core/primitives/double.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||||
|
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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)
|
49
pyrogram/api/core/primitives/int.py
Normal file
49
pyrogram/api/core/primitives/int.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||||
|
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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
|
30
pyrogram/api/core/primitives/string.py
Normal file
30
pyrogram/api/core/primitives/string.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||||
|
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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())
|
44
pyrogram/api/core/primitives/vector.py
Normal file
44
pyrogram/api/core/primitives/vector.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||||
|
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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
|
||||||
|
]
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user