diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index e011044ee..0872b5e77 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -844,6 +844,7 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): "offset": offsets[key] if offsets.get(key) is not None else 0, } for key, value in last.items() + if key != "time" } def get_temperature_history(self, *args, **kwargs): diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 6216fd6c6..4fa6fea15 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -657,7 +657,7 @@ class HierarchicalChainMap: return result def set_by_path(self, path, value): - current = self._chainmap.maps[0] + current = self._chainmap.maps[0] # config only key = self._path_to_key(path) # delete any subkeys @@ -666,35 +666,34 @@ class HierarchicalChainMap: if isinstance(value, dict): current.update(self._flatten(value, key)) else: - # when assigning to a existing, empty dict, delete the dict - # - # a/b = {} <- delete this - # a/b/c = "a" - up_one_path = self._path_to_key(path[:-1]) - if up_one_path in current and current[up_one_path] == {}: - del current[up_one_path] - - current[self._path_to_key(path)] = value + # make sure to clear anything below the path (e.g. switching from dict + # to something else, for whatever reason) + self._clean_upward_path(current, path) + current[key] = value def del_by_path(self, path): if not path: raise ValueError("Invalid path") - current = self._chainmap.maps[0] + current = self._chainmap.maps[0] # config only + delete_key = self._path_to_key(path) + deleted = False # delete any subkeys - delete_key = self._path_to_key(path) - if not self._del_prefix(current, delete_key): + deleted = self._del_prefix(current, delete_key) + + # delete the key itself if it's there + try: + del current[delete_key] + deleted = True + except KeyError: + pass + + if not deleted: raise KeyError("Could not find entry for " + str(path)) - # create a placeholder object above if needed - # - # a/b = {} - # a/b/c = "a" # deleted - up_one_path = self._path_to_key(path[:-1]) - up_one_path_prefix = up_one_path + _CHAINMAP_SEP - if not any(map(lambda k: k.startswith(up_one_path_prefix), current.keys())): - current[up_one_path] = {} + # clean anything that's now empty and above our path + self._clean_upward_path(current, path) def _del_prefix(self, current, key): prefix = key + _CHAINMAP_SEP @@ -705,6 +704,26 @@ class HierarchicalChainMap: return len(to_delete) > 0 + def _clean_upward_path(self, current, path): + working_path = path + while len(working_path): + working_path = working_path[:-1] + if not working_path: + break + + key = self._path_to_key(working_path) + prefix = key + _CHAINMAP_SEP + if any(map(lambda k: k.startswith(prefix), current)): + # there's at least one subkey here, we're done + break + + # delete the key itself if it's there + try: + del current[key] + except KeyError: + # key itself wasn't in there + pass + def with_config_defaults(self, config=None, defaults=None): """ Builds a new map with the following layers: provided config + any intermediary diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index ca742ad45..11363ed36 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -310,13 +310,13 @@ class PositionRecord: return True def __init__(self, *args, **kwargs): - attrs = self._standard_attrs | {key for key in kwargs if self.valid_e(key)} + attrs = self._attrs(*kwargs.keys()) for attr in attrs: setattr(self, attr, kwargs.get(attr)) def copy_from(self, other): # make sure all standard attrs and attrs from other are set - attrs = self._standard_attrs | {key for key in dir(other) if self.valid_e(key)} + attrs = self._attrs(*dir(other)) for attr in attrs: setattr(self, attr, getattr(other, attr)) @@ -326,9 +326,16 @@ class PositionRecord: delattr(self, attr) def as_dict(self): - attrs = self._standard_attrs | {key for key in dir(self) if self.valid_e(key)} + attrs = self._attrs(*dir(self)) return {attr: getattr(self, attr) for attr in attrs} + def _attrs(self, *args): + return self._standard_attrs | {key for key in args if self.valid_e(key)} + + def reset(self): + for attr in self._attrs(*dir(self)): + setattr(self, attr, None) + class TemperatureRecord: RESERVED_IDENTIFIER_REGEX = re.compile("[0-9]+|[bc]") @@ -1619,6 +1626,7 @@ class MachineCom: timeout ) ) + self._record_cancel_data = False self._cancel_preparation_done() def _cancel_preparation_done(self, check_timer=True, user=None): @@ -1705,6 +1713,7 @@ class MachineCom: # we don't call on_print_job_cancelled on our callback here # because we do this only after our M114 has been answered # by the firmware + self.cancel_position.reset() self._record_cancel_data = True with self._cancel_mutex: @@ -1778,6 +1787,7 @@ class MachineCom: timeout ) ) + self._record_pause_data = False self._pause_preparation_done() def _pause_preparation_done(self, check_timer=True, suppress_script=False, user=None): @@ -1899,6 +1909,7 @@ class MachineCom: # we don't call on_print_job_paused on our callback here # because we do this only after our M114 has been answered # by the firmware + self.pause_position.reset() self._record_pause_data = True with self._pause_mutex: diff --git a/tests/settings/_files/overlay.yaml b/tests/settings/_files/overlay.yaml index 3674054c0..9d0065da6 100644 --- a/tests/settings/_files/overlay.yaml +++ b/tests/settings/_files/overlay.yaml @@ -4,6 +4,7 @@ devel: sendWait: true waitInterval: 1.0 repetierStyleResends: false + capabilities: {} test: b: b1: 1 diff --git a/tests/settings/test_settings.py b/tests/settings/test_settings.py index becda9bd7..d624b3ed1 100644 --- a/tests/settings/test_settings.py +++ b/tests/settings/test_settings.py @@ -411,6 +411,18 @@ class SettingsTest(unittest.TestCase): self.assertNotIn("server", settings._config) self.assertEqual(5000, settings.get(["server", "port"])) + def test_set_default_subtree(self): + with self.settings() as settings: + default = {"host": "0.0.0.0", "port": 5000} + self.assertEqual( + {"host": "0.0.0.0", "port": 8080}, settings.get(["server"], merged=True) + ) + + settings.set(["server"], default) + + self.assertNotIn("server", settings._config) + self.assertEqual(default, settings.get(["server"], merged=True)) + def test_set_none(self): with self.settings() as settings: self.assertTrue("port" in settings._config["server"]) @@ -702,10 +714,32 @@ class ChainmapTest(unittest.TestCase): self.chainmap.del_by_path( ["devel", "virtualPrinter", "capabilities", "autoreport_temp"] ) + + # make sure we only see the empty default now self.assertEqual( {}, self.chainmap.get_by_path(["devel", "virtualPrinter", "capabilities"]) ) + # make sure the whole (empty) tree is gone from top layer + path = ["devel", "virtualPrinter", "capabilities", "autoreport_temp"] + while len(path): + self.assertFalse(_key(*path) in self.chainmap._chainmap.maps[0]) + path = path[:-1] + + def test_del_by_path_with_subtree(self): + self.chainmap.del_by_path(["devel", "virtualPrinter", "capabilities"]) + + # make sure we only see the empty default now + self.assertEqual( + {}, self.chainmap.get_by_path(["devel", "virtualPrinter", "capabilities"]) + ) + + # make sure the whole (empty) tree is gone from top layer + path = ["devel", "virtualPrinter", "capabilities", "autoreport_temp"] + while len(path): + self.assertFalse(_key(*path) in self.chainmap._chainmap.maps[0]) + path = path[:-1] + @ddt.data( ( {"a": 1},