From 849cea15c139f072ac3145736cfc002b13d28aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 15 Mar 2021 17:35:34 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9E=95=20Use=20immutabledict=20instead?= =?UTF-8?q?=20of=20frozendict=20on=20Py3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit frozendict appears to be unmaintained and is incompatible to Python 3.10+ As discussed in #4043 --- setup.py | 8 ++++++-- src/octoprint/printer/standard.py | 15 ++++++++++----- src/octoprint/util/__init__.py | 23 ++++++++++++++++------- src/octoprint/util/json/__init__.py | 8 ++++++-- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/setup.py b/setup.py index db02066ba..fe0010fb1 100644 --- a/setup.py +++ b/setup.py @@ -58,7 +58,6 @@ INSTALL_REQUIRES = [ "websocket-client>=0.57,<1", "wrapt>=1.12.1,<2", "emoji>=0.5.4,<1", - "frozendict>=1.2,<2", "sentry-sdk>=0.15.1,<1", "filetype>=1.0.7,<2", "zipstream-new>=1.1.8,<1.2", @@ -76,10 +75,15 @@ INSTALL_REQUIRES_PYTHON2 = [ "chainmap>=1.0.3,<2", "typing>=3.7.4.1,<4", "enum34>=1.1.10,<1.2", + "frozendict>=1.2,<2", ] # Python 3 specific requirements -INSTALL_REQUIRES_PYTHON3 = ["feedparser>=6.0.2,<7", "zeroconf>=0.24,<0.25"] +INSTALL_REQUIRES_PYTHON3 = [ + "feedparser>=6.0.2,<7", + "zeroconf>=0.24,<0.25", + "immutabledict>=1.2.0,<2", +] # OSX specific requirements INSTALL_REQUIRES_OSX = [ diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index e4cbdfc74..5316b8d1e 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -15,7 +15,12 @@ import os import threading import time -from frozendict import frozendict +try: + from immutabledict import immutabledict +except ImportError: + # Python 2 + from frozendict import frozendict as immutabledict + from past.builtins import basestring, long import octoprint.util.json @@ -51,7 +56,7 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): self._logger_job = logging.getLogger("{}.job".format(__name__)) self._dict = ( - frozendict + immutabledict if settings().getBoolean(["devel", "useFrozenDictForPrinterState"]) else dict ) @@ -194,7 +199,7 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): @property def firmware_info(self): - return frozendict(self._firmware_info) if self._firmware_info else None + return immutabledict(self._firmware_info) if self._firmware_info else None # ~~ handling of PrinterCallbacks @@ -820,11 +825,11 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): return self._comm.getErrorString() def get_current_data(self, *args, **kwargs): - return util.thaw_frozendict(self._stateMonitor.get_current_data()) + return util.thaw_immutabledict(self._stateMonitor.get_current_data()) def get_current_job(self, *args, **kwargs): currentData = self._stateMonitor.get_current_data() - return util.thaw_frozendict(currentData["job"]) + return util.thaw_immutabledict(currentData["job"]) def get_current_temperatures(self, *args, **kwargs): if self._comm is not None: diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 33dc92862..25add714f 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -27,13 +27,17 @@ from functools import wraps from typing import Union try: - # Python 3.4+ from collections.abc import Iterable, MutableMapping, Set except ImportError: # Python 2.7 from collections import Iterable, MutableMapping, Set -import frozendict +try: + from immutabledict import immutabledict +except ImportError: + # Python 2 + from frozendict import frozendict as immutabledict + import past.builtins try: @@ -1367,20 +1371,25 @@ except ImportError: 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") +def thaw_immutabledict(obj): + if not isinstance(obj, (dict, immutabledict)): + raise ValueError("obj must be a dict or immutabledict instance") # only true love can thaw a frozen dict letitgo = {} for key, value in obj.items(): - if isinstance(value, (dict, frozendict.frozendict)): - letitgo[key] = thaw_frozendict(value) + if isinstance(value, (dict, immutabledict)): + letitgo[key] = thaw_immutabledict(value) else: letitgo[key] = copy.deepcopy(value) return letitgo +thaw_frozendict = deprecated( + "thaw_frozendict has been renamed to thaw_immutabledict", since="1.6.0" +)(thaw_immutabledict) + + def utmify(link, source=None, medium=None, name=None, term=None, content=None): if source is None: return link diff --git a/src/octoprint/util/json/__init__.py b/src/octoprint/util/json/__init__.py index 8b572d719..41a8f30c1 100644 --- a/src/octoprint/util/json/__init__.py +++ b/src/octoprint/util/json/__init__.py @@ -8,7 +8,11 @@ __copyright__ = "Copyright (C) 2018 The OctoPrint Project - Released under terms import collections import json -import frozendict +try: + from immutabledict import immutabledict +except ImportError: + # Python 2 + from frozendict import frozendict as immutabledict from octoprint.util import to_unicode @@ -45,5 +49,5 @@ class JsonEncoding(object): raise TypeError -JsonEncoding.add_encoder(frozendict.frozendict, lambda obj: dict(obj)) +JsonEncoding.add_encoder(immutabledict, lambda obj: dict(obj)) JsonEncoding.add_encoder(bytes, lambda obj: to_unicode(obj)) From aa53344304f69476f0433978ec85ae07efab88c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 15 Mar 2021 17:42:34 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=91=B7=20Add=203.9=20to=20python=20te?= =?UTF-8?q?st=20matrix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 4 ++-- .github/workflows/test_install.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ada193c26..712d1d490 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -50,7 +50,7 @@ jobs: name: ๐Ÿงช Unit tests strategy: matrix: - python: ["2.7", "3.7", "3.8"] + python: ["2.7", "3.7", "3.8", "3.9"] runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -70,7 +70,7 @@ jobs: needs: build strategy: matrix: - python: ["2.7", "3.7", "3.8"] + python: ["2.7", "3.7", "3.8", "3.9"] installable: ["wheel", "sdist"] runs-on: ubuntu-latest steps: diff --git a/.github/workflows/test_install.yml b/.github/workflows/test_install.yml index 578737033..ba2dc5875 100644 --- a/.github/workflows/test_install.yml +++ b/.github/workflows/test_install.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: os: ["ubuntu-latest"] - python: ["2.7", "3.7", "3.8"] + python: ["2.7", "3.7", "3.8", "3.9"] branch: ["master"] runs-on: ${{ matrix.os }} steps: @@ -49,7 +49,7 @@ jobs: strategy: matrix: os: ["ubuntu-latest"] - python: ["2.7", "3.7", "3.8"] + python: ["2.7", "3.7", "3.8", "3.9"] runs-on: ${{ matrix.os }} steps: - name: ๐Ÿ‘€ Determine latest release From 3feb8deb8cfb8fd6cf4d3a6fc0c65b3710e182a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 15 Mar 2021 17:55:25 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=92=9A=20Fix=20unit=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some day I'll learn to search in src AND tests... --- tests/util/test_misc.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tests/util/test_misc.py b/tests/util/test_misc.py index 140ab00c3..64de26908 100644 --- a/tests/util/test_misc.py +++ b/tests/util/test_misc.py @@ -8,7 +8,12 @@ __copyright__ = "Copyright (C) 2017 The OctoPrint Project - Released under terms import unittest import ddt -from frozendict import frozendict + +try: + from immutabledict import immutabledict +except ImportError: + # Python 2 + from frozendict import frozendict as immutabledict import octoprint.util @@ -66,31 +71,35 @@ class MiscTestCase(unittest.TestCase): self.assertEqual(actual, expected) @ddt.data( - (frozendict(a=1, b=2, c=3), {"a": 1, "b": 2, "c": 3}), + (immutabledict(a=1, b=2, c=3), {"a": 1, "b": 2, "c": 3}), ( - frozendict(a=1, b=2, c=frozendict(c1=1, c2=2)), + immutabledict(a=1, b=2, c=immutabledict(c1=1, c2=2)), {"a": 1, "b": 2, "c": {"c1": 1, "c2": 2}}, ), ({"a": 1, "b": 2, "c": 3}, {"a": 1, "b": 2, "c": 3}), ( - {"a": 1, "b": 2, "c": frozendict(c1=1, c2=2)}, + {"a": 1, "b": 2, "c": immutabledict(c1=1, c2=2)}, {"a": 1, "b": 2, "c": {"c1": 1, "c2": 2}}, ), ( - {"a": 1, "b": 2, "c": {"c1": 1, "c2": 2, "c3": frozendict(c11=11, c12=12)}}, + { + "a": 1, + "b": 2, + "c": {"c1": 1, "c2": 2, "c3": immutabledict(c11=11, c12=12)}, + }, {"a": 1, "b": 2, "c": {"c1": 1, "c2": 2, "c3": {"c11": 11, "c12": 12}}}, ), ) @ddt.unpack - def test_unfreeze_frozendict(self, input, expected): - result = octoprint.util.thaw_frozendict(input) + def test_unfreeze_immutabledict(self, input, expected): + result = octoprint.util.thaw_immutabledict(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): + def test_unfreeze_immutabledict_invalid(self, input): try: - octoprint.util.thaw_frozendict(input) + octoprint.util.thaw_immutabledict(input) self.fail("expected ValueError") except ValueError: # expected