Add primitive types

This commit is contained in:
Dan 2017-12-05 12:29:15 +01:00
parent 5332a5f36d
commit c9f90faf3d
9 changed files with 319 additions and 0 deletions

17
pyrogram/api/__init__.py Normal file
View 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/>.

View 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/>.

View 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

View 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()

View 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)
)

View 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)

View 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

View 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())

View 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
]
)