Skip to content

Commit

Permalink
Type annotations (#13)
Browse files Browse the repository at this point in the history
* Added mypy hook to pre-commit , reorder imports, and pydocstyle. Fixed all issues hooks reported.

* Updated hooks

* Added mypy hook to pre-commit , reorder imports, and pydocstyle. Fixed all issues hooks reported.

* Updated hooks

* Test clean up

* Hook fixes

* Version bump

* Updated pre-commit hooks and applies their fixes

* Black formatting and remove unused # noqa

* Updated required paho-mqtt version
  • Loading branch information
djesic authored Nov 9, 2020
1 parent e51b135 commit 15cd0d7
Show file tree
Hide file tree
Showing 70 changed files with 948 additions and 571 deletions.
4 changes: 2 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[flake8]
ignore = E203, E266, E501, W503, F403, F401, C901
ignore = E203, E266, E501, W503, F403, C901, E402
max-line-length = 79
max-complexity = 18
select = B,C,E,F,W,T4,B9
select = B,C,E,F,W,T4,B9
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ venv.bak/
# Sublime Text stuff
*.sublime-workspace
*.sublime-project
sftp-config.json
sftp-config.json
11 changes: 11 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[mypy]

python_version = 3.7
disallow_untyped_defs = True
disallow_incomplete_defs = True
warn_redundant_casts = True
warn_unused_ignores = True
warn_unreachable = True
disallow_untyped_globals = True
show_column_numbers = True
pretty = true
33 changes: 28 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-json
- repo: https://github.com/asottile/reorder_python_imports
rev: v2.3.6
hooks:
- id: reorder-python-imports
- repo: https://github.com/ambv/black
rev: 19.3b0
rev: 20.8b1
hooks:
- id: black
language_version: python3.7
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.1
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
additional_dependencies: ['flake8-comprehensions', 'flake8-rst-docstrings']
- repo: https://github.com/pycqa/pydocstyle
rev: 5.1.1
hooks:
- id: pydocstyle
args:
- --ignore=D401,D203,D212,D104
exclude: ^(.*test.*)|(.*docs.*)$
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.790
hooks:
- id: flake8
- id: mypy
exclude: ^(.*test.*)|(.*docs.*)|(.*example.*)$
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Python 3 package for connecting devices to WolkAbout IoT Platform through [WolkGateway](https://github.com/Wolkabout/WolkGateway).

[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black) [![Documentation Status](https://readthedocs.org/projects/wolkgatewaymodule-sdk-python/badge/?version=latest)](https://wolkgatewaymodule-sdk-python.readthedocs.io/en/latest/?badge=latest) [![PyPI version](https://badge.fury.io/py/wolk-gateway-module.svg)](https://badge.fury.io/py/wolk-gateway-module) ![GitHub](https://img.shields.io/github/license/Wolkabout/WolkGatewayModule-SDK-Python.svg?style=flat-square) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/wolk-gateway-module.svg?style=flat-square)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black) [![Documentation Status](https://readthedocs.org/projects/wolkgatewaymodule-sdk-python/badge/?version=latest)](https://wolkgatewaymodule-sdk-python.readthedocs.io/en/latest/?badge=latest) [![PyPI version](https://badge.fury.io/py/wolk-gateway-module.svg)](https://badge.fury.io/py/wolk-gateway-module) ![GitHub](https://img.shields.io/github/license/Wolkabout/WolkGatewayModule-SDK-Python.svg?style=flat-square) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/wolk-gateway-module.svg?style=flat-square) [![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)

----
This package is meant to be used for developing WolkGateway modules that enable devices without IP connectivity to send their data to WolkAbout IoT Platform.
Expand Down
2 changes: 1 addition & 1 deletion docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ help:
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ make html

The docs will be generated, the output files will be placed in the `build/html/` directory, and can be browsed (locally) with any browser.

Alternatively, you can see browse the generated documentation hosted on Read the Docs [here](https://wolkgatewaymodule-sdk-python.readthedocs.io/en/latest/).
Alternatively, you can see browse the generated documentation hosted on Read the Docs [here](https://wolkgatewaymodule-sdk-python.readthedocs.io/en/latest/).
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
sphinx
sphinx-rtd-theme
paho_mqtt
paho_mqtt
2 changes: 1 addition & 1 deletion docs/source/abcs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ Connectivity Service
--------------------

.. autoclass:: wolk_gateway_module.connectivity.connectivity_service.ConnectivityService
:members:
:members:
2 changes: 0 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
Expand Down
2 changes: 1 addition & 1 deletion docs/source/device_creation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ After a device template has been created, now a device can be created from it.
.. autoclass:: wolk_gateway_module.model.device.Device
:members:

.. automethod:: wolk_gateway_module.model.device.Device.__init__
.. automethod:: wolk_gateway_module.model.device.Device.__init__
3 changes: 1 addition & 2 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ the specifics of their particular use case.

However, all the communication that is directed towards the gateway through
WolkConnect - BUS Handler is already provided with this package, an open source implementation
written in Python 3.7 that uses the MQTT protocol over TCP/IP to communicate with
written in Python 3.7 that uses the MQTT protocol over TCP/IP to communicate with
`WolkGateway <https://github.com/Wolkabout/WolkGateway>`_.


Expand All @@ -38,4 +38,3 @@ Indices and tables
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

16 changes: 8 additions & 8 deletions docs/source/interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Available device states:
An stub implementation would look something like this:

.. code-block:: python
def get_device_status(device_key):
if device_key == "DEVICE_KEY":
# Handle getting current device status here
Expand All @@ -39,7 +39,7 @@ Actuator functions
------------------

Actuators enable remote control over a device peripheral that can change between predefined states,
like turning a switch on or off, or setting a light dimmer to 20% intensity.
like turning a switch on or off, or setting a light dimmer to 20% intensity.

In order to enable remote control, the Platform first needs to be notified
about the actuators current state - is it ready to receive a command, is it busy changing its position
Expand All @@ -57,7 +57,7 @@ Available actuator states:
A stub implementation would look something like this:

.. code-block:: python
def get_actuator_status(device_key, reference):
if device_key == "DEVICE_KEY":
if reference == "SW":
Expand Down Expand Up @@ -95,8 +95,8 @@ as ``actuation_handler`` and ``actuator_status_provider``
actuator_status_provider=get_actuator_status,
)
Once ``Wolk.connect()`` has been called, it will call ``actuator_status_provider``
to get the current actuator status for each actuator of all added device.
Once ``Wolk.connect()`` has been called, it will call ``actuator_status_provider``
to get the current actuator status for each actuator of all added device.
However, publishing actuator statuses can be done explicitly by calling:

.. code-block:: python
Expand Down Expand Up @@ -169,8 +169,8 @@ as ``configuration_handler`` and ``configuration_provider``
configuration_provider=get_configuration,
)
Once ``Wolk.connect()`` has been called, it will call ``configuration_provider``
to get the current configuration options for each added device with configurations.
Once ``Wolk.connect()`` has been called, it will call ``configuration_provider``
to get the current configuration options for each added device with configurations.
However, publishing configurations can be done explicitly by calling:

.. code-block:: python
Expand Down Expand Up @@ -244,4 +244,4 @@ An object of this class needs to be passed to ``Wolk`` like so:
)
When ``Wolk.connect()`` is called it will use ``firmware_handler.get_firmware_version()`` for
each added device that has support for firmware update and report to WolkAbout IoT Platform.
each added device that has support for firmware update and report to WolkAbout IoT Platform.
2 changes: 1 addition & 1 deletion docs/source/protocols.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ MQTT Connectivity Service
-------------------------

.. autoclass:: wolk_gateway_module.mqtt_connectivity_service.MQTTConnectivityService
:members:
:members:
2 changes: 1 addition & 1 deletion docs/source/wolk.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ The ``Wolk`` class is a wrapper and an API for everything this package has to of
.. autoclass:: wolk_gateway_module.wolk.Wolk
:members:

.. automethod:: wolk_gateway_module.wolk.Wolk.__init__
.. automethod:: wolk_gateway_module.wolk.Wolk.__init__
2 changes: 1 addition & 1 deletion example/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"host" : "localhost",
"port" : 1883,
"module_name" : "Module-SDK-Python"
}
}
15 changes: 10 additions & 5 deletions example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from time import sleep, time
from typing import Dict, Tuple, Union
from random import randint
from time import sleep
from time import time
from typing import Dict
from typing import Tuple
from typing import Union

import wolk_gateway_module as wolk

Expand Down Expand Up @@ -153,13 +156,15 @@


class FirmwareHandlerImplementation(wolk.FirmwareHandler):
"""Handle firmware installation and abort commands, and report version.
"""
Handle firmware installation and abort commands, and report version.
Once an object of this class is passed to a Wolk object,
it will set callback methods `on_install_success` and
`on_install_fail` used for reporting the result of
the firmware update process. Use these callbacks in `install_firmware`
and `abort_installation` methods."""
and `abort_installation` methods.
"""

def install_firmware(
self, device_key: str, firmware_file_path: str
Expand Down Expand Up @@ -309,7 +314,7 @@ def get_actuator_status(


def get_configuration(
device_key: str
device_key: str,
) -> Dict[
str,
Union[
Expand Down
16 changes: 15 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,18 @@ exclude = '''
| build
| dist
)/
'''
'''

[tool.coverage.report]
exclude_lines = [
"raise NotImplementedError",
"def __repr__",
"if __name__ == .__main__.:",
]

[tool.coverage.run]
branch = true
omit = ["*/.local/*", "/usr/*"]

[tool.coverage.html]
directory = "coverage_html_directory"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
paho_mqtt==1.4.0
paho_mqtt>=1.4.0
20 changes: 18 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
"""Run this module to install package on your machine."""
# Copyright 2020 WolkAbout Technology s.r.o.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from os import path

import setuptools

from wolk_gateway_module import __version__

this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, "README.md"), encoding="utf-8") as f:
long_description = f.read()

setuptools.setup(
name="wolk-gateway-module",
version="1.0.7",
install_requires=["paho_mqtt==1.4.0"],
version=__version__,
install_requires=["paho_mqtt>=1.4.0"],
include_package_data=True,
license="Apache License 2.0",
author="WolkAbout",
Expand Down
2 changes: 1 addition & 1 deletion test/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Unit tests are written using [unit test](https://docs.python.org/3/library/unittest.html) framework.
Tests can be run individually by calling `python3.7 test_name.py` or all tests can be run at once by calling `python3.7 -m unittest` from this directory.
Tests can be run individually by calling `python3.7 test_name.py` or all tests can be run at once by calling `python3.7 -m unittest` from this directory.
27 changes: 0 additions & 27 deletions test/test_json_data_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import sys
import unittest
Expand Down Expand Up @@ -287,32 +286,6 @@ def test_make_actuator_status_message(self):

self.assertEqual(expected, serialized)

def test_make_multi_data_actuator_status_message(self):
"""Test serializing of tuple actuator status for device key."""
json_data_protocol = JsonDataProtocol()

device_key = "some_key"
reference = "REF"
actuator_status = ActuatorStatus(
reference, ActuatorState.READY, (15, 12)
)

expected = Message(
self.ACTUATOR_STATUS
+ self.DEVICE_PATH_PREFIX
+ device_key
+ self.CHANNEL_DELIMITER
+ self.REFERENCE_PATH_PREFIX
+ reference,
json.dumps({"status": "READY", "value": "15,12"}),
)

serialized = json_data_protocol.make_actuator_status_message(
device_key, actuator_status
)

self.assertEqual(expected, serialized)

def test_make_configuration_message(self):
"""Test serializing of configuration message for device key."""
json_data_protocol = JsonDataProtocol()
Expand Down
1 change: 0 additions & 1 deletion test/test_json_firmware_update_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import sys
import unittest
Expand Down
Loading

0 comments on commit 15cd0d7

Please sign in to comment.