Skip to content

Commit 1fdadd6

Browse files
committed
✨ Support placeholders in messages
Checks can parse/set their content so that the message for a warning type can be customized a bit.
1 parent 6763368 commit 1fdadd6

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

octoprint_firmware_check/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ def _run_checks(self, check_type, *args, **kwargs):
223223
if check.url is not None:
224224
url = check.url
225225

226-
self._register_warning(warning_type, message, severity, url)
226+
self._register_warning(
227+
warning_type, message.format(**check.placeholders), severity, url
228+
)
227229

228230
# noinspection PyUnresolvedReferences
229231
self._event_bus.fire(

octoprint_firmware_check/checks/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def __init__(self):
1919
self._active = True
2020
self._triggered = False
2121
self._start_time = None
22+
self.placeholders = {}
2223

2324
def received(self, line):
2425
"""Called when receiving a new line from the printer"""

octoprint_firmware_check/checks/firmware_development.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
55
__copyright__ = "Copyright (C) 2019 The OctoPrint Project - Released under terms of the AGPLv3 License"
66

7+
import datetime
8+
import re
9+
710
from flask_babel import gettext
811

912
from . import Check, Severity
@@ -15,8 +18,9 @@ def as_dict(cls):
1518
return dict(
1619
checks=(MarlinBugfixCheck(),),
1720
message=gettext(
18-
"Your printer's firmware is a development build. It might be more unstable "
19-
"than a release version and should be updated regularly."
21+
"Your printer's firmware is a {buildtype} build of {firmware} "
22+
"(build date {builddate}). It might be more unstable "
23+
"than a release version and should be kept up-to-date."
2024
),
2125
severity=Severity.INFO,
2226
)
@@ -26,11 +30,36 @@ class MarlinBugfixCheck(Check):
2630
"""
2731
Marlin bugfix builds.
2832
29-
Identified by firmware name that contains "Marlin bugfix-"
33+
Identified by firmware name that contains "Marlin bugfix-<version> (<builddate>)"
3034
"""
3135

3236
name = "marlin_bugfix"
3337

38+
pattern = re.compile(
39+
r"marlin (?P<version>bugfix-[^-]*)\s\((?P<builddate>.*)\)",
40+
)
41+
42+
def __init__(self):
43+
super().__init__()
44+
self.placeholders = {
45+
"firmware": "Marlin",
46+
"version": "unknown",
47+
"builddate": "unknown",
48+
"buildtype": "development",
49+
}
50+
3451
def m115(self, name, data):
35-
self._triggered = name and "marlin bugfix-" in name.lower()
52+
match = self.pattern.match(name.lower())
53+
if match:
54+
self.placeholders["version"] = match.group("version")
55+
self.placeholders["builddate"] = match.group("builddate")
56+
57+
try:
58+
self.placeholders["builddate"] = datetime.datetime.strptime(
59+
match.group("builddate"), "%b %d %Y %H:%M:%S"
60+
).strftime("%Y%m%d")
61+
except Exception:
62+
pass
63+
64+
self._triggered = name and match
3665
self._active = False

0 commit comments

Comments
 (0)