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 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/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/addon/endpoints.py b/modules/addon/endpoints.py index 78fda0a..10049fe 100644 --- a/modules/addon/endpoints.py +++ b/modules/addon/endpoints.py @@ -134,7 +134,7 @@ def plugins(): :return: """ response = requests.get("https://raw.githubusercontent.com/jpgimenez/craftbeerpi-plugins/master/plugins.yaml") - cbpi.cache["plugins"] = merge(yaml.load(response.text), cbpi.cache["plugins"]) + 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/base_plugins/gpio_actor/__init__.py b/modules/base_plugins/gpio_actor/__init__.py index cedf43f..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): @@ -116,12 +145,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/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') 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 f28fffb..fba8235 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,14 +1,26 @@ -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 +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.13.1 +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 +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