Skip to content

Commit

Permalink
support Python 2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Lehmann committed Jun 22, 2017
1 parent 2b460a0 commit 3fec853
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
4 changes: 4 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://pypi.python.org/pypi/typing>`_ 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 <https://pypi.python.org/pypi/typing>`_ installed.

Indices and tables
==================
Expand Down
54 changes: 34 additions & 20 deletions flask_mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[mypy]
ignore_missing_imports = True
disallow_untyped_calls = True
disallow_untyped_defs = True
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 3fec853

Please sign in to comment.