From 33305839032579cd54c57496c0ab9777e6ad2439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Gim=C3=A9nez?= Date: Mon, 3 Aug 2020 19:32:43 -0300 Subject: [PATCH 1/6] migrate to Flask 1.1 and Python 3.8 --- Dockerfile | 2 +- modules/base_plugins/gpio_actor/__init__.py | 6 ++-- modules/core/core.py | 4 +-- modules/notification/__init__.py | 2 +- requirements.txt | 37 +++++++++++++-------- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/Dockerfile b/Dockerfile index 455b74c..dd18886 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Dockerfile for development on a pc/mac -FROM python:3.5 +FROM python:3.8 EXPOSE 5000 diff --git a/modules/base_plugins/gpio_actor/__init__.py b/modules/base_plugins/gpio_actor/__init__.py index cedf43f..e7726b4 100644 --- a/modules/base_plugins/gpio_actor/__init__.py +++ b/modules/base_plugins/gpio_actor/__init__.py @@ -116,12 +116,12 @@ class DummyPWM(ActorBase): :return: ''' self.power = int(power) if power is not None else 100 - print "DummyPWM ON %s" % self.power + print("DummyPWM ON %s" % self.power) def off(self): self.power = 100 - print "OFF" + print("OFF") def set_power(self, power): self.power = int(power) - print "DummyPWM POWER %s" % self.power + print("DummyPWM POWER %s" % self.power) diff --git a/modules/core/core.py b/modules/core/core.py index 66fb0e0..479c61f 100644 --- a/modules/core/core.py +++ b/modules/core/core.py @@ -378,12 +378,12 @@ class CraftBeerPi(ActorAPI, SensorAPI): # Event Bus - def event(self, name, async=False): + def event(self, name, use_async=False): def real_decorator(function): if self.eventbus.get(name) is None: self.eventbus[name] = [] - self.eventbus[name].append({"function": function, "async": async}) + self.eventbus[name].append({"function": function, "async": use_async}) def wrapper(*args, **kwargs): return function(*args, **kwargs) return wrapper diff --git a/modules/notification/__init__.py b/modules/notification/__init__.py index 2f440cb..97e06ec 100644 --- a/modules/notification/__init__.py +++ b/modules/notification/__init__.py @@ -24,7 +24,7 @@ class NotificationView(FlaskView): cbpi.cache["messages"].pop(idx) return ('', 204) -@cbpi.event("MESSAGE", async=True) +@cbpi.event("MESSAGE", use_async=True) def messageEvent(message, **kwargs): """ React on message event. add the message to the cache and push the message to the clients diff --git a/requirements.txt b/requirements.txt index f28fffb..91b52cf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,14 +1,23 @@ -Flask==1.0 -Flask-SocketIO==2.6.2 -eventlet==0.19.0 -greenlet==0.4.10 -python-dateutil==2.5.3 -python-engineio==3.8.2.post1 -python-mimeparse==1.5.2 -python-socketio==1.4.4 -PyYAML==4.2b1 -requests==2.20.0 -Werkzeug==0.15.3 -httplib2==0.18.0 -flask-classy==0.6.10 -GitPython +attrs==19.3.0 +certifi==2020.6.20 +chardet==3.0.4 +click==7.1.2 +coverage==5.2.1 +Flask==1.1.2 +Flask-Classy==0.6.10 +Flask-SocketIO==2.6.2 +gitdb==4.0.5 +GitPython==3.1.7 +greenlet==0.4.16 +idna==2.10 +itsdangerous==1.1.0 +Jinja2==2.11.2 +MarkupSafe==1.1.1 +python-engineio==3.8.2.post1 +python-socketio==1.4.4 +PyYAML==5.3.1 +requests==2.24.0 +six==1.15.0 +smmap==3.0.4 +urllib3==1.25.10 +Werkzeug==1.0.1 From b5eb6999f7c7d08eb2fcc825ea13fe5305e906f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Gim=C3=A9nez?= Date: Mon, 3 Aug 2020 22:09:43 -0300 Subject: [PATCH 2/6] lint --- .pre-commit-config.yaml | 16 +++++ modules/base_plugins/gpio_actor/__init__.py | 73 ++++++++++++++------- requirements-dev.txt | 3 + requirements.txt | 20 +++--- 4 files changed, 81 insertions(+), 31 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 requirements-dev.txt diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..67017dc --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,16 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.4.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + - repo: https://github.com/pycqa/pylint + rev: pylint-2.5.3 + hooks: + - id: pylint + stages: [commit] + additional_dependencies: [pylint-flask] diff --git a/modules/base_plugins/gpio_actor/__init__.py b/modules/base_plugins/gpio_actor/__init__.py index e7726b4..efeb0cc 100644 --- a/modules/base_plugins/gpio_actor/__init__.py +++ b/modules/base_plugins/gpio_actor/__init__.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +""" base gpio actors """ import time from modules import cbpi @@ -6,19 +7,25 @@ from modules.core.hardware import ActorBase, SensorPassive, SensorActive from modules.core.props import Property try: - import RPi.GPIO as GPIO + import RPi.GPIO as GPIO # pylint: disable=import-error GPIO.setmode(GPIO.BCM) -except Exception as e: - print(e) - pass - +except ModuleNotFoundError as exp: + print(exp) @cbpi.actor class GPIOSimple(ActorBase): - - gpio = Property.Select("GPIO", options=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27], description="GPIO to which the actor is connected") + """ + Simple GPIO Actor + """ + gpio = Property.Select("GPIO", + options=[ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + 26, 27 + ], + description="GPIO to which the actor is connected") def init(self): GPIO.setup(int(self.gpio), GPIO.OUT) @@ -32,20 +39,28 @@ class GPIOSimple(ActorBase): print("GPIO OFF") GPIO.output(int(self.gpio), 0) + @cbpi.actor class GPIOPWM(ActorBase): - - gpio = Property.Select("GPIO", options=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27], description="GPIO to which the actor is connected") + """ + GPIO Actor with PWM support + """ + gpio = Property.Select("GPIO", + options=[ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + 26, 27 + ], + description="GPIO to which the actor is connected") frequency = Property.Number("Frequency (Hz)", configurable=True) - p = None + gpio_inst = None power = 100 # duty cycle def init(self): GPIO.setup(int(self.gpio), GPIO.OUT) GPIO.output(int(self.gpio), 0) - def on(self, power=None): if power is not None: self.power = int(power) @@ -53,29 +68,37 @@ class GPIOPWM(ActorBase): if self.frequency is None: self.frequency = 0.5 # 2 sec - self.p = GPIO.PWM(int(self.gpio), float(self.frequency)) - self.p.start(int(self.power)) + self.gpio_inst = GPIO.PWM(int(self.gpio), float(self.frequency)) + self.gpio_inst.start(int(self.power)) def set_power(self, power): ''' Optional: Set the power of your actor :param power: int value between 0 - 100 - :return: + :return: ''' - if self.p: + if self.gpio_inst: if power is not None: self.power = int(power) - self.p.ChangeDutyCycle(self.power) + self.gpio_inst.ChangeDutyCycle(self.power) def off(self): print("GPIO OFF") - self.p.stop() + self.gpio_inst.stop() @cbpi.actor class RelayBoard(ActorBase): - - gpio = Property.Select("GPIO", options=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27], description="GPIO to which the actor is connected") + """ + Relay board Actor + """ + gpio = Property.Select("GPIO", + options=[ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + 26, 27 + ], + description="GPIO to which the actor is connected") def init(self): GPIO.setup(int(self.gpio), GPIO.OUT) @@ -89,24 +112,30 @@ class RelayBoard(ActorBase): GPIO.output(int(self.gpio), 1) + @cbpi.actor class Dummy(ActorBase): - + """ + Simple Dummy Actor + """ def on(self, power=100): ''' Code to switch on the actor :param power: int value between 0 - 100 - :return: + :return: ''' print("ON") def off(self): print("OFF") + @cbpi.actor class DummyPWM(ActorBase): - + """ + Dummy Actor with PWM support + """ power = 100 def on(self, power=100): diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..a82b166 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,3 @@ +coverage==5.2.1 +pre-commit +pylint diff --git a/requirements.txt b/requirements.txt index 91b52cf..df1e105 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,23 +1,25 @@ -attrs==19.3.0 -certifi==2020.6.20 -chardet==3.0.4 -click==7.1.2 -coverage==5.2.1 Flask==1.1.2 Flask-Classy==0.6.10 Flask-SocketIO==2.6.2 -gitdb==4.0.5 -GitPython==3.1.7 greenlet==0.4.16 + +python-engineio==3.8.2.post1 +python-socketio==1.4.4 + +attrs==19.3.0 +certifi==2020.6.20 +chardet==3.0.4 +click==7.1.2 idna==2.10 itsdangerous==1.1.0 Jinja2==2.11.2 MarkupSafe==1.1.1 -python-engineio==3.8.2.post1 -python-socketio==1.4.4 PyYAML==5.3.1 requests==2.24.0 six==1.15.0 smmap==3.0.4 urllib3==1.25.10 Werkzeug==1.0.1 + +gitdb==4.0.5 +GitPython==3.1.7 From e03ab9c1f13e8d06deb279be7d4143b7a37192ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Gim=C3=A9nez?= Date: Mon, 3 Aug 2020 22:11:39 -0300 Subject: [PATCH 3/6] ignore .vscode --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f42fbc9..6d0b415 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ modules/ui/package-lock.json .python-version upload/* *.bak +.vscode From f14b3c9cfc13d9f6a3d2ed65017d642bcb997f36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Gim=C3=A9nez?= Date: Mon, 3 Aug 2020 23:49:05 -0300 Subject: [PATCH 4/6] add eventlet dep... --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index df1e105..97d529a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ Flask==1.1.2 Flask-Classy==0.6.10 Flask-SocketIO==2.6.2 greenlet==0.4.16 +eventlet==0.26.1 python-engineio==3.8.2.post1 python-socketio==1.4.4 From 33626e221ec1acbcf21366829d5cbac82e57dd6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Gim=C3=A9nez?= Date: Mon, 3 Aug 2020 23:50:02 -0300 Subject: [PATCH 5/6] python3 support --- modules/addon/endpoints.py | 4 ++-- modules/system/endpoints.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/addon/endpoints.py b/modules/addon/endpoints.py index 44c8066..10049fe 100644 --- a/modules/addon/endpoints.py +++ b/modules/addon/endpoints.py @@ -133,8 +133,8 @@ def plugins(): Read the central plugin yaml to get a list of all official plugins :return: """ - response = requests.get("https://raw.githubusercontent.com/Manuel83/craftbeerpi-plugins/master/plugins.yaml") - cbpi.cache["plugins"] = merge(yaml.load(response.text), cbpi.cache["plugins"]) + response = requests.get("https://raw.githubusercontent.com/jpgimenez/craftbeerpi-plugins/master/plugins.yaml") + cbpi.cache["plugins"] = merge(yaml.safe_load(response.text), cbpi.cache["plugins"]) for key, value in cbpi.cache["plugins"].items(): value["installed"] = os.path.isdir("./modules/plugins/%s/" % (key)) diff --git a/modules/system/endpoints.py b/modules/system/endpoints.py index 1cca9d0..31f90ef 100755 --- a/modules/system/endpoints.py +++ b/modules/system/endpoints.py @@ -130,8 +130,8 @@ class SystemView(FlaskView): endpoints[rule.rule][m] = dict(summary="", description="", consumes=["application/json"],produces=["application/json"]) with open("config/version.yaml", 'r') as stream: + y = yaml.safe_load(stream) - y = yaml.load(stream) pprint.pprint(y) pprint.pprint(re) return Response(yaml.dump(re), mimetype='text/yaml') From 8ded4a1b1d3096bc44db771dc82dad24484ae165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Gim=C3=A9nez?= Date: Mon, 3 Aug 2020 23:53:07 -0300 Subject: [PATCH 6/6] update python-engineio --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 97d529a..fba8235 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ Flask-SocketIO==2.6.2 greenlet==0.4.16 eventlet==0.26.1 -python-engineio==3.8.2.post1 +python-engineio==3.13.1 python-socketio==1.4.4 attrs==19.3.0