Thaw the frozendict on printer.get_current_data
We don't want calling code to run into trouble because of that. Fixes #2506
This commit is contained in:
1 parent
70254e910c
commit
f782cfe83f
3 files changed
+39
-2
No files matched your search
@@ -550,11 +550,11 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback):
|
||||
return self._comm.getStateId(state=state)
|
||||
|
||||
def get_current_data(self, *args, **kwargs):
|
||||
return self._stateMonitor.get_current_data()
|
||||
return util.thaw_frozendict(self._stateMonitor.get_current_data())
|
||||
|
||||
def get_current_job(self, *args, **kwargs):
|
||||
currentData = self._stateMonitor.get_current_data()
|
||||
return currentData["job"]
|
||||
return util.thaw_frozendict(currentData["job"])
|
||||
|
||||
def get_current_temperatures(self, *args, **kwargs):
|
||||
if self._comm is not None:
|
||||
|
||||
@@ -21,6 +21,7 @@ from functools import wraps
|
||||
import warnings
|
||||
import contextlib
|
||||
import collections
|
||||
import frozendict
|
||||
|
||||
try:
|
||||
import queue
|
||||
@@ -970,6 +971,20 @@ except RuntimeError:
|
||||
monotonic_time = time.time
|
||||
|
||||
|
||||
def thaw_frozendict(obj):
|
||||
if not isinstance(obj, (dict, frozendict.frozendict)):
|
||||
raise ValueError("obj must be a dict or frozendict instance")
|
||||
|
||||
# only true love can thaw a frozen dict
|
||||
letitgo = dict()
|
||||
for key, value in obj.items():
|
||||
if isinstance(value, frozendict.frozendict):
|
||||
letitgo[key] = thaw_frozendict(value)
|
||||
else:
|
||||
letitgo[key] = value
|
||||
return letitgo
|
||||
|
||||
|
||||
def utmify(link, source=None, medium=None, name=None, term=None, content=None):
|
||||
if source is None:
|
||||
return link
|
||||
|
||||
@@ -7,6 +7,7 @@ __copyright__ = "Copyright (C) 2017 The OctoPrint Project - Released under terms
|
||||
|
||||
import unittest
|
||||
import ddt
|
||||
from frozendict import frozendict
|
||||
|
||||
import octoprint.util
|
||||
|
||||
@@ -45,3 +46,24 @@ class MiscTestCase(unittest.TestCase):
|
||||
def test_utmify(self, link, kwargs, expected):
|
||||
actual = octoprint.util.utmify(link, **kwargs)
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
@ddt.data(
|
||||
(frozendict(a=1, b=2, c=3), dict(a=1, b=2, c=3)),
|
||||
(frozendict(a=1, b=2, c=frozendict(c1=1, c2=2)), dict(a=1, b=2, c=dict(c1=1, c2=2))),
|
||||
(dict(a=1, b=2, c=3), dict(a=1, b=2, c=3)),
|
||||
(dict(a=1, b=2, c=frozendict(c1=1, c2=2)), dict(a=1, b=2, c=dict(c1=1, c2=2))),
|
||||
)
|
||||
@ddt.unpack
|
||||
def test_unfreeze_frozendict(self, input, expected):
|
||||
result = octoprint.util.thaw_frozendict(input)
|
||||
self.assertIsInstance(result, dict)
|
||||
self.assertDictEqual(result, expected)
|
||||
|
||||
@ddt.data(None, "invalid", 3, [1, 2], (3, 4))
|
||||
def test_unfreeze_frozendict_invalid(self, input):
|
||||
try:
|
||||
octoprint.util.thaw_frozendict(input)
|
||||
self.fail("expected ValueError")
|
||||
except ValueError:
|
||||
# expected
|
||||
pass
|
||||
Reference in new issue
Block a user