From 270a06333677a56ebb12dff563df85a267763aa6 Mon Sep 17 00:00:00 2001 From: Ondrej Tuma Date: Mon, 14 Oct 2024 12:14:17 +0200 Subject: [PATCH] Linter fixes --- prusaerrors/connect/codes.py | 57 ++++++++++++----------- prusaerrors/shared/codes.py | 89 ++++++++++++++++++++++++------------ 2 files changed, 89 insertions(+), 57 deletions(-) diff --git a/prusaerrors/connect/codes.py b/prusaerrors/connect/codes.py index d647581..33d7d73 100644 --- a/prusaerrors/connect/codes.py +++ b/prusaerrors/connect/codes.py @@ -1,7 +1,6 @@ # This file is part of the SL1 firmware # Copyright (C) 2020 Prusa Research a.s. - www.prusa3d.com # SPDX-License-Identifier: GPL-3.0-or-later - """ All printer type error/attention codes @@ -12,12 +11,12 @@ from typing import Optional import yaml +from prusaerrors.shared.codes import (Category, Code, Codes, Printer, + unique_codes) -from prusaerrors.shared.codes import unique_codes, Codes, Printer, Code, Category - - -BUDDY = ['MINI', 'MK4', 'IX', 'XL', 'MK3.5', - 'MK4S', 'MK3.9', 'MK3.9S', 'MK3.5S'] +BUDDY = [ + 'MINI', 'MK4', 'IX', 'XL', 'MK3.5', 'MK4S', 'MK3.9', 'MK3.9S', 'MK3.5S' +] class PrinterCode(Code): @@ -27,18 +26,23 @@ class PrinterCode(Code): """ # pylint: disable = too-many-arguments + # pylint: disable = too-many-positional-arguments def __init__( - self, - printer: Printer, - category: Category, - error: int, - title: str, - message: str, - approved: bool, - id_: str, + self, + printer: Printer, + category: Category, + error: int, + title: str, + message: str, + approved: bool, + id_: str, ): - super().__init__(printer=printer, category=category, error=error, - title=title, message=message, approved=approved) + super().__init__(printer=printer, + category=category, + error=error, + title=title, + message=message, + approved=approved) self.id = id_ @property @@ -61,10 +65,9 @@ def decor(cls): data = yaml.safe_load(src_file) assert "Errors" in data - code_re = re.compile( - r"^(?P([0-9][0-9]|XX))" - r"(?P[0-9])" - r"(?P[0-9][0-9])$") + code_re = re.compile(r"^(?P([0-9][0-9]|XX))" + r"(?P[0-9])" + r"(?P[0-9][0-9])$") for entry in data["Errors"]: code_parts = code_re.match(entry["code"]).groupdict() category = Category(int(code_parts["category"])) @@ -82,18 +85,18 @@ def decor(cls): for printer in printers: printer = Printer[printer.upper().replace(".", "")] - code = PrinterCode( - printer, category, error, entry["title"], - entry["text"], entry.get("approved", False), - entry["id"]) + code = PrinterCode(printer, category, error, + entry["title"], entry["text"], + entry.get("approved", + False), entry["id"]) setattr(cls, str(code), code) # code contains printer number else: printer = Printer(int(code_parts["printer"])) - code = PrinterCode(printer, category, error, entry["title"], - entry["text"], entry.get("approved", False), - entry["id"]) + code = PrinterCode(printer, category, error, + entry["title"], entry["text"], + entry.get("approved", False), entry["id"]) setattr(cls, str(code), code) return cls diff --git a/prusaerrors/shared/codes.py b/prusaerrors/shared/codes.py index af162a7..4f4e43c 100644 --- a/prusaerrors/shared/codes.py +++ b/prusaerrors/shared/codes.py @@ -1,7 +1,6 @@ # This file is part of the SL1 firmware # Copyright (C) 2020 Prusa Research a.s. - www.prusa3d.com # SPDX-License-Identifier: GPL-3.0-or-later - """ Base classes for SL1 errors and warnings """ @@ -9,9 +8,10 @@ import functools import json import re -from enum import unique, IntEnum +from enum import IntEnum, unique from pathlib import Path -from typing import List, TextIO, Dict +from typing import Dict, List, TextIO, Optional + import yaml @@ -43,7 +43,8 @@ class Category(IntEnum): """ Prusa error category codes - This mapping is taken from general Prusa guidelines on errors, do not modify. + This mapping is taken from general Prusa guidelines on errors, do not + modify. """ MECHANICAL = 1 # Mechanical failures, engines XYZ, tower @@ -64,16 +65,15 @@ class Code: """ # pylint: disable = too-many-arguments - def __init__( - self, - printer: Printer, - category: Category, - error: int, - title: str, - message: str, - approved: bool, - action: List[str] = None - ): + # pylint: disable = too-many-positional-arguments + def __init__(self, + printer: Printer, + category: Category, + error: int, + title: str, + message: str, + approved: bool, + action: Optional[List[str]] = None): if printer.value < 0 or printer.value > 99: raise ValueError(f"Printer class {printer} out of range") if category.value < 0 or category.value > 9: @@ -167,8 +167,9 @@ def approved(self): """ Whenever the message text was approved for use in production system - Unapproved tests are not supposed to be translated. This is planed to raise warnings and prevent the resulting - build from being used in production. + Unapproved tests are not supposed to be translated. This is planed + to raise warnings and prevent the resulting build from being used in + production. """ return self._approved @@ -214,7 +215,10 @@ def get_codes(cls) -> Dict[str, Code]: :return: Member code dict """ - return {item: var for item, var in vars(cls).items() if isinstance(var, Code)} + return { + item: var + for item, var in vars(cls).items() if isinstance(var, Code) + } @classmethod def get(cls, code: str): @@ -227,7 +231,10 @@ def get(cls, code: str): :return: Code instance """ if not cls._code_map: - cls._code_map = {code.code: code for code in cls.get_codes().values()} + cls._code_map = { + code.code: code + for code in cls.get_codes().values() + } return cls._code_map[code] @classmethod @@ -238,7 +245,13 @@ def dump_json(cls, file: TextIO) -> None: :param file: Where to dump :return: None """ - obj = {name.lower(): {"code": code.code, "message": code.message} for name, code in cls.get_codes().items()} + obj = { + name.lower(): { + "code": code.code, + "message": code.message + } + for name, code in cls.get_codes().items() + } return json.dump(obj, file, indent=True) @classmethod @@ -286,7 +299,9 @@ def dump_qml_dictionary(cls, file: TextIO): :return: None """ file.write("import QtQuick 2.10\n") - file.write("/* Generated by sla-errors. Your edits to this file will be lost. */\n") + file.write( + "/* Generated by sla-errors. Your edits to this file will be lost. */\n" + ) file.write("pragma Singleton\n") file.write("Item {\n") file.write("\treadonly property var messages:{\n") @@ -306,7 +321,9 @@ def dump_cpp_ts(cls, file: TextIO): :param file: Where to dump :return: None """ - file.write("// Generated translation string definitions for all defined error messages\n") + file.write( + "// Generated translation string definitions for all defined error messages\n" + ) for code in cls.get_codes().values(): if code.message: file.write(f"QT_TR_NOOP({code.raw_message});\n") @@ -331,7 +348,9 @@ def dump_google_docs(cls, file: TextIO) -> None: for name, code in cls.get_codes().items(): message = code.message if code.message else "" category = f"{c2docs[code.category]}\t{code.category.value}" - file.write(f'SL1\t10\t{category}\t{code.error}\t"{name}"\t"{message}"\t{code.code}\n') + file.write( + f'SL1\t10\t{category}\t{code.error}\t"{name}"\t"{message}"\t{code.code}\n' + ) @classmethod def dump_yaml(cls, file: TextIO) -> None: @@ -344,9 +363,13 @@ def dump_yaml(cls, file: TextIO) -> None: codes = [] for name, code in cls.get_codes().items(): - codes.append( - {"code": code.raw_code, "title": name, "text": code.message, "id": name, "approved": code.approved,} - ) + codes.append({ + "code": code.raw_code, + "title": name, + "text": code.message, + "id": name, + "approved": code.approved, + }) yaml.dump({"Errors": codes}, file, sort_keys=False) @@ -361,7 +384,8 @@ def unique_codes(cls): used = set() for name, code in cls.get_codes().items(): if code.code in used: - raise ValueError(f"Code {name} with value {code.code} is duplicate!") + raise ValueError( + f"Code {name} with value {code.code} is duplicate!") used.add(code.code) return cls @@ -377,7 +401,8 @@ def unique_titles(cls): used = set() for name, code in cls.get_codes().items(): if code.title in used: - raise ValueError(f"Code {name} with title {code.title} is duplicate!") + raise ValueError( + f"Code {name} with title {code.title} is duplicate!") used.add(code.title) return cls @@ -393,15 +418,19 @@ def decor(cls): data = yaml.safe_load(src_file) assert "Errors" in data - code_re = re.compile(r"^(?P[0-9][0-9])(?P[0-9])(?P[0-9][0-9])$") + code_re = re.compile( + r"^(?P[0-9][0-9])(?P[0-9])(?P[0-9][0-9])$" + ) for entry in data["Errors"]: code_parts = code_re.match(entry["code"]).groupdict() printer = Printer(int(code_parts["printer"])) category = Category(int(code_parts["category"])) error = int(code_parts["error"]) action = entry.get("action", []) - setattr(cls, entry["id"], Code(printer, category, error, entry["title"], entry["text"], - entry["approved"], action)) + setattr( + cls, entry["id"], + Code(printer, category, error, entry["title"], entry["text"], + entry["approved"], action)) return cls return decor