X Tutup
Skip to content

Commit d940afa

Browse files
committed
merge master into cr4.0
2 parents 998040d + 0c74b3c commit d940afa

File tree

11 files changed

+106
-222
lines changed

11 files changed

+106
-222
lines changed

telegram/bot.py

Lines changed: 35 additions & 116 deletions
Large diffs are not rendered by default.

telegram/inputfile.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@
2020

2121
"""This module contains a object that represents a Telegram InputFile."""
2222

23-
import mimetypes
24-
import os
25-
import sys
26-
import imghdr
27-
2823
try:
24+
# python 3
2925
from email.generator import _make_boundary as choose_boundary
30-
from urllib.request import urlopen
3126
except ImportError:
27+
# python 2
3228
from mimetools import choose_boundary
33-
from urllib2 import urlopen
29+
30+
import imghdr
31+
import mimetypes
32+
import os
33+
import sys
34+
35+
from future.moves.urllib.request import urlopen
3436

3537
from telegram import TelegramError
3638

@@ -81,6 +83,8 @@ def __init__(self,
8183
if 'filename' in data:
8284
self.filename = self.data.pop('filename')
8385
elif hasattr(self.input_file, 'name'):
86+
# on py2.7, pylint fails to understand this properly
87+
# pylint: disable=E1101
8488
self.filename = os.path.basename(self.input_file.name)
8589
elif from_url:
8690
self.filename = os.path.basename(self.input_file.url) \

telegram/utils/request.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
# pylint: disable=no-name-in-module,unused-import
32
#
43
# A library that provides a Python interface to the Telegram Bot API
54
# Copyright (C) 2015-2016
@@ -25,22 +24,9 @@
2524
import socket
2625
from ssl import SSLError
2726

28-
try:
29-
# python2
30-
from httplib import HTTPException
31-
except ImportError:
32-
# python3
33-
from http.client import HTTPException
34-
35-
try:
36-
# python3
37-
from urllib.request import urlopen, urlretrieve, Request
38-
from urllib.error import HTTPError, URLError
39-
except ImportError:
40-
# python2
41-
from urllib import urlretrieve
42-
from urllib2 import urlopen, Request, URLError
43-
from urllib2 import HTTPError
27+
from future.moves.http.client import HTTPException
28+
from future.moves.urllib.error import HTTPError, URLError
29+
from future.moves.urllib.request import urlopen, urlretrieve, Request
4430

4531
from telegram import (InputFile, TelegramError)
4632
from telegram.error import Unauthorized, NetworkError, TimedOut
@@ -130,8 +116,7 @@ def get(url):
130116
@_try_except_req
131117
def post(url,
132118
data,
133-
timeout=None,
134-
network_delay=2.):
119+
timeout=None):
135120
"""Request an URL.
136121
Args:
137122
url:
@@ -141,11 +126,6 @@ def post(url,
141126
timeout:
142127
float. If this value is specified, use it as the definitive timeout (in
143128
seconds) for urlopen() operations. [Optional]
144-
network_delay:
145-
float. If using the timeout specified in `data` (which is a timeout for
146-
the Telegram servers operation), then `network_delay` as an extra delay
147-
(in seconds) to compensate for network latency.
148-
default: 2 [Optional]
149129
150130
Notes:
151131
If neither `timeout` nor `data['timeout']` is specified. The underlying
@@ -159,8 +139,6 @@ def post(url,
159139

160140
if timeout is not None:
161141
urlopen_kwargs['timeout'] = timeout
162-
elif 'timeout' in data:
163-
urlopen_kwargs['timeout'] = data['timeout'] + network_delay
164142

165143
if InputFile.is_inputfile(data):
166144
data = InputFile(data)

tests/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
#
1717
# You should have received a copy of the GNU General Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
1920
"""This module contains a object that represents a Base class for tests"""
2021

21-
import signal
22+
import os
2223
import sys
24+
import signal
2325

24-
import os
2526
from nose.tools import make_decorator
2627

2728
sys.path.append('.')
@@ -61,6 +62,7 @@ def is_dict(dictionary):
6162

6263

6364
class TestTimedOut(AssertionError):
65+
6466
def __init__(self, time_limit, frame):
6567
super(TestTimedOut, self).__init__('time_limit={0}'.format(time_limit))
6668
self.time_limit = time_limit

tests/test_bot.py

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
#
1818
# You should have received a copy of the GNU General Public License
1919
# along with this program. If not, see [http://www.gnu.org/licenses/].
20+
2021
"""This module contains a object that represents Tests for Telegram Bot"""
2122

22-
import sys
23-
from datetime import datetime
2423
import io
24+
from datetime import datetime
25+
import sys
2526

2627
from flaky import flaky
2728

@@ -54,26 +55,22 @@ def testGetMe(self):
5455
@flaky(3, 1)
5556
@timeout(10)
5657
def testSendMessage(self):
57-
message = self._bot.sendMessage(
58-
chat_id=self._chat_id,
59-
text='Моё судно на воздушной подушке полно угрей')
58+
message = self._bot.sendMessage(chat_id=self._chat_id,
59+
text='Моё судно на воздушной подушке полно угрей')
6060

6161
self.assertTrue(self.is_json(message.to_json()))
62-
self.assertEqual(message.text,
63-
u'Моё судно на воздушной подушке полно угрей')
62+
self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей')
6463
self.assertTrue(isinstance(message.date, datetime))
6564

6665
@flaky(3, 1)
6766
@timeout(10)
6867
def testSilentSendMessage(self):
69-
message = self._bot.sendMessage(
70-
chat_id=self._chat_id,
71-
text='Моё судно на воздушной подушке полно угрей',
72-
disable_notification=True)
68+
message = self._bot.sendMessage(chat_id=self._chat_id,
69+
text='Моё судно на воздушной подушке полно угрей',
70+
disable_notification=True)
7371

7472
self.assertTrue(self.is_json(message.to_json()))
75-
self.assertEqual(message.text,
76-
u'Моё судно на воздушной подушке полно угрей')
73+
self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей')
7774
self.assertTrue(isinstance(message.date, datetime))
7875

7976
@flaky(3, 1)
@@ -100,10 +97,9 @@ def testForwardMessage(self):
10097
@flaky(3, 1)
10198
@timeout(10)
10299
def testSendPhoto(self):
103-
message = self._bot.sendPhoto(
104-
photo=open('tests/data/telegram.png', 'rb'),
105-
caption='testSendPhoto',
106-
chat_id=self._chat_id)
100+
message = self._bot.sendPhoto(photo=open('tests/data/telegram.png', 'rb'),
101+
caption='testSendPhoto',
102+
chat_id=self._chat_id)
107103

108104
self.assertTrue(self.is_json(message.to_json()))
109105
self.assertEqual(message.photo[0].file_size, 1451)
@@ -112,11 +108,10 @@ def testSendPhoto(self):
112108
@flaky(3, 1)
113109
@timeout(10)
114110
def testSilentSendPhoto(self):
115-
message = self._bot.sendPhoto(
116-
photo=open('tests/data/telegram.png', 'rb'),
117-
caption='testSendPhoto',
118-
chat_id=self._chat_id,
119-
disable_notification=True)
111+
message = self._bot.sendPhoto(photo=open('tests/data/telegram.png', 'rb'),
112+
caption='testSendPhoto',
113+
chat_id=self._chat_id,
114+
disable_notification=True)
120115

121116
self.assertTrue(self.is_json(message.to_json()))
122117
self.assertEqual(message.photo[0].file_size, 1451)
@@ -125,41 +120,35 @@ def testSilentSendPhoto(self):
125120
@flaky(3, 1)
126121
@timeout(10)
127122
def testResendPhoto(self):
128-
message = self._bot.sendPhoto(
129-
photo='AgADAQADyKcxGx8j9Qdp6d-gpUsw4Gja1i8ABEVJsVqQk8LfJ3wAAgI',
130-
chat_id=self._chat_id)
123+
message = self._bot.sendPhoto(photo='AgADAQADyKcxGx8j9Qdp6d-gpUsw4Gja1i8ABEVJsVqQk8LfJ3wAAgI',
124+
chat_id=self._chat_id)
131125

132126
self.assertTrue(self.is_json(message.to_json()))
133-
self.assertEqual(
134-
message.photo[0].file_id,
135-
'AgADAQADyKcxGx8j9Qdp6d-gpUsw4Gja1i8ABEVJsVqQk8LfJ3wAAgI')
127+
self.assertEqual(message.photo[0].file_id, 'AgADAQADyKcxGx8j9Qdp6d-gpUsw4Gja1i8ABEVJsVqQk8LfJ3wAAgI')
136128

137129
@flaky(3, 1)
138130
@timeout(10)
139131
def testSendJPGURLPhoto(self):
140-
message = self._bot.sendPhoto(
141-
photo='http://dummyimage.com/600x400/000/fff.jpg&text=telegram',
142-
chat_id=self._chat_id)
132+
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.jpg&text=telegram',
133+
chat_id=self._chat_id)
143134

144135
self.assertTrue(self.is_json(message.to_json()))
145136
self.assertEqual(message.photo[0].file_size, 822)
146137

147138
@flaky(3, 1)
148139
@timeout(10)
149140
def testSendPNGURLPhoto(self):
150-
message = self._bot.sendPhoto(
151-
photo='http://dummyimage.com/600x400/000/fff.png&text=telegram',
152-
chat_id=self._chat_id)
141+
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.png&text=telegram',
142+
chat_id=self._chat_id)
153143

154144
self.assertTrue(self.is_json(message.to_json()))
155145
self.assertEqual(message.photo[0].file_size, 684)
156146

157147
@flaky(3, 1)
158148
@timeout(10)
159149
def testSendGIFURLPhoto(self):
160-
message = self._bot.sendPhoto(
161-
photo='http://dummyimage.com/600x400/000/fff.gif&text=telegram',
162-
chat_id=self._chat_id)
150+
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.gif&text=telegram',
151+
chat_id=self._chat_id)
163152

164153
self.assertTrue(self.is_json(message.to_json()))
165154
self.assertEqual(message.photo[0].file_size, 684)
@@ -169,7 +158,8 @@ def testSendGIFURLPhoto(self):
169158
def testSendBufferedReaderPhoto(self):
170159
photo = open('tests/data/telegram.png', 'rb')
171160
br_photo = io.BufferedReader(io.BytesIO(photo.read()))
172-
message = self._bot.sendPhoto(photo=br_photo, chat_id=self._chat_id)
161+
message = self._bot.sendPhoto(photo=br_photo,
162+
chat_id=self._chat_id)
173163

174164
self.assertTrue(self.is_json(message.to_json()))
175165
self.assertEqual(message.photo[0].file_size, 1451)
@@ -189,8 +179,7 @@ def testGetUserProfilePhotos(self):
189179
self.assertEqual(upf.photos[0][0].file_size, 12421)
190180

191181
def _test_invalid_token(self, token):
192-
self.assertRaisesRegexp(telegram.error.InvalidToken, 'Invalid token',
193-
telegram.Bot, token)
182+
self.assertRaisesRegexp(telegram.error.InvalidToken, 'Invalid token', telegram.Bot, token)
194183

195184
def testInvalidToken1(self):
196185
self._test_invalid_token('123')
@@ -202,14 +191,12 @@ def testInvalidToken3(self):
202191
self._test_invalid_token('12:')
203192

204193
def testUnauthToken(self):
205-
with self.assertRaisesRegexp(telegram.error.Unauthorized,
206-
'Unauthorized'):
194+
with self.assertRaisesRegexp(telegram.error.Unauthorized, 'Unauthorized'):
207195
bot = telegram.Bot('1234:abcd1234')
208196
bot.getMe()
209197

210198
def testInvalidSrvResp(self):
211-
with self.assertRaisesRegexp(telegram.TelegramError,
212-
'Invalid server response'):
199+
with self.assertRaisesRegexp(telegram.TelegramError, 'Invalid server response'):
213200
# bypass the valid token check
214201
bot = telegram.Bot.__new__(telegram.Bot)
215202
bot.base_url = 'https://api.telegram.org/bot{0}'.format('12')

tests/test_chat.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# !/usr/bin/env python
1+
#!/usr/bin/env python
22
#
33
# A library that provides a Python interface to the Telegram Bot API
44
# Copyright (C) 2015-2016
@@ -16,11 +16,11 @@
1616
#
1717
# You should have received a copy of the GNU General Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
1920
"""This module contains a object that represents Tests for Telegram Chat"""
2021

21-
import sys
2222
import unittest
23-
23+
import sys
2424
sys.path.append('.')
2525

2626
import telegram
@@ -66,6 +66,5 @@ def test_group_chat_to_dict(self):
6666
self.assertEqual(group_chat['title'], self.title)
6767
self.assertEqual(group_chat['type'], self.type)
6868

69-
7069
if __name__ == '__main__':
7170
unittest.main()

tests/test_contact.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# !/usr/bin/env python
1+
#!/usr/bin/env python
22
#
33
# A library that provides a Python interface to the Telegram Bot API
44
# Copyright (C) 2015-2016
@@ -16,11 +16,11 @@
1616
#
1717
# You should have received a copy of the GNU General Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
1920
"""This module contains a object that represents Tests for Telegram Contact"""
2021

21-
import sys
2222
import unittest
23-
23+
import sys
2424
sys.path.append('.')
2525

2626
import telegram
@@ -65,6 +65,5 @@ def test_contact_to_dict(self):
6565
self.assertEqual(contact['last_name'], self.last_name)
6666
self.assertEqual(contact['user_id'], self.user_id)
6767

68-
6968
if __name__ == '__main__':
7069
unittest.main()

tests/test_emoji.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
#
1717
# You should have received a copy of the GNU General Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
1920
"""This module contains a object that represents Tests for Telegram Emoji"""
2021

21-
import sys
2222
import unittest
23-
23+
import sys
2424
sys.path.append('.')
2525

26+
import telegram
2627
from telegram.emoji import Emoji
2728
from tests.base import BaseTest
2829

tests/test_jobqueue.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929
else:
3030
import unittest
3131

32-
try:
33-
from urllib2 import urlopen, Request
34-
except ImportError:
35-
from urllib.request import Request, urlopen
36-
3732
sys.path.append('.')
3833

3934
from telegram.ext import JobQueue, Updater

tests/test_location.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
#
1717
# You should have received a copy of the GNU General Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
1920
"""This module contains a object that represents Tests for Telegram Location"""
2021

21-
import sys
2222
import unittest
23-
23+
import sys
2424
sys.path.append('.')
2525

2626
import telegram
@@ -40,7 +40,8 @@ def setUp(self):
4040
}
4141

4242
def test_send_location_implicit_args(self):
43-
message = self._bot.sendLocation(self._chat_id, self.latitude,
43+
message = self._bot.sendLocation(self._chat_id,
44+
self.latitude,
4445
self.longitude)
4546

4647
location = message.location

0 commit comments

Comments
 (0)
X Tutup