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.
This commit is contained in:
Ghostkeeper 2019-10-16 15:04:07 +02:00
parent 5624f0a8ae
commit 8179dfc412
No known key found for this signature in database
GPG Key ID: 86BEF881AE2CF276
2 changed files with 8 additions and 8 deletions

View File

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

View File

@ -55,7 +55,7 @@ def test_getCurrentQualityGroupsNoGlobalStack(container_registry):
def test_getCurrentQualityGroups(container_registry, application): def test_getCurrentQualityGroups(container_registry, application):
with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)): with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
container_tree = ContainerTree() 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)): with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = application)):
result = container_tree.getCurrentQualityGroups() result = container_tree.getCurrentQualityGroups()
@ -79,7 +79,7 @@ def test_getCurrentQualityChangesGroupsNoGlobalStack(container_registry):
def test_getCurrentQualityChangesGroups(container_registry, application): def test_getCurrentQualityChangesGroups(container_registry, application):
with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)): with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
container_tree = ContainerTree() 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)): with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = application)):
result = container_tree.getCurrentQualityChangesGroups() result = container_tree.getCurrentQualityChangesGroups()