Make some dependencies optional

This commit is contained in:
Lorenz Diener 2019-05-31 14:12:04 +02:00
parent 739d22e642
commit e692fac905
3 changed files with 41 additions and 8 deletions

View file

@ -1135,6 +1135,9 @@ displayed.
Mastodon allows an application to have one webpush subscription per user at a time.
All crypto utilities require Mastodon.pys optional "webpush" feature dependencies
(specifically, the "cryptography" and "http_ece" packages).
.. automethod:: Mastodon.push_subscription
.. automethod:: Mastodon.push_subscription_set
.. automethod:: Mastodon.push_subscription_update

View file

@ -20,9 +20,20 @@ import threading
import sys
import six
from decorator import decorate
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import ec
import http_ece
IMPL_HAS_CRYPTO = True
try:
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import ec
except:
IMPL_HAS_CRYPTO = False
IMPL_HAS_ECE = True
try:
import http_ece
except:
IMPL_HAS_ECE = False
import base64
import json
import blurhash
@ -2306,6 +2317,9 @@ class Mastodon:
Returns two dicts: One with the private key and shared secret and another with the
public key and shared secret.
"""
if not IMPL_HAS_CRYPTO:
raise NotImplementedError('To use the crypto tools, please install the webpush feature dependencies.')
push_key_pair = ec.generate_private_key(ec.SECP256R1(), default_backend())
push_key_priv = push_key_pair.private_numbers().private_value
push_key_pub = push_key_pair.public_key().public_numbers().encode_point()
@ -2331,6 +2345,9 @@ class Mastodon:
Returns the decoded webpush as a `push notification dict`_.
"""
if (not IMPL_HAS_ECE) or (not IMPL_HAS_CRYPTO):
raise NotImplementedError('To use the crypto tools, please install the webpush feature dependencies.')
salt = self.__decode_webpush_b64(encryption_header.split("salt=")[1].strip())
dhparams = self.__decode_webpush_b64(crypto_key_header.split("dh=")[1].split(";")[0].strip())
p256ecdsa = self.__decode_webpush_b64(crypto_key_header.split("p256ecdsa=")[1].strip())

View file

@ -1,12 +1,27 @@
from setuptools import setup
test_deps = ['pytest', 'pytest-runner', 'pytest-cov', 'vcrpy', 'pytest-vcr', 'pytest-mock', 'requests-mock']
test_deps = [
'pytest',
'pytest-runner',
'pytest-cov',
'vcrpy',
'pytest-vcr',
'pytest-mock',
'requests-mock'
]
webpush_deps = [
'http_ece>=1.0.5',
'cryptography>=1.6.0',
]
extras = {
"test": test_deps
"test": test_deps,
"webpush": webpush_deps,
}
setup(name='Mastodon.py',
version='1.4.2',
version='1.4.3',
description='Python wrapper for the Mastodon API',
packages=['mastodon'],
install_requires=[
@ -16,8 +31,6 @@ setup(name='Mastodon.py',
'pytz',
'python-magic',
'decorator>=4.0.0',
'http_ece>=1.0.5',
'cryptography>=1.6.0',
'blurhash>=1.1.3',
],
tests_require=test_deps,