From 2b460a0799a4d20247812ccd7416d15abb955d7a Mon Sep 17 00:00:00 2001 From: Stefan Lehmann Date: Thu, 22 Jun 2017 11:15:24 +0000 Subject: [PATCH 1/2] improve requirements of example --- .gitignore | 1 + example/requirements/Makefile | 23 +++++++++++++++ .../{requirements.in => requirements/base.in} | 0 .../base.txt} | 15 +++++----- example/requirements/develop.in | 2 ++ example/requirements/develop.txt | 28 +++++++++++++++++++ requirements/develop.in | 1 + requirements/develop.txt | 1 + 8 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 example/requirements/Makefile rename example/{requirements.in => requirements/base.in} (100%) rename example/{requirements.txt => requirements/base.txt} (64%) create mode 100644 example/requirements/develop.in create mode 100644 example/requirements/develop.txt diff --git a/.gitignore b/.gitignore index 894689e..b0ad86d 100644 --- a/.gitignore +++ b/.gitignore @@ -91,3 +91,4 @@ ENV/ doc/_build/* *.crt .mypy_cache/* +venv_*/ diff --git a/example/requirements/Makefile b/example/requirements/Makefile new file mode 100644 index 0000000..81d132e --- /dev/null +++ b/example/requirements/Makefile @@ -0,0 +1,23 @@ +.PHONY: all check clean + + +objects = $(wildcard *.in) +outputs = $(objects:.in=.txt) + + +all: $(outputs) + + +%.txt: %.in + pip-compile -v -U --output-file $@ $< + + +develop.txt: base.txt + + +check: + @which pip-compile > /dev/null + + +clean: + -rm *.txt diff --git a/example/requirements.in b/example/requirements/base.in similarity index 100% rename from example/requirements.in rename to example/requirements/base.in diff --git a/example/requirements.txt b/example/requirements/base.txt similarity index 64% rename from example/requirements.txt rename to example/requirements/base.txt index 6020bbb..8ef4c22 100644 --- a/example/requirements.txt +++ b/example/requirements/base.txt @@ -2,24 +2,25 @@ # This file is autogenerated by pip-compile # To update, run: # -# pip-compile --output-file requirements.txt requirements.in +# pip-compile --output-file base.txt base.in # click==6.7 # via flask dominate==2.3.1 # via flask-bootstrap enum-compat==0.0.2 # via eventlet +enum34==1.1.6 # via enum-compat eventlet==0.21.0 flask-bootstrap==3.3.7.1 -flask-mqtt==0.0.4 +flask-mqtt==0.0.5 flask-socketio==2.8.6 -flask==0.12.1 +flask==0.12.2 greenlet==0.4.12 # via eventlet itsdangerous==0.24 # via flask jinja2==2.9.6 # via flask markupsafe==1.0 # via jinja2 -paho-mqtt==1.2.3 # via flask-mqtt -python-engineio==1.3.2 # via flask-socketio, python-socketio -python-socketio==1.7.4 # via flask-socketio +paho-mqtt==1.3.0 # via flask-mqtt +python-engineio==1.5.4 # via flask-socketio, python-socketio +python-socketio==1.7.5 # via flask-socketio six==1.10.0 # via python-engineio, python-socketio typing==3.6.1 # via flask-mqtt visitor==0.1.3 # via flask-bootstrap -werkzeug==0.12.1 # via flask +werkzeug==0.12.2 # via flask diff --git a/example/requirements/develop.in b/example/requirements/develop.in new file mode 100644 index 0000000..29e7210 --- /dev/null +++ b/example/requirements/develop.in @@ -0,0 +1,2 @@ +-r base.txt +pip-tools diff --git a/example/requirements/develop.txt b/example/requirements/develop.txt new file mode 100644 index 0000000..5b9add5 --- /dev/null +++ b/example/requirements/develop.txt @@ -0,0 +1,28 @@ +# +# This file is autogenerated by pip-compile +# To update, run: +# +# pip-compile --output-file develop.txt develop.in +# +click==6.7 +dominate==2.3.1 +enum-compat==0.0.2 +enum34==1.1.6 +eventlet==0.21.0 +first==2.0.1 # via pip-tools +flask-bootstrap==3.3.7.1 +flask-mqtt==0.0.5 +flask-socketio==2.8.6 +flask==0.12.2 +greenlet==0.4.12 +itsdangerous==0.24 +jinja2==2.9.6 +markupsafe==1.0 +paho-mqtt==1.3.0 +pip-tools==1.9.0 +python-engineio==1.5.4 +python-socketio==1.7.5 +six==1.10.0 +typing==3.6.1 +visitor==0.1.3 +werkzeug==0.12.2 diff --git a/requirements/develop.in b/requirements/develop.in index 918e111..09e771a 100644 --- a/requirements/develop.in +++ b/requirements/develop.in @@ -1,3 +1,4 @@ -r base.txt +coverage mypy sphinx diff --git a/requirements/develop.txt b/requirements/develop.txt index 27220ea..761fed5 100644 --- a/requirements/develop.txt +++ b/requirements/develop.txt @@ -9,6 +9,7 @@ babel==2.4.0 # via sphinx certifi==2017.4.17 # via requests chardet==3.0.4 # via requests click==6.7 +coverage==4.4.1 docutils==0.13.1 # via sphinx flask==0.12.2 idna==2.5 # via requests From 3fec853b6566e96e2cbb8f6091dce2a3118528dc Mon Sep 17 00:00:00 2001 From: Stefan Lehmann Date: Thu, 22 Jun 2017 13:08:11 +0000 Subject: [PATCH 2/2] support Python 2.7 --- doc/changelog.rst | 4 ++++ doc/index.rst | 3 ++- flask_mqtt/__init__.py | 54 ++++++++++++++++++++++++++---------------- setup.cfg | 4 ++++ setup.py | 2 +- 5 files changed, 45 insertions(+), 22 deletions(-) create mode 100644 setup.cfg diff --git a/doc/changelog.rst b/doc/changelog.rst index 2248aca..110eece 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -1,6 +1,10 @@ Changelog ========= +Version 0.0.6 +------------- +* Flask-MQTT now supports Python 2.7 + Version 0.0.5 ------------- * fixed unsupported type annotations for older Python 3 versions diff --git a/doc/index.rst b/doc/index.rst index f6c3037..3548126 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -25,7 +25,8 @@ or monitor and control these devices from one or multiple clients. Support ======= -This package uses type annotations so it needs Python 3 and the `typing package `_ installed. Since Python 3.6 the typing package is a builtin package. Python 2 is not supported. +This package uses type annotations so it needs Python 3.6 or Python 2.7/3.x +with the `typing package `_ installed. Indices and tables ================== diff --git a/flask_mqtt/__init__.py b/flask_mqtt/__init__.py index e544c0a..f91e41a 100644 --- a/flask_mqtt/__init__.py +++ b/flask_mqtt/__init__.py @@ -11,23 +11,25 @@ MQTT_LOG_WARNING -__version__ = '0.0.5' +__version__ = '0.0.6' class Mqtt(): - def __init__(self, app: Flask=None) -> None: + def __init__(self, app): + # type: (Flask) -> None self.app = app self.client = Client() self.client.on_connect = self._handle_connect self.client.on_disconnect = self._handle_disconnect - self.topics = [] #: type: List + self.topics = [] # type: List[str] self.connected = False if app is not None: self.init_app(app) - def init_app(self, app: Flask) -> None: + def init_app(self, app): + # type: (Flask) -> None self.username = app.config.get('MQTT_USERNAME') self.password = app.config.get('MQTT_PASSWORD') self.broker_url = app.config.get('MQTT_BROKER_URL', 'localhost') @@ -46,7 +48,8 @@ def init_app(self, app: Flask) -> None: self._connect() - def _connect(self) -> None: + def _connect(self): + # type: () -> None if self.username is not None: self.client.username_pw_set(self.username, self.password) @@ -71,20 +74,23 @@ def _connect(self) -> None: keepalive=self.keepalive ) - def _disconnect(self) -> None: + def _disconnect(self): + # type: () -> None self.client.disconnect() - def _handle_connect(self, client: Client, userdata: Any, flags: Dict, - rc: int) -> None: + def _handle_connect(self, client, userdata, flags, rc): + # type: (Client, Any, Dict, int) -> None if rc == MQTT_ERR_SUCCESS: self.connected = True for topic in self.topics: self.client.subscribe(topic) - def _handle_disconnect(self, client: str, userdata: Any, rc: int) -> None: + def _handle_disconnect(self, client, userdata, rc): + # type: (str, Any, int) -> None self.connected = False - def on_topic(self, topic: str) -> Callable: + def on_topic(self, topic): + # type: (str) -> Callable """ Decorator to add a callback function that is called when a certain topic has been published. The callback function is expected to have the @@ -102,12 +108,14 @@ def handle_mytopic(client, userdata, message): .format(message.topic, message.payload.decode())) """ - def decorator(handler: Callable[[str], None]) -> Callable[[str], None]: + def decorator(handler): + # type: (Callable[[str], None]) -> Callable[[str], None] self.client.message_callback_add(topic, handler) return handler return decorator - def subscribe(self, topic: str, qos: int=0) -> None: + def subscribe(self, topic, qos=0): + # type: (str, int) -> None """ Subscribe to a certain topic. @@ -137,7 +145,8 @@ def subscribe(self, topic: str, qos: int=0) -> None: return result - def unsubscribe(self, topic: str) -> None: + def unsubscribe(self, topic): + # type: (str) -> None """ Unsubscribe from a single topic. @@ -157,7 +166,8 @@ def unsubscribe(self, topic: str) -> None: return result - def unsubscribe_all(self) -> None: + def unsubscribe_all(self): + # type: () -> None """ Unsubscribe from all topics. @@ -166,8 +176,8 @@ def unsubscribe_all(self) -> None: for topic in topics: self.unsubscribe(topic) - def publish(self, topic: str, payload: bytes=None, qos: int=0, - retain: bool=False) -> Tuple[int, int]: + def publish(self, topic, payload=None, qos=0, retain=False): + # type: (str, bytes, int, bool) -> Tuple[int, int] """ Send a message to the broker. @@ -192,7 +202,8 @@ def publish(self, topic: str, payload: bytes=None, qos: int=0, self.client.reconnect() return self.client.publish(topic, payload, qos, retain) - def on_message(self) -> Callable: + def on_message(self): + # type: () -> Callable """ Decorator to handle all messages that have been subscribed. @@ -206,12 +217,14 @@ def handle_messages(client, userdata, message): .format(message.topic, message.payload.decode())) """ - def decorator(handler: Callable) -> Callable: + def decorator(handler): + # type: (Callable) -> Callable self.client.on_message = handler return handler return decorator - def on_log(self) -> Callable: + def on_log(self): + # type: () -> Callable """ Decorator to handle MQTT logging. @@ -224,7 +237,8 @@ def handle_logging(client, userdata, level, buf): print(client, userdata, level, buf) """ - def decorator(handler: Callable) -> Callable: + def decorator(handler): + # type: (Callable) -> Callable self.client.on_log = handler return handler return decorator diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..abf6846 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,4 @@ +[mypy] +ignore_missing_imports = True +disallow_untyped_calls = True +disallow_untyped_defs = True diff --git a/setup.py b/setup.py index cfa6e1e..b5e6b1a 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ else: setup( name='Flask-MQTT', - version='0.0.5', + version='0.0.6', url='https://github.com/MrLeeh/Flask-MQTT', license='MIT', author='Stefan Lehmann',