From 1713c37dcaa5014a374644fb96243368da804790 Mon Sep 17 00:00:00 2001 From: "Xuan (Sean) Hu" Date: Mon, 17 Feb 2020 13:56:21 +0800 Subject: [PATCH] Better Topic Message Support. (#3) - `uids` is NOT required parameter for `send_message` now. - `flake8` and its extensions compatibility. - `setuptools_scm` for version. - `pipenv` for dependencies management. --- .gitignore | 2 + MANIFEST.in | 3 -- Makefile | 21 +++++------ Pipfile | 18 +++++++++ README-en.md | 5 ++- README.md | 5 ++- VERSION | 1 - setup.py | 55 +++++++++++++++------------- tox.ini | 15 +++++++- wxpusher/__init__.py | 9 ++--- wxpusher/exceptions.py | 3 +- wxpusher/tests/__init__.py | 3 +- wxpusher/tests/config.sample.py | 10 ++++- wxpusher/tests/exceptions.py | 3 +- wxpusher/tests/test_create_qrcode.py | 6 ++- wxpusher/tests/test_query_message.py | 5 ++- wxpusher/tests/test_query_user.py | 6 ++- wxpusher/tests/test_send_message.py | 25 ++++++++++--- wxpusher/wxpusher.py | 22 ++++++----- 19 files changed, 139 insertions(+), 78 deletions(-) delete mode 100644 MANIFEST.in create mode 100644 Pipfile delete mode 100644 VERSION diff --git a/.gitignore b/.gitignore index efeb4e1..f321900 100644 --- a/.gitignore +++ b/.gitignore @@ -129,4 +129,6 @@ dmypy.json .pyre/ # Custom +.vscode +Pipfile.lock config.py diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index 389b286..0000000 --- a/MANIFEST.in +++ /dev/null @@ -1,3 +0,0 @@ -include LICENSE -include README.md -include VERSION diff --git a/Makefile b/Makefile index 7641742..baec9c0 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,7 @@ -.PHONY: clean install dev lint pycodestyle pyflakes pylint test dist upload +.PHONY: clean install lint flake8 pylint test dist upload + +PIPENV := $(shell command -v pipenv > /dev/null && echo env) +PIPRUN := $(shell command -v pipenv > /dev/null && echo pipenv run) clean: find . -name '*.pyc' -print0 | xargs -0 rm -f @@ -9,21 +12,15 @@ clean: -rm -rf .tox .coverage install: - pip install . - -dev: - pip install .[dev] - -lint: pycodestyle pyflakes pylint + pip$(PIPENV) install . -pycodestyle: - -pycodestyle setup.py wxpusher +lint: flake8 pylint -pyflakes: - -pyflakes setup.py wxpusher +flake8: + ${PIPRUN} flake8 setup.py wxpusher pylint: - -pylint setup.py wxpusher + ${PIPRUN} pylint setup.py wxpusher test: tox diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..a0ff44d --- /dev/null +++ b/Pipfile @@ -0,0 +1,18 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] +flake8 = "*" +flake8-commas = "*" +flake8-docstrings = "*" +flake8-import-order = "*" +pep8-naming = "*" +pylint = "*" + +[packages] +wxpusher = {editable = true,path = "."} + +[requires] +python_version = "3" diff --git a/README-en.md b/README-en.md index f6dbe05..f9e5ff1 100644 --- a/README-en.md +++ b/README-en.md @@ -21,7 +21,10 @@ pip install -U wxpusher ```python from wxpusher import WxPusher -WxPusher.send_message('', '', '') +WxPusher.send_message('', + uids='', + topic_ids='', + token='') WxPusher.query_message('') WxPusher.create_qrcode('', '', '') WxPusher.query_user('', '', '') diff --git a/README.md b/README.md index 7a3456f..fbcafdd 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,10 @@ pip install -U wxpusher ```python from wxpusher import WxPusher -WxPusher.send_message('', '', '') +WxPusher.send_message('', + uids='', + topic_ids='', + token='') WxPusher.query_message('') WxPusher.create_qrcode('', '', '') WxPusher.query_user('', '', '') diff --git a/VERSION b/VERSION deleted file mode 100644 index 3e3c2f1..0000000 --- a/VERSION +++ /dev/null @@ -1 +0,0 @@ -2.1.1 diff --git a/setup.py b/setup.py index a8c7a5b..7da3600 100644 --- a/setup.py +++ b/setup.py @@ -1,49 +1,52 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ +Python packaging for wxpusher. + File: setup.py Author: huxuan Email: i(at)huxuan.org -Description: Python packaging for wxpusher. """ +from pkg_resources import DistributionNotFound +from pkg_resources import get_distribution + from setuptools import setup +NAME = 'wxpusher' + CLASSIFIERS = [ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'License :: OSI Approved :: Apache Software License', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3 :: Only', - 'Topic :: Utilities' + 'Topic :: Utilities', ] INSTALL_REQUIRES = [ - 'requests' + 'requests', ] -DEV_REQUIRES = [ - 'pycodestyle', - 'pyflakes', - 'pylint' -] - -TEST_REQUIRES = [ - 'coverage', - 'nose' -] - -EXTRAS_REQUIRE = { - 'dev': DEV_REQUIRES, - 'test': TEST_REQUIRES -} - DESCRIPTION = ( 'WxPusher Python SDK.' ) -VERSION = open('VERSION').read().strip() +KEYWORDS = [ + 'wxpusher', + 'wechat', + 'weixin', + 'notification', + 'push-notification', + 'python-sdk', +] + +try: + VERSION = f'v{get_distribution(NAME).version}' +except DistributionNotFound: + VERSION = 'master' + PROJECT_URL = 'https://github.com/wxpusher/wxpusher-sdk-python' -BASE_URL = f'{PROJECT_URL}/blob/v{VERSION}' +BASE_URL = f'{PROJECT_URL}/blob/{VERSION}' def readme(): @@ -54,19 +57,19 @@ def readme(): return content -setup(name='wxpusher', - version=VERSION, +setup(name=NAME, description=DESCRIPTION, long_description=readme(), long_description_content_type='text/markdown', classifiers=CLASSIFIERS, - keywords='wxpusher wechat push-notification', - url='https://github.com/wxpusher/wxpusher-sdk-python', + keywords=' '.join(KEYWORDS), + url=PROJECT_URL, author='Xuan (Sean) Hu', author_email='i+wxpusher@huxuan.org', license='Apache License 2.0', packages=['wxpusher'], + use_scm_version=True, + setup_requires=['setuptools_scm'], install_requires=INSTALL_REQUIRES, - extras_require=EXTRAS_REQUIRE, python_requires='>=3', include_package_data=True) diff --git a/tox.ini b/tox.ini index b3602e7..6ed2952 100644 --- a/tox.ini +++ b/tox.ini @@ -4,10 +4,21 @@ # and then run "tox" from this directory. [tox] -envlist = py3 +envlist = py3, lint-py3 [testenv] deps = - .[test] + coverage + nose + pipenv commands = + pipenv install --skip-lock nosetests --with-coverage --cover-erase --cover-package=wxpusher + +[testenv:lint-py3] +basepython = python3 +deps = + pipenv +commands= + pipenv install --skip-lock --dev + pipenv run make lint diff --git a/wxpusher/__init__.py b/wxpusher/__init__.py index 379f4a2..164771f 100644 --- a/wxpusher/__init__.py +++ b/wxpusher/__init__.py @@ -1,19 +1,18 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ +Init for WxPusher. + File: __init__.py Author: huxuan Email: i(at)huxuan.org -Description: init for WxPusher. """ -from .wxpusher import WxPusher from .exceptions import WxPusherException from .exceptions import WxPusherNoneTokenException +from .wxpusher import WxPusher __all__ = [ 'WxPusher', 'WxPusherException', - 'WxPusherNoneTokenException' + 'WxPusherNoneTokenException', ] - -__version__ = open('VERSION').read().strip() diff --git a/wxpusher/exceptions.py b/wxpusher/exceptions.py index ce60077..e8e5a6c 100644 --- a/wxpusher/exceptions.py +++ b/wxpusher/exceptions.py @@ -1,10 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ +WxPusher Exceptions. + File: exceptions.py Author: huxuan Email: i(at)huxuan.org -Description: WxPusher Exceptions. """ diff --git a/wxpusher/tests/__init__.py b/wxpusher/tests/__init__.py index 779e65a..3899ccf 100644 --- a/wxpusher/tests/__init__.py +++ b/wxpusher/tests/__init__.py @@ -1,10 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ +Init for unittest. + File: __init__.py Author: huxuan Email: i(at)huxuan.org -Description: Init for unittest. """ from . import exceptions diff --git a/wxpusher/tests/config.sample.py b/wxpusher/tests/config.sample.py index ba16879..7bb4c46 100644 --- a/wxpusher/tests/config.sample.py +++ b/wxpusher/tests/config.sample.py @@ -1,15 +1,21 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ +Unittest configuration sample. + File: config.sample.py Author: huxuan Email: i(at)huxuan.org -Description: Unittest configuration sample. """ # the `appToken` for test. TOKEN = '' # the `uids` for test, note that it should be a list. UIDS = [ - '' + '', +] + +# the `topic_ids` for test, note that it should be a list. +TOPIC_IDS = [ + '', ] diff --git a/wxpusher/tests/exceptions.py b/wxpusher/tests/exceptions.py index f4d1967..0b6a859 100644 --- a/wxpusher/tests/exceptions.py +++ b/wxpusher/tests/exceptions.py @@ -1,10 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ +Custom exceptions for unittest. + File: exceptions.py Author: huxuan Email: i(at)huxuan.org -Description: Custom exceptions for unittest. """ from wxpusher import WxPusherException diff --git a/wxpusher/tests/test_create_qrcode.py b/wxpusher/tests/test_create_qrcode.py index 4bbda5d..8c22b6b 100644 --- a/wxpusher/tests/test_create_qrcode.py +++ b/wxpusher/tests/test_create_qrcode.py @@ -1,10 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ +Unittest for creating qrcode. + File: test_create_qrcode.py Author: huxuan Email: i(at)huxuan.org -Description: Unittest for creating qrcode. """ import unittest @@ -18,12 +19,13 @@ class TestCreateQRCode(unittest.TestCase): @classmethod def setUpClass(cls): + """Set up for class.""" WxPusher.default_token = config.TOKEN def test_create_qrcode(self): """Positive case for creating qrcode.""" res = WxPusher.create_qrcode( - self.test_create_qrcode.__doc__ + self.test_create_qrcode.__doc__, ) self.assertIsInstance(res, dict) self.assertIn('code', res) diff --git a/wxpusher/tests/test_query_message.py b/wxpusher/tests/test_query_message.py index 9ee9d84..4e07ee4 100644 --- a/wxpusher/tests/test_query_message.py +++ b/wxpusher/tests/test_query_message.py @@ -1,13 +1,14 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ +Unittest for querying message. + File: test_send_message.py Author: huxuan Email: i(at)huxuan.org -Description: Unittest for querying message. """ -import unittest import random +import unittest from wxpusher import WxPusher diff --git a/wxpusher/tests/test_query_user.py b/wxpusher/tests/test_query_user.py index 368a23a..5d6c441 100644 --- a/wxpusher/tests/test_query_user.py +++ b/wxpusher/tests/test_query_user.py @@ -1,10 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ +Unittest for querying user. + File: test_send_message.py Author: huxuan Email: i(at)huxuan.org -Description: Unittest for querying user. """ import random import unittest @@ -19,12 +20,13 @@ class TestSendMessage(unittest.TestCase): @classmethod def setUpClass(cls): + """Set up for class.""" WxPusher.default_token = config.TOKEN def test_query_user(self): """Positive case for querying user.""" res = WxPusher.query_user( - 1, random.randint(1, 99) + 1, random.randint(1, 99), ) self.assertIsInstance(res, dict) self.assertIn('code', res) diff --git a/wxpusher/tests/test_send_message.py b/wxpusher/tests/test_send_message.py index f78b2af..15886c7 100644 --- a/wxpusher/tests/test_send_message.py +++ b/wxpusher/tests/test_send_message.py @@ -1,10 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ +Unittest for sending message. + File: test_send_message.py Author: huxuan Email: i(at)huxuan.org -Description: Unittest for sending message. """ import unittest @@ -18,14 +19,26 @@ class TestSendMessage(unittest.TestCase): @classmethod def setUpClass(cls): + """Set up for class.""" WxPusher.default_token = config.TOKEN - def test_send_message(self): - """Positive case for sending message.""" + def test_send_message_uid(self): + """Positive case for sending message with uid.""" + res = WxPusher.send_message( + self.test_send_message_uid.__doc__, + uids=config.UIDS, + url='http://example.com/', + ) + self.assertIsInstance(res, dict) + self.assertIn('code', res) + self.assertEqual(1000, res['code']) + + def test_send_message_topic_id(self): + """Positive case for sending message with topic_id.""" res = WxPusher.send_message( - self.test_send_message.__doc__, - config.UIDS, - url='http://example.com/' + self.test_send_message_topic_id.__doc__, + topic_ids=config.TOPIC_IDS, + url='http://example.com/', ) self.assertIsInstance(res, dict) self.assertIn('code', res) diff --git a/wxpusher/wxpusher.py b/wxpusher/wxpusher.py index 4a879e8..7982e99 100644 --- a/wxpusher/wxpusher.py +++ b/wxpusher/wxpusher.py @@ -1,10 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ +WxPusher Python SDK. + File: wxpusher.py Author: huxuan Email: i(at)huxuan.org -Description: WxPusher Python SDK. """ import requests @@ -15,18 +16,19 @@ class WxPusher(): """WxPusher Python SDK.""" + default_token = None @classmethod - def send_message(cls, content, uids, token=None, **kwargs): + def send_message(cls, content, **kwargs): """Send Message.""" payload = { - 'appToken': cls.get_token(token), + 'appToken': cls._get_token(kwargs.get('token')), 'content': content, 'contentType': kwargs.get('content_type', 1), 'topicIds': kwargs.get('topic_ids', []), - 'uids': uids, - 'url': kwargs.get('url') + 'uids': kwargs.get('uids', []), + 'url': kwargs.get('url'), } url = f'{BASEURL}/send/message' return requests.post(url, json=payload).json() @@ -41,9 +43,9 @@ def query_message(cls, message_id): def create_qrcode(cls, extra, valid_time=1800, token=None): """Create qrcode with extra callback information.""" payload = { - 'appToken': cls.get_token(token), + 'appToken': cls._get_token(token), 'extra': extra, - 'validTime': valid_time + 'validTime': valid_time, } url = f'{BASEURL}/fun/create/qrcode' return requests.post(url, json=payload).json() @@ -52,15 +54,15 @@ def create_qrcode(cls, extra, valid_time=1800, token=None): def query_user(cls, page, page_size, token=None): """Query users.""" payload = { - 'appToken': cls.get_token(token), + 'appToken': cls._get_token(token), 'page': page, - 'pageSize': page_size + 'pageSize': page_size, } url = f'{BASEURL}/fun/wxuser' return requests.get(url, params=payload).json() @classmethod - def get_token(cls, token=None): + def _get_token(cls, token=None): """Get token with validation.""" token = token or cls.default_token if not token: