Skip to content

Commit c9f90fa

Browse files
committed
Add primitive types
1 parent 5332a5f commit c9f90fa

File tree

9 files changed

+319
-0
lines changed

9 files changed

+319
-0
lines changed

pyrogram/api/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

pyrogram/api/core/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from .bool import Bool, BoolTrue, BoolFalse
20+
from .bytes import Bytes
21+
from .double import Double
22+
from .int import Int, Long, Int128, Int256
23+
from .string import String
24+
from .vector import Vector
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from io import BytesIO
20+
21+
from ..object import Object
22+
23+
24+
class BoolFalse(Object):
25+
ID = 0xbc799737
26+
value = False
27+
28+
@classmethod
29+
def read(cls, *args) -> bool:
30+
return cls.value
31+
32+
def __new__(cls) -> bytes:
33+
return int.to_bytes(cls.ID, 4, "little")
34+
35+
36+
class BoolTrue(BoolFalse):
37+
ID = 0x997275b5
38+
value = True
39+
40+
41+
class Bool(Object):
42+
@classmethod
43+
def read(cls, b: BytesIO) -> bool:
44+
value = int.from_bytes(b.read(4), "little")
45+
46+
return (
47+
True if value == BoolTrue.ID
48+
else False if value == BoolFalse.ID
49+
else None
50+
)
51+
52+
def __new__(cls, value: bool) -> BoolTrue or BoolFalse:
53+
return BoolTrue() if value else BoolFalse()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from io import BytesIO
20+
21+
from ..object import Object
22+
23+
24+
class Bytes(Object):
25+
@staticmethod
26+
def read(b: BytesIO) -> bytes:
27+
length = int.from_bytes(b.read(1), "little")
28+
29+
if length <= 253:
30+
x = b.read(length)
31+
b.read(-(length + 1) % 4)
32+
else:
33+
length = int.from_bytes(b.read(3), "little")
34+
x = b.read(length)
35+
b.read(-length % 4)
36+
37+
return x
38+
39+
def __new__(cls, value: bytes) -> bytes:
40+
length = len(value)
41+
42+
if length <= 253:
43+
return (
44+
bytes([length])
45+
+ value
46+
+ bytes(-(length + 1) % 4)
47+
)
48+
else:
49+
return (
50+
bytes([254])
51+
+ int.to_bytes(length, 3, "little")
52+
+ value
53+
+ bytes(-length % 4)
54+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from io import BytesIO
20+
from struct import unpack, pack
21+
22+
from ..object import Object
23+
24+
25+
class Double(Object):
26+
@staticmethod
27+
def read(b: BytesIO) -> float:
28+
return unpack("d", b.read(8))[0]
29+
30+
def __new__(cls, value: float) -> bytes:
31+
return pack("d", value)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from io import BytesIO
20+
21+
from ..object import Object
22+
23+
24+
class Int(Object):
25+
SIZE = 4
26+
27+
@classmethod
28+
def read(cls, b: BytesIO, signed: bool = True) -> int:
29+
return int.from_bytes(b.read(cls.SIZE), "little", signed=signed)
30+
31+
def __new__(cls, value: int, signed: bool = True) -> bytes:
32+
return int.to_bytes(value, cls.SIZE, "little", signed=signed)
33+
34+
35+
class Long(Int):
36+
SIZE = 8
37+
38+
# TODO: PyCharm can't infer types when overriding parent's __new__ and is showing unnecessary warnings.
39+
# Add this to shut warnings down
40+
def __new__(cls, *args):
41+
return super().__new__(cls, *args)
42+
43+
44+
class Int128(Int):
45+
SIZE = 16
46+
47+
48+
class Int256(Int):
49+
SIZE = 32
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from io import BytesIO
20+
21+
from . import Bytes
22+
23+
24+
class String(Bytes):
25+
@staticmethod
26+
def read(b: BytesIO) -> str:
27+
return super(String, String).read(b).decode()
28+
29+
def __new__(cls, value: str) -> bytes:
30+
return super().__new__(cls, value.encode())
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from io import BytesIO
20+
21+
from . import Int
22+
from ..object import Object
23+
24+
25+
class Vector(Object):
26+
ID = 0x1cb5c415
27+
28+
@staticmethod
29+
def read(b: BytesIO, t: Object = None) -> list:
30+
return [
31+
t.read(b) if t
32+
else Object.read(b)
33+
for _ in range(Int.read(b))
34+
]
35+
36+
def __new__(cls, value: list, t: Object = None) -> bytes:
37+
return b"".join(
38+
[Int(cls.ID), Int(len(value))]
39+
+ [
40+
t(i) if t
41+
else i.write()
42+
for i in value
43+
]
44+
)

0 commit comments

Comments
 (0)