Apply linting

This commit is contained in:
n07070
2026-05-21 02:57:27 +02:00
parent c50922790d
commit 2daafe28f2
8 changed files with 209 additions and 155 deletions

View File

@@ -23,7 +23,6 @@ import sys
import os # For VARS from the shell.
import pprint # To pretty print JSON
import toml # Used for the config file parsing
import threading
from flask import (
Flask,
request,
@@ -43,7 +42,6 @@ from web import Web # Wrapper for the web routes and API
from print_queue import PrintQueue
from worker import PrintWorker
# We create the main Flask object
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*")
@@ -87,7 +85,7 @@ except FileExistsError:
app.logger.debug("Directory %s already exists.", UPLOAD_FOLDER)
except PermissionError:
app.logger.error("Permission denied: Unable to create %s", UPLOAD_FOLDER)
exit(77)
sys.exit(77)
# Output the config file
if os.getenv("FLASK_DEBUG"):
@@ -137,6 +135,7 @@ limiter = Limiter(
# General routes
@app.route("/")
@limiter.limit("1/second", override_defaults=False)
def index():
@@ -152,8 +151,10 @@ def webcam():
app.logger.debug("Loading webcam interface")
return render_template("webcam.html")
# Form treatement
@app.route("/web/print/sms", methods=["POST"])
@limiter.limit("6/minute", override_defaults=False)
def web_print_sms():
@@ -164,7 +165,7 @@ def web_print_sms():
txt = request.form["txt"]
except werkzeug.exceptions.BadRequestKeyError as e:
app.logger.error("Whoops, we are missing the txt input field. : %s ", str(e))
flash("Whoops, no forms submitted or missing signature : " + str(e), 'error')
flash("Whoops, no forms submitted or missing signature : " + str(e), "error")
return redirect(url_for("index"))
try:
@@ -172,23 +173,23 @@ def web_print_sms():
sign = request.form["signature"]
except werkzeug.exceptions.BadRequestKeyError as e:
app.logger.warning(
"No signature found for this print, using default signature.", str(e)
"No signature found for this print, using default signature : %s ", str(e)
)
sign = configuration_file["defaults"]["signature"]
try:
web.print_sms(txt, sign)
except Exception as e:
except RuntimeError as e:
app.logger.error("Whoops, we could not print an SMS because : %s ", str(e))
flash("Whoops, we could not print an SMS because :" + str(e), 'error')
flash("Whoops, we could not print an SMS because :" + str(e), "error")
return redirect(url_for("index"))
# end try
flash("The SMS has been printed !", 'info')
flash("The SMS has been printed !", "info")
return redirect(url_for("index"))
@app.route("/web/print/img", methods=["POST"])
@app.route("/web/print/img", methods=["POST"])
@limiter.limit("1/second", override_defaults=False)
def web_print_img():
"""Prints an image on a printer"""
@@ -199,7 +200,7 @@ def web_print_img():
sign = request.form["signature"]
except werkzeug.exceptions.BadRequestKeyError as e:
app.logger.warning(
"No signature found for this print, using default signature.", str(e)
"No signature found for this print, using default signature : %s", str(e)
)
sign = configuration_file["defaults"]["signature"]
@@ -207,7 +208,7 @@ def web_print_img():
if "img" not in request.files:
app.logger.error("Whoops, no images submitted : %s ", str(e))
app.logger.error("Error getting the files : %s", str(e))
flash("Whoops, no images submitted : " + str(e), 'error')
flash("Whoops, no images submitted : " + str(e), "error")
return redirect(url_for("index"))
file = request.files["img"]
@@ -215,20 +216,21 @@ def web_print_img():
# empty file without a filename.
if file.filename == "":
app.logger.error("Submitted file has no filename !")
flash("Submitted file has no filename !", 'error')
flash("Submitted file has no filename !", "error")
return redirect(url_for("index"))
try:
app.logger.debug("Sending the image to the printer.")
web.print_image(file, sign)
except Exception as e:
except RuntimeError as e:
app.logger.error("The image could not be printed because : %s ", str(e))
flash("The image could not be printed because : " + str(e), 'error')
flash("The image could not be printed because : " + str(e), "error")
return redirect(url_for("index"))
flash("Picture printed !", 'info')
flash("Picture printed !", "info")
return redirect(url_for("index"))
# API routes
# The api has the following methods
# api/print/{sms,img,letter,qr,barcode}
@@ -267,11 +269,12 @@ def api_print_sms():
try:
# comment: We try to print the SMS
web.print_sms(txt, sign)
except Exception as e:
except RuntimeError as e:
return str(e), 500
# end try
return "OK", 200
@app.route("/api/print/img", methods=["POST"])
@limiter.limit("6/minute", override_defaults=False)
def api_print_image():
@@ -302,38 +305,43 @@ def api_print_image():
try:
app.logger.debug("Sending the image to the printer.")
web.print_image(file, sign)
except Exception as e:
except RuntimeError as e:
return str(e), 500
return "OK", 200
@app.route("/api/camera/picture", methods=["GET"])
def camera_picture():
"""Returns a picture taken by the camera on a raspberry pi"""
if RASPBERRY_PI_CONNECTED:
try:
return rpi.camera_picture()
except Exception as e:
except RuntimeError as e:
return jsonify({"message": "Error getting the stream : " + e}), 500
else:
return jsonify({"message": "No camera present"}), 500
@app.route('/api/queue', methods=["GET"])
@app.route("/api/queue", methods=["GET"])
def api_queue_status():
"""API endpoint for entire queue"""
return jsonify(web.get_queue_state())
@app.route('/api/worker', methods=["GET"])
@app.route("/api/worker", methods=["GET"])
def api_worker_state():
"""API endpoint to get the worker state"""
return jsonify(worker.current_state())
@app.route('/api/worker/start')
@app.route("/api/worker/start")
def api_worker_start():
worker.start_worker()
return jsonify(worker.current_state())
@app.route('/api/worker/stop')
@app.route("/api/worker/stop")
def api_worker_stop():
worker.stop_worker()
return jsonify(worker.current_state())
@@ -341,6 +349,7 @@ def api_worker_stop():
## Authentification
@app.route("/login")
@limiter.limit("1/second", override_defaults=False)
def login_page():
@@ -393,5 +402,6 @@ def camera_status():
else:
socketio.emit("camera_status", False)
if __name__ == "__main__":
app.run(debug=True, use_reloader=False, host="0.0.0.0", ssl_context="adhoc")