Skip to content

Commit

Permalink
Merge pull request #542 from python-telegram-bot/urllib3-vendor-beta
Browse files Browse the repository at this point in the history
Vendor (embed) urllib3 with our package.
  • Loading branch information
tsnoam committed Apr 29, 2017
2 parents c7dbdce + 34059c9 commit ca43510
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 54 deletions.
5 changes: 4 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[run]
source = telegram
omit = telegram/vendor/*

[report]
omit = tests/
omit =
tests/
telegram/vendor/*
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "telegram/vendor/urllib3"]
path = telegram/vendor/urllib3
url = https://github.com/python-telegram-bot/urllib3.git
branch = ptb
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ install:
- pip install -r requirements-dev.txt
- if [[ $TRAVIS_PYTHON_VERSION != 'pypy'* ]]; then pip install ujson; fi
script:
- nosetests -v --with-flaky --no-flaky-report --with-coverage --cover-package=telegram/
- nosetests -v --with-flaky --no-flaky-report --with-coverage --cover-package=telegram/ tests
- if [[ $TRAVIS_PYTHON_VERSION == 3.5 ]]; then pre-commit run --all-files; fi
after_success:
coveralls
7 changes: 6 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ Credits

``python-telegram-bot`` was originally created by
`Leandro Toledo <https://github.com/leandrotoledo>`_ and is now maintained by
`Jannes Höke <https://github.com/jh0ker>`_ (`@jh0ker <https://t.me/jh0ker>`_ on Telegram).
`Jannes Höke <https://github.com/jh0ker>`_ (`@jh0ker <https://t.me/jh0ker>`_ on Telegram) and
`Noam Meltzer <https://github.com/tsnoam>`_.

We're vendoring urllib3 as part of ``python-telegram-bot`` which is distributed under the MIT
license. For more info, full credits & license terms, the sources can be found here:
`https://github.com/python-telegram-bot/urllib3`.

Contributors
------------
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
future>=0.15.2
urllib3==1.20
certifi
9 changes: 8 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def requirements():
return requirements_list


packages = find_packages(exclude=['tests*'])
packages.extend(['telegram.vendor.urllib3.urllib3',
'telegram.vendor.urllib3.urllib3.packages', 'telegram.vendor.urllib3.urllib3.packages.ssl_match_hostname',
'telegram.vendor.urllib3.urllib3.packages.backports', 'telegram.vendor.urllib3.urllib3.contrib',
'telegram.vendor.urllib3.urllib3.util',
])

with codecs.open('README.rst', 'r', 'utf-8') as fd:
fn = os.path.join('telegram', 'version.py')
with open(fn) as fh:
Expand All @@ -32,7 +39,7 @@ def requirements():
keywords='python telegram bot api wrapper',
description="We have made you a wrapper you can't refuse",
long_description=fd.read(),
packages=find_packages(exclude=['tests*']),
packages=packages,
install_requires=requirements(),
extras_require={
'json': 'ujson',
Expand Down
4 changes: 4 additions & 0 deletions telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"""A library that provides a Python interface to the Telegram Bot API"""

from sys import version_info
import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'vendor', 'urllib3'))

from .base import TelegramObject
from .user import User
Expand Down
139 changes: 91 additions & 48 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,31 +129,33 @@ def decorator(self, *args, **kwargs):

return decorator

def message(func):
def _message_wrapper(self, url, data, *args, **kwargs):
if kwargs.get('reply_to_message_id'):
data['reply_to_message_id'] = kwargs.get('reply_to_message_id')

@functools.wraps(func)
def decorator(self, *args, **kwargs):
url, data = func(self, *args, **kwargs)
if kwargs.get('disable_notification'):
data['disable_notification'] = kwargs.get('disable_notification')

if kwargs.get('reply_to_message_id'):
data['reply_to_message_id'] = kwargs.get('reply_to_message_id')
if kwargs.get('reply_markup'):
reply_markup = kwargs.get('reply_markup')
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup

if kwargs.get('disable_notification'):
data['disable_notification'] = kwargs.get('disable_notification')
result = self._request.post(url, data, timeout=kwargs.get('timeout'))

if kwargs.get('reply_markup'):
reply_markup = kwargs.get('reply_markup')
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
if result is True:
return result

result = self._request.post(url, data, timeout=kwargs.get('timeout'))
return Message.de_json(result, self)

if result is True:
return result
def message(func):

return Message.de_json(result, self)
@functools.wraps(func)
def decorator(self, *args, **kwargs):
url, data = func(self, *args, **kwargs)
return Bot._message_wrapper(self, url, data, *args, **kwargs)

return decorator

Expand Down Expand Up @@ -283,15 +285,14 @@ def forwardMessage(self,
return url, data

@log
@message
def sendPhoto(self,
chat_id,
photo,
caption=None,
disable_notification=False,
reply_to_message_id=None,
reply_markup=None,
timeout=None,
timeout=20.,
**kwargs):
"""Use this method to send photos.
Expand All @@ -308,9 +309,7 @@ def sendPhoto(self,
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
to remove reply keyboard or to force a reply from the user.
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
from the server (instead of the one specified during creation of the connection
pool).
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
**kwargs (dict): Arbitrary keyword arguments.
Returns:
Expand All @@ -327,10 +326,19 @@ def sendPhoto(self,
if caption:
data['caption'] = caption

return url, data
return self._message_wrapper(
url,
data,
chat_id=chat_id,
photo=photo,
caption=caption,
disable_notification=disable_notification,
reply_to_message_id=reply_to_message_id,
reply_markup=reply_markup,
timeout=timeout,
**kwargs)

@log
@message
def sendAudio(self,
chat_id,
audio,
Expand All @@ -341,7 +349,7 @@ def sendAudio(self,
disable_notification=False,
reply_to_message_id=None,
reply_markup=None,
timeout=None,
timeout=20.,
**kwargs):
"""Use this method to send audio files, if you want Telegram clients to
display them in the music player. Your audio must be in an .mp3 format.
Expand Down Expand Up @@ -370,9 +378,7 @@ def sendAudio(self,
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
to remove reply keyboard or to force a reply from the user.
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
from the server (instead of the one specified during creation of the connection
pool).
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
**kwargs (dict): Arbitrary keyword arguments.
Returns:
Expand All @@ -395,10 +401,22 @@ def sendAudio(self,
if caption:
data['caption'] = caption

return url, data
return self._message_wrapper(
url,
data,
chat_id=chat_id,
audio=audio,
duration=duration,
performer=performer,
title=title,
caption=caption,
disable_notification=disable_notification,
reply_to_message_id=reply_to_message_id,
reply_markup=reply_markup,
timeout=20.,
**kwargs)

@log
@message
def sendDocument(self,
chat_id,
document,
Expand All @@ -407,7 +425,7 @@ def sendDocument(self,
disable_notification=False,
reply_to_message_id=None,
reply_markup=None,
timeout=None,
timeout=20.,
**kwargs):
"""Use this method to send general files.
Expand All @@ -426,9 +444,7 @@ def sendDocument(self,
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
to remove reply keyboard or to force a reply from the user.
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
from the server (instead of the one specified during creation of the connection
pool).
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
**kwargs (dict): Arbitrary keyword arguments.
Returns:
Expand All @@ -447,7 +463,18 @@ def sendDocument(self,
if caption:
data['caption'] = caption

return url, data
return self._message_wrapper(
url,
data,
chat_id=chat_id,
document=document,
filename=filename,
caption=caption,
disable_notification=disable_notification,
reply_to_message_id=reply_to_message_id,
reply_markup=reply_markup,
timeout=timeout,
**kwargs)

@log
@message
Expand Down Expand Up @@ -492,7 +519,6 @@ def sendSticker(self,
return url, data

@log
@message
def sendVideo(self,
chat_id,
video,
Expand All @@ -501,7 +527,7 @@ def sendVideo(self,
disable_notification=False,
reply_to_message_id=None,
reply_markup=None,
timeout=None,
timeout=20.,
**kwargs):
"""Use this method to send video files, Telegram clients support mp4
videos (other formats may be sent as telegram.Document).
Expand All @@ -521,9 +547,7 @@ def sendVideo(self,
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
to remove reply keyboard or to force a reply from the user.
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
from the server (instead of the one specified during creation of the connection
pool).
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
Returns:
:class:`telegram.Message`: On success, instance representing the message posted.
Expand All @@ -541,10 +565,20 @@ def sendVideo(self,
if caption:
data['caption'] = caption

return url, data
return self._message_wrapper(
url,
data,
chat_id=chat_id,
video=video,
duration=duration,
caption=caption,
disable_notification=disable_notification,
reply_to_message_id=reply_to_message_id,
reply_markup=reply_markup,
timeout=timeout,
**kwargs)

@log
@message
def sendVoice(self,
chat_id,
voice,
Expand All @@ -553,7 +587,7 @@ def sendVoice(self,
disable_notification=False,
reply_to_message_id=None,
reply_markup=None,
timeout=None,
timeout=20.,
**kwargs):
"""Use this method to send audio files, if you want Telegram clients to display the file as
a playable voice message. For this to work, your audio must be in an .ogg file encoded with
Expand All @@ -575,9 +609,7 @@ def sendVoice(self,
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
to remove reply keyboard or to force a reply from the user.
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
from the server (instead of the one specified during creation of the connection
pool).
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
**kwargs (dict): Arbitrary keyword arguments.
Returns:
Expand All @@ -596,7 +628,18 @@ def sendVoice(self,
if caption:
data['caption'] = caption

return url, data
return self._message_wrapper(
url,
data,
chat_id=chat_id,
voice=voice,
duration=duration,
caption=caption,
disable_notification=disable_notification,
reply_to_message_id=reply_to_message_id,
reply_markup=reply_markup,
timeout=timeout,
**kwargs)

@log
@message
Expand Down
2 changes: 1 addition & 1 deletion telegram/utils/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self,
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
],
timeout=urllib3.Timeout(
connect=self._connect_timeout, read=read_timeout),)
connect=self._connect_timeout, read=read_timeout, total=None))

# Set a proxy according to the following order:
# * proxy defined in proxy_url (+ urllib3_proxy_kwargs)
Expand Down
1 change: 1 addition & 0 deletions telegram/vendor/urllib3
Submodule urllib3 added at 4b076e

0 comments on commit ca43510

Please sign in to comment.