From 3cce49fe799bdf633aef2ab2bc5ece5dad3327ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 16 Feb 2022 10:24:07 +0100 Subject: [PATCH 1/6] =?UTF-8?q?=F0=9F=90=9B=20Fix=20plugin=20event=20ident?= =?UTF-8?q?ifier=20generation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit plugin_Example_event was wrongly turned into PLUGIN__EXAMPLE_EVENT. Fixed regex to solve this. Closes #4417 --- src/octoprint/events.py | 2 +- tests/test_events.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/test_events.py diff --git a/src/octoprint/events.py b/src/octoprint/events.py index 1928fe7d7..537f220ed 100644 --- a/src/octoprint/events.py +++ b/src/octoprint/events.py @@ -139,7 +139,7 @@ class Events: return name, event # based on https://stackoverflow.com/a/1176023 - _first_cap_re = re.compile("(.)([A-Z][a-z]+)") + _first_cap_re = re.compile("([^_])([A-Z][a-z]+)") _all_cap_re = re.compile("([a-z0-9])([A-Z])") @classmethod diff --git a/tests/test_events.py b/tests/test_events.py new file mode 100644 index 000000000..5443836b7 --- /dev/null +++ b/tests/test_events.py @@ -0,0 +1,22 @@ +__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" +__copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms of the AGPLv3 License" + +import unittest + +import ddt + +import octoprint.events + + +@ddt.ddt +class TestEvents(unittest.TestCase): + @ddt.data( + ("plugin_example_event", "PLUGIN_EXAMPLE_EVENT"), + ("plugin_Example_event", "PLUGIN_EXAMPLE_EVENT"), + ("plugin_ExAmple_Event", "PLUGIN_EX_AMPLE_EVENT"), + ("plugin_exAmple_EvEnt", "PLUGIN_EX_AMPLE_EV_ENT"), + ) + @ddt.unpack + def test_to_identifier(self, value, expected): + actual = octoprint.events.Events._to_identifier(value) + self.assertEqual(actual, expected) From 698dcc2aa156c4c92606c0d0698f47057aecb8c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 16 Feb 2022 12:03:15 +0100 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=A5=85=20Protect=20against=20None=20s?= =?UTF-8?q?et=20subtrees?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove one in our own defaults. Also add tests for flatten and unflatten. --- src/octoprint/settings.py | 4 +-- tests/test_settings.py | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 1b1b19548..87bf94524 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -416,7 +416,7 @@ default_settings = { "remoteUserHeader": "REMOTE_USER", "addRemoteUsers": False, }, - "slicing": {"enabled": True, "defaultSlicer": None, "defaultProfiles": None}, + "slicing": {"enabled": True, "defaultSlicer": None, "defaultProfiles": {}}, "events": {"enabled": True, "subscriptions": []}, "api": {"key": None, "allowCrossOrigin": False, "apps": {}}, "terminalFilters": [ @@ -566,7 +566,7 @@ class HierarchicalChainMap: subkeys = key[len(prefix) :].split(_CHAINMAP_SEP) current = result for subkey in subkeys[:-1]: - if subkey not in current: + if subkey not in current or current[subkey] is None: current[subkey] = {} current = current[subkey] current[subkeys[-1]] = value diff --git a/tests/test_settings.py b/tests/test_settings.py index 050aff109..04f994475 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -29,3 +29,55 @@ class TestHelpers(unittest.TestCase): @ddt.unpack def test_valid_boolean_trues(self, value, expected): self.assertEqual(expected, value in octoprint.settings.valid_boolean_trues) + + +def _key(*path): + return octoprint.settings._CHAINMAP_SEP.join(path) + + +@ddt.ddt +class ChainmapTest(unittest.TestCase): + @ddt.data( + ( + {"a": 1}, + {_key("a"): 1}, + ), + ({"a": {"b": "b"}}, {_key("a", "b"): "b"}), + ( + {"a": {"b": "b", "c": "c", "d": {"e": "e"}}}, + {_key("a", "b"): "b", _key("a", "c"): "c", _key("a", "d", "e"): "e"}, + ), + ) + @ddt.unpack + def test_flatten(self, value, expected): + self.assertEqual( + expected, octoprint.settings.HierarchicalChainMap._flatten(value) + ) + + @ddt.data( + ( + [ + (_key("a"), 1), + ], + {"a": 1}, + ), + ( + [ + (_key("a", "b"), "b"), + ], + {"a": {"b": "b"}}, + ), + ( + [(_key("a", "b"), "b"), (_key("a", "c"), "c"), (_key("a", "d", "e"), "e")], + {"a": {"b": "b", "c": "c", "d": {"e": "e"}}}, + ), + ( + [(_key("a"), None), (_key("a", "b"), "b")], + {"a": {"b": "b"}}, + ), + ) + @ddt.unpack + def test_unflatten(self, value, expected): + self.assertEqual( + expected, octoprint.settings.HierarchicalChainMap._unflatten(value) + ) From 937abb484077c08f229e68fc75caabe5454ed86a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 16 Feb 2022 12:24:12 +0100 Subject: [PATCH 3/6] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Change=20=5Funflatten?= =?UTF-8?q?=20to=20operate=20on=20dict?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _flatten & _unflatten should mirror each other. --- src/octoprint/settings.py | 68 +++++++++++++++++++++------------------ tests/test_settings.py | 12 +++---- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 87bf94524..6222b3753 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -555,35 +555,9 @@ class HierarchicalChainMap: """ @staticmethod - def _unflatten(kv_pairs, prefix=""): - """ - :type kv_pairs: Iterable[Tuple[str, any]] - """ - result = dict() - for key, value in kv_pairs: - if not key.startswith(prefix): - continue - subkeys = key[len(prefix) :].split(_CHAINMAP_SEP) - current = result - for subkey in subkeys[:-1]: - if subkey not in current or current[subkey] is None: - current[subkey] = {} - current = current[subkey] - current[subkeys[-1]] = value + def _flatten(d: dict, parent_key: str = "") -> dict: + """Flattens a hierarchical dictionary.""" - return result - - def __init__(self, *maps): - self._chainmap = ChainMap(*map(self._flatten, maps)) - - @staticmethod - def from_layers(*layers): - result = HierarchicalChainMap() - result._chainmap.maps = layers - return result - - @staticmethod - def _flatten(d, parent_key=""): if d is None: return {} @@ -596,8 +570,26 @@ class HierarchicalChainMap: items.append((new_key, v)) return dict(items) - def deep_dict(self): - return self._unflatten(self._chainmap.items()) + @staticmethod + def _unflatten(d: dict, prefix: str = "") -> dict: + """Unflattens a flattened dictionary.""" + + if d is None: + return {} + + result = dict() + for key, value in d.items(): + if not key.startswith(prefix): + continue + subkeys = key[len(prefix) :].split(_CHAINMAP_SEP) + current = result + for subkey in subkeys[:-1]: + if subkey not in current or current[subkey] is None: + current[subkey] = {} + current = current[subkey] + current[subkeys[-1]] = value + + return result @staticmethod def _path_to_key(path): @@ -606,6 +598,18 @@ class HierarchicalChainMap: """ return _CHAINMAP_SEP.join(path) + @staticmethod + def from_layers(*layers): + result = HierarchicalChainMap() + result._chainmap.maps = layers + return result + + def __init__(self, *maps): + self._chainmap = ChainMap(*map(self._flatten, maps)) + + def deep_dict(self): + return self._unflatten(self._chainmap) + def has_path(self, path, only_local=False, only_defaults=False): if only_defaults: current = self._chainmap.parents @@ -643,7 +647,7 @@ class HierarchicalChainMap: # we might be trying to grab a dict, look for children key = key + _CHAINMAP_SEP result = self._unflatten( - ((k, v) for k, v in current.items() if k.startswith(key)), prefix=key + {k: v for k, v in current.items() if k.startswith(key)}, prefix=key ) if not result: raise KeyError("Could not find entry for " + str(path)) @@ -729,7 +733,7 @@ class HierarchicalChainMap: @property def top_map(self): """This is the layer that is written to""" - return self._unflatten(self._chainmap.maps[0].items()) + return self._unflatten(self._chainmap.maps[0]) @top_map.setter def top_map(self, value): diff --git a/tests/test_settings.py b/tests/test_settings.py index 04f994475..706a6e916 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -56,23 +56,19 @@ class ChainmapTest(unittest.TestCase): @ddt.data( ( - [ - (_key("a"), 1), - ], + {_key("a"): 1}, {"a": 1}, ), ( - [ - (_key("a", "b"), "b"), - ], + {_key("a", "b"): "b"}, {"a": {"b": "b"}}, ), ( - [(_key("a", "b"), "b"), (_key("a", "c"), "c"), (_key("a", "d", "e"), "e")], + {_key("a", "b"): "b", _key("a", "c"): "c", _key("a", "d", "e"): "e"}, {"a": {"b": "b", "c": "c", "d": {"e": "e"}}}, ), ( - [(_key("a"), None), (_key("a", "b"), "b")], + {_key("a"): None, _key("a", "b"): "b"}, {"a": {"b": "b"}}, ), ) From 82338209df3d762aca190d2603d154735f8d38ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 16 Feb 2022 15:30:11 +0100 Subject: [PATCH 4/6] =?UTF-8?q?=F0=9F=90=9B=20Enable=20print=20button=20if?= =?UTF-8?q?=20ready=20OR=20paused?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #4421 --- src/octoprint/static/js/app/viewmodels/printerstate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/octoprint/static/js/app/viewmodels/printerstate.js b/src/octoprint/static/js/app/viewmodels/printerstate.js index 67c240131..45ce5d610 100644 --- a/src/octoprint/static/js/app/viewmodels/printerstate.js +++ b/src/octoprint/static/js/app/viewmodels/printerstate.js @@ -48,7 +48,7 @@ $(function () { self.enablePrint = ko.pureComputed(function () { return ( self.isOperational() && - self.isReady() && + (self.isReady() || self.isPaused()) && !self.isPrinting() && !self.isCancelling() && !self.isPausing() && From 0171f2bb7d4fb7da0e2d783c6132c0913a3cafa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 16 Feb 2022 16:08:18 +0100 Subject: [PATCH 5/6] =?UTF-8?q?=E2=9C=A8=20Expose=20unknown=20command=20tr?= =?UTF-8?q?eatment=20on=20UI=20&=20add=20auto=20detection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prusa Firmware now has sendChecksumWithUnknownCommands and unknownCommandsNeedAck as they've decided to add non-GCODE commands to their MMU command subset that otherwise possibly cause issues in the field, especially if part of a sliced file. Implements #4423 --- src/octoprint/server/api/settings.py | 4 ++++ .../static/js/app/viewmodels/settings.js | 2 ++ .../dialogs/settings/serialconnection.jinja2 | 21 +++++++++++++++++-- src/octoprint/util/comm.py | 2 ++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/octoprint/server/api/settings.py b/src/octoprint/server/api/settings.py index b81c92b3b..c48642a70 100644 --- a/src/octoprint/server/api/settings.py +++ b/src/octoprint/server/api/settings.py @@ -216,6 +216,10 @@ def getSettings(): "waitForStart": s.getBoolean(["serial", "waitForStartOnConnect"]), "alwaysSendChecksum": s.getBoolean(["serial", "alwaysSendChecksum"]), "neverSendChecksum": s.getBoolean(["serial", "neverSendChecksum"]), + "sendChecksumWithUnknownCommands": s.getBoolean( + ["serial", "sendChecksumWithUnknownCommands"] + ), + "unknownCommandsNeedAck": s.getBoolean(["serial", "unknownCommandsNeedAck"]), "sdRelativePath": s.getBoolean(["serial", "sdRelativePath"]), "sdAlwaysAvailable": s.getBoolean(["serial", "sdAlwaysAvailable"]), "sdLowerCase": s.getBoolean(["serial", "sdLowerCase"]), diff --git a/src/octoprint/static/js/app/viewmodels/settings.js b/src/octoprint/static/js/app/viewmodels/settings.js index c7df6406f..5ae24af21 100644 --- a/src/octoprint/static/js/app/viewmodels/settings.js +++ b/src/octoprint/static/js/app/viewmodels/settings.js @@ -219,6 +219,8 @@ $(function () { self.serial_triggerOkForM29 = ko.observable(undefined); self.serial_waitForStart = ko.observable(undefined); self.serial_sendChecksum = ko.observable("print"); + self.serial_sendChecksumWithUnknownCommands = ko.observable(undefined); + self.serial_unknownCommandsNeedAck = ko.observable(undefined); self.serial_sdRelativePath = ko.observable(undefined); self.serial_sdLowerCase = ko.observable(undefined); self.serial_sdAlwaysAvailable = ko.observable(undefined); diff --git a/src/octoprint/templates/dialogs/settings/serialconnection.jinja2 b/src/octoprint/templates/dialogs/settings/serialconnection.jinja2 index 3d7bd971e..8e3b50cd3 100644 --- a/src/octoprint/templates/dialogs/settings/serialconnection.jinja2 +++ b/src/octoprint/templates/dialogs/settings/serialconnection.jinja2 @@ -343,7 +343,23 @@ - +
+
+ +
+
+
+
+ +
+
+
@@ -457,7 +473,8 @@
- {{ _('Use this to specify emergency commands which should jump any queues and - if supported by the firmware - be force sent. Just the G or M code, comma separated. Defaults to M112, M108 and M410.') }}
+ {{ _('Use this to specify emergency commands which should jump any queues and - if supported by the firmware - be force sent. Just the G or M code, comma separated. Defaults to M112, M108 and M410.') }} +
diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index c197f0c1c..7feef5bb6 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -2819,6 +2819,8 @@ class MachineCom: ) self._sdLowerCase = True + self._sendChecksumWithUnknownCommands = True + self._unknownCommandsNeedAck = True self._firmware_info_received = True self._firmware_info = data From d10780df54b81b1fbc56ef231c16b5bb469ef1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 16 Feb 2022 18:50:18 +0100 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=90=9B=20Fix=20look-up=20of=20unmerge?= =?UTF-8?q?d=20individual=20keys?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/settings.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 6222b3753..0410712e9 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -635,13 +635,9 @@ class HierarchicalChainMap: # the first level where we find the value. if not merged and not only_local: for layer in current.maps: - for k in layer: - if k.startswith(key): - current = layer - break - else: - continue - break + if key in layer: + current = layer + break if key not in current: # we might be trying to grab a dict, look for children @@ -1870,7 +1866,7 @@ class Settings: for key in keys: try: value = chain.get_by_path( - parent_path + [key], only_local=not incl_defaults + parent_path + [key], only_local=not incl_defaults, merged=merged ) except KeyError: raise NoSuchSettingsPath()