Apply linting to the Raspberry Pi

This commit is contained in:
n07070
2026-06-04 00:59:17 +02:00
parent 6d9db2d2aa
commit adcc744e7a
2 changed files with 47 additions and 27 deletions

View File

@@ -100,11 +100,7 @@ print_queue = PrintQueue(app)
rpi = Raspberry( rpi = Raspberry(
print_queue, print_queue,
app, app,
socketio, configuration_file
configuration_file["rpi"]["button_gpio_port_number"],
configuration_file["rpi"]["indicator_gpio_port_number"],
configuration_file["rpi"]["flash_gpio_port_number"],
configuration_file["rpi"]["flash"],
) )
RASPBERRY_PI_CONNECTED = rpi.is_raspberry_pi() RASPBERRY_PI_CONNECTED = rpi.is_raspberry_pi()

View File

@@ -10,7 +10,6 @@ import io # To check if we are on a Raspberry Pi
import subprocess import subprocess
import os import os
from time import sleep, gmtime, strftime from time import sleep, gmtime, strftime
from flask_socketio import SocketIO
from gpiozero import Button, LED, DigitalOutputDevice from gpiozero import Button, LED, DigitalOutputDevice
from PIL import Image from PIL import Image
from task import TextTask, ImageTask, CutTask from task import TextTask, ImageTask, CutTask
@@ -24,29 +23,22 @@ class Raspberry:
- Activating a flash ( or light ) - Activating a flash ( or light )
- Flash an indicator light - Flash an indicator light
# pylint: disable=too-many-instance-attributes
# dede
""" """
def __init__( def __init__(
self, self,
print_queue, print_queue,
app, app,
socketio, configuration_file
button_gpio_port_number,
indicator_gpio_port_number,
flash_gpio_port_number,
is_flash_present,
): ):
self.print_queue = print_queue self.print_queue = print_queue
self.socketio = socketio
self.app = app self.app = app
self.configuration_file = configuration_file
self.flash_gpio = flash_gpio_port_number
self.is_flash_present = is_flash_present
self.button_gpio = button_gpio_port_number
self.led_gpio = indicator_gpio_port_number
self.image_path = self.app.config["UPLOAD_FOLDER"] + "/image.jpg" self.image_path = self.app.config["UPLOAD_FOLDER"] + "/image.jpg"
def is_raspberry_pi(self, raise_on_errors=False): def is_raspberry_pi(self):
""" """
Checking if we are on a Raspberry Pi by checking Checking if we are on a Raspberry Pi by checking
information on the /proc/cpuinfo file information on the /proc/cpuinfo file
@@ -73,9 +65,10 @@ class Raspberry:
return False return False
if not found: if not found:
self.app.logger.error( self.app.logger.warning(
"Couldn't get sufficient hardware information from /proc/cpuinfo, Unable to determine if we are on a Raspberry Pi." "Couldn't get sufficient hardware information from /proc/cpuinfo"
) )
self.app.logger.error("Unable to determine if we are on a Raspberry Pi.")
return False return False
except IOError: except IOError:
self.app.logger.error("Unable to open `/proc/cpuinfo`.") self.app.logger.error("Unable to open `/proc/cpuinfo`.")
@@ -84,28 +77,38 @@ class Raspberry:
self.app.logger.debug("It seems we are on a Raspberry Pi") self.app.logger.debug("It seems we are on a Raspberry Pi")
try: try:
self.initialise_gpio() self._initialise_gpio()
except Exception as e: except Exception as e:
self.app.logger.debug("Could not init GPIO : " + str(e)) self.app.logger.debug("Could not init GPIO : " + str(e))
raise e raise e
return True return True
def initialise_gpio(self): def _initialise_gpio(self):
"""
Set GPIO ports from configuration and activate them
to show the user it's working.
"""
self.app.logger.debug("Initializing GPIO") self.app.logger.debug("Initializing GPIO")
self.led = LED(self.led_gpio) self.led = LED(self.configuration_file["rpi"]["indicator_gpio_port_number"])
self.app.logger.debug("Activated indicator LED") self.app.logger.debug("Activated indicator LED")
self.indicator_countdown(iters=3) self.indicator_countdown(iters=3)
self.button = Button(self.button_gpio, pull_up=True, bounce_time=0.1) self.button = Button(
self.configuration_file["rpi"]["button_gpio_port_number"],
pull_up=True, bounce_time=0.1
)
self.button.when_pressed = self.on_button_pressed self.button.when_pressed = self.on_button_pressed
self.app.logger.debug("Activated button") self.app.logger.debug("Activated button")
# The "flash" is a relay-controlled device ( light bulb for example ) # The "flash" is a relay-controlled device ( light bulb for example )
self.flash = DigitalOutputDevice(self.flash_gpio) self.flash = DigitalOutputDevice(self.configuration_file["rpi"]["flash_gpio_port_number"])
self.flash_toggle() self.flash_toggle()
self.app.logger.debug("Activated flash") self.app.logger.debug("Activated flash")
def indicator_countdown(self, iters=10, multi=10): def indicator_countdown(self, iters=10, multi=10):
"""
Activates the LED faster and faster to show a countdown
"""
for i in range(iters, 0, -1): for i in range(iters, 0, -1):
self.led.on() self.led.on()
sleep(i / multi) sleep(i / multi)
@@ -113,7 +116,10 @@ class Raspberry:
sleep(i / multi) sleep(i / multi)
def indicator_led(self, timing=0.2, l=5): def indicator_led(self, timing=0.2, l=5):
for i in range(l): """
Turns on the indicator LED for a certain period of time
"""
for _ in range(l):
self.app.logger.debug("LED turned on") self.app.logger.debug("LED turned on")
self.led.on() self.led.on()
sleep(timing) sleep(timing)
@@ -122,6 +128,9 @@ class Raspberry:
sleep(timing) sleep(timing)
def flash_toggle(self): def flash_toggle(self):
"""
Flashes the flash
"""
self.app.logger.debug("Flash turned on") self.app.logger.debug("Flash turned on")
self.flash.on() self.flash.on()
sleep(0.3) sleep(0.3)
@@ -129,6 +138,9 @@ class Raspberry:
self.app.logger.debug("Flash turned off") self.app.logger.debug("Flash turned off")
def take_picture(self): def take_picture(self):
"""
Takes a picture via the USB webcam
"""
# Validate if the image path is valid # Validate if the image path is valid
if not os.path.isdir(os.path.dirname(self.image_path)): if not os.path.isdir(os.path.dirname(self.image_path)):
self.app.logger.error( self.app.logger.error(
@@ -174,6 +186,9 @@ class Raspberry:
position="bottom_right", position="bottom_right",
margin=10, margin=10,
): ):
"""
Takes an image and overlays it with a another picture.
"""
try: try:
image = Image.open(image_path).convert("RGBA") image = Image.open(image_path).convert("RGBA")
logo = Image.open(logo_path).convert("RGBA") logo = Image.open(logo_path).convert("RGBA")
@@ -200,7 +215,9 @@ class Raspberry:
y = image.height - logo.height - margin y = image.height - logo.height - margin
else: else:
raise ValueError( raise ValueError(
"Invalid position. Choose from 'bottom_right', 'top_left', 'top_right', or 'bottom_left'." "Invalid position." +
"Choose from 'bottom_right', 'top_left', " +
" 'top_right', or 'bottom_left'."
) )
# Composite the logo onto the image # Composite the logo onto the image
@@ -218,6 +235,9 @@ class Raspberry:
return True return True
def crop_to_square(self, image_path, output_path=None): def crop_to_square(self, image_path, output_path=None):
"""
Crop an image so that it becomes a square
"""
try: try:
image = Image.open(image_path) image = Image.open(image_path)
width, height = image.size width, height = image.size
@@ -245,6 +265,10 @@ class Raspberry:
return True return True
def on_button_pressed(self): def on_button_pressed(self):
"""
When a button press is detected, a picture is taken from the webcam
and added to the print queue.
"""
self.app.logger.debug("Button has been pressed") self.app.logger.debug("Button has been pressed")
self.led.on() self.led.on()
self.app.logger.debug("Counting down") self.app.logger.debug("Counting down")