Skip to content

Commit

Permalink
Linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ondratu committed Oct 15, 2024
1 parent 8cb18d0 commit 16f32de
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 57 deletions.
57 changes: 30 additions & 27 deletions prusaerrors/connect/codes.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -61,10 +65,9 @@ def decor(cls):
data = yaml.safe_load(src_file)
assert "Errors" in data

code_re = re.compile(
r"^(?P<printer>([0-9][0-9]|XX))"
r"(?P<category>[0-9])"
r"(?P<error>[0-9][0-9])$")
code_re = re.compile(r"^(?P<printer>([0-9][0-9]|XX))"
r"(?P<category>[0-9])"
r"(?P<error>[0-9][0-9])$")
for entry in data["Errors"]:
code_parts = code_re.match(entry["code"]).groupdict()
category = Category(int(code_parts["category"]))
Expand All @@ -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

Expand Down
89 changes: 59 additions & 30 deletions prusaerrors/shared/codes.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# 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
"""

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


Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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:
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -393,15 +418,19 @@ def decor(cls):
data = yaml.safe_load(src_file)
assert "Errors" in data

code_re = re.compile(r"^(?P<printer>[0-9][0-9])(?P<category>[0-9])(?P<error>[0-9][0-9])$")
code_re = re.compile(
r"^(?P<printer>[0-9][0-9])(?P<category>[0-9])(?P<error>[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

0 comments on commit 16f32de

Please sign in to comment.