Browse Source

Make cache of machines a protected field

We don't want to use it outside of the mapping. This mapping should be transparent.
We are still using it from our tests though but that's fine. Tests are allowed to touch private fields.

Contributes to issue CURA-6793.
Ghostkeeper 5 years ago
parent
commit
8179dfc412
2 changed files with 8 additions and 8 deletions
  1. 6 6
      cura/Machines/ContainerTree.py
  2. 2 2
      tests/Machines/TestContainerTree.py

+ 6 - 6
cura/Machines/ContainerTree.py

@@ -84,7 +84,7 @@ class ContainerTree:
     #   This handles the lazy loading of MachineNodes.
     class MachineNodeMap:
         def __init__(self):
-            self.machines = {}
+            self._machines = {}
 
         ##  Returns whether a printer with a certain definition ID exists. This
         #   is regardless of whether or not the printer is loaded yet.
@@ -99,12 +99,12 @@ class ContainerTree:
         #   \param definition_id The definition to look for.
         #   \return A machine node for that definition.
         def __getitem__(self, definition_id: str) -> MachineNode:
-            if definition_id not in self.machines:
+            if definition_id not in self._machines:
                 start_time = time.time()
-                self.machines[definition_id] = MachineNode(definition_id)
-                self.machines[definition_id].materialsChanged.connect(ContainerTree.getInstance().materialsChanged)
+                self._machines[definition_id] = MachineNode(definition_id)
+                self._machines[definition_id].materialsChanged.connect(ContainerTree.getInstance().materialsChanged)
                 Logger.log("d", "Adding container tree for {definition_id} took {duration} seconds.".format(definition_id = definition_id, duration = time.time() - start_time))
-            return self.machines[definition_id]
+            return self._machines[definition_id]
 
         ##  Gets a machine node for the specified definition ID, with default.
         #
@@ -125,7 +125,7 @@ class ContainerTree:
         #   \param definition_id The definition that we may have cached.
         #   \return ``True`` if it's cached.
         def is_loaded(self, definition_id: str) -> bool:
-            return definition_id in self.machines
+            return definition_id in self._machines
 
     ##  Pre-loads all currently added printers as a background task so that
     #   switching printers in the interface is faster.

+ 2 - 2
tests/Machines/TestContainerTree.py

@@ -55,7 +55,7 @@ def test_getCurrentQualityGroupsNoGlobalStack(container_registry):
 def test_getCurrentQualityGroups(container_registry, application):
     with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
         container_tree = ContainerTree()
-        container_tree.machines.machines["current_global_stack"] = MagicMock()  # Mock so that we can track whether the getQualityGroups function gets called with correct parameters.
+        container_tree.machines._machines["current_global_stack"] = MagicMock()  # Mock so that we can track whether the getQualityGroups function gets called with correct parameters.
 
         with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = application)):
             result = container_tree.getCurrentQualityGroups()
@@ -79,7 +79,7 @@ def test_getCurrentQualityChangesGroupsNoGlobalStack(container_registry):
 def test_getCurrentQualityChangesGroups(container_registry, application):
     with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
         container_tree = ContainerTree()
-        container_tree.machines.machines["current_global_stack"] = MagicMock()  # Mock so that we can track whether the getQualityGroups function gets called with correct parameters.
+        container_tree.machines._machines["current_global_stack"] = MagicMock()  # Mock so that we can track whether the getQualityGroups function gets called with correct parameters.
 
         with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = application)):
             result = container_tree.getCurrentQualityChangesGroups()