diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..b95c8b5 --- /dev/null +++ b/.flake8 @@ -0,0 +1,16 @@ +[flake8] +exclude = .github,.vscode,__pycache__,dist +max-complexity = 25 +# To work with Black +# E501: line too long +# W503: Line break occurred before a binary operator +# E203: Whitespace before ':' +# D202 No blank lines allowed after function docstring +# W504 line break after binary operator +ignore = + E501, + W503, + E203, + D202, + W504 +noqa-require-code = True \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..318d468 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,39 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions + +name: Build + +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: [3.5, 3.8, 3.9] + + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install flake8 pytest + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 9b36a0b..0000000 --- a/.travis.yml +++ /dev/null @@ -1,11 +0,0 @@ -language: python - -python: - - "3.5" - - "3.6" - -install: - - "pip install ." - - "pip install -r requirements.txt" - -script: ./tests/fulltests.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1f5c709 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "python.linting.flake8Enabled": true, + "python.linting.enabled": true +} diff --git a/README.rst b/README.rst index 09dd67f..775b965 100644 --- a/README.rst +++ b/README.rst @@ -146,8 +146,8 @@ How can you help? `EFF `__, or `EPIC `__ and let me know that you did. -.. |Build Status| image:: https://travis-ci.org/nugget/python-anthemav.svg?branch=master - :target: https://travis-ci.org/nugget/python-anthemav +.. |Build Status| image:: https://github.com/nugget/python-anthemav/actions/workflows/build.yml/badge.svg?branch=master + :target: https://github.com/nugget/python-anthemav/actions/workflows/build.yml .. |GitHub release| image:: https://img.shields.io/github/release/nugget/python-anthemav.svg :target: https://github.com/nugget/python-anthemav/releases .. |PyPI| image:: https://img.shields.io/pypi/v/anthemav.svg diff --git a/anthemav/connection.py b/anthemav/connection.py index 70d341f..e0b2b35 100644 --- a/anthemav/connection.py +++ b/anthemav/connection.py @@ -7,7 +7,7 @@ try: ensure_future = asyncio.ensure_future -except: +except Exception: ensure_future = getattr(asyncio, "async") @@ -70,7 +70,7 @@ async def create( def connection_lost(): """Function callback for Protocoal class when connection is lost.""" if conn._auto_reconnect and not conn._closing: - ensure_future(conn._reconnect(), loop=conn._loop) + ensure_future(conn.reconnect(), loop=conn._loop) conn.protocol = protocol_class( connection_lost_callback=connection_lost, @@ -78,7 +78,8 @@ def connection_lost(): update_callback=update_callback, ) - await conn._reconnect() + if auto_reconnect: + await conn.reconnect() return conn @@ -100,7 +101,7 @@ def _reset_retry_interval(self): def _increase_retry_interval(self): self._retry_interval = min(300, 1.5 * self._retry_interval) - async def _reconnect(self): + async def reconnect(self): while True: try: if self._halted: diff --git a/anthemav/protocol.py b/anthemav/protocol.py index a4639a2..efdc933 100755 --- a/anthemav/protocol.py +++ b/anthemav/protocol.py @@ -437,7 +437,7 @@ def formatted_command(self, command): try: self.transport.write(command) time.sleep(0.01) - except: + except Exception: self.log.warning("No transport found, unable to send command") # @@ -644,7 +644,8 @@ def mute(self): @mute.setter def mute(self, value): self._set_boolean("Z1MUT", value) - # Query mute because the AVR doesn't always return back the state (eg: after power on without changing the volume first) + # Query mute because the AVR doesn't always return back the state + # (eg: after power on without changing the volume first) self.query("Z1MUT") # diff --git a/example.py b/example.py index c99a6f2..d31ed25 100755 --- a/example.py +++ b/example.py @@ -8,8 +8,7 @@ log = logging.getLogger(__name__) -@asyncio.coroutine -def test(): +async def test(): parser = argparse.ArgumentParser(description=test.__doc__) parser.add_argument("--host", default="127.0.0.1", help="IP or FQDN of AVR") parser.add_argument("--port", default="14999", help="Port of AVR") @@ -32,7 +31,7 @@ def log_callback(message): log.info("Connecting to Anthem AVR at %s:%i" % (host, port)) - conn = yield from anthemav.Connection.create( + conn = await anthemav.Connection.create( host=host, port=port, loop=loop, update_callback=log_callback ) @@ -40,7 +39,7 @@ def log_callback(message): conn.protocol.power = True log.info("Power state is " + str(conn.protocol.power)) - yield from asyncio.sleep(2, loop=loop) + await asyncio.sleep(2, loop=loop) log.info("Panel brightness (raw) is " + str(conn.protocol.panel_brightness)) log.info("Panel brightness (text) is " + str(conn.protocol.panel_brightness_text)) @@ -62,5 +61,5 @@ def log_callback(message): if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) loop = asyncio.get_event_loop() - asyncio.async(test()) + loop.run_until_complete(test()) loop.run_forever() diff --git a/requirements_test.txt b/requirements_test.txt new file mode 100644 index 0000000..4c1d725 --- /dev/null +++ b/requirements_test.txt @@ -0,0 +1,3 @@ +asyncio +pytest-asyncio +pytest \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..db0e022 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +""" Test anthemav module """ diff --git a/tests/fulltests.py b/tests/fulltests.py index c76d434..c144bb8 100755 --- a/tests/fulltests.py +++ b/tests/fulltests.py @@ -1,11 +1,8 @@ #!/usr/bin/env python3 import asyncio -import unittest import logging -import anthemav - async def test(): log = logging.getLogger(__name__) diff --git a/tests/test_connection.py b/tests/test_connection.py new file mode 100644 index 0000000..c55a33a --- /dev/null +++ b/tests/test_connection.py @@ -0,0 +1,12 @@ +import pytest +from anthemav import Connection + + +def test_instantiate_connection(): + Connection() + + +@pytest.mark.asyncio +async def test_create_connection_auto_reconnect_false(): + conn = await Connection().create(auto_reconnect=False) + assert conn is not None