Fix project conflict resolving for global stack and extruder stacks

CURA-4053

- Fix that if the resolve strategy is new for machine, Cura should
  always create new global and extruder stacks
- Fix possible duplicated IDs when "Create New" machine is selected
This commit is contained in:
Lipu Fei 2017-07-19 15:17:10 +02:00
parent 834fd055b6
commit 3474bb0738

View file

@ -582,13 +582,11 @@ class ThreeMFWorkspaceReader(WorkspaceReader):
# -- # --
# load global stack file # load global stack file
try: try:
# Check if a stack by this ID already exists; if self._resolve_strategies["machine"] == "override":
container_stacks = self._container_registry.findContainerStacks(id = global_stack_id_original) container_stacks = self._container_registry.findContainerStacks(id = global_stack_id_original)
if container_stacks:
stack = container_stacks[0] stack = container_stacks[0]
if self._resolve_strategies["machine"] == "override": # HACK
# TODO: HACK
# There is a machine, check if it has authentication data. If so, keep that data. # There is a machine, check if it has authentication data. If so, keep that data.
network_authentication_id = container_stacks[0].getMetaDataEntry("network_authentication_id") network_authentication_id = container_stacks[0].getMetaDataEntry("network_authentication_id")
network_authentication_key = container_stacks[0].getMetaDataEntry("network_authentication_key") network_authentication_key = container_stacks[0].getMetaDataEntry("network_authentication_key")
@ -597,8 +595,11 @@ class ThreeMFWorkspaceReader(WorkspaceReader):
container_stacks[0].addMetaDataEntry("network_authentication_id", network_authentication_id) container_stacks[0].addMetaDataEntry("network_authentication_id", network_authentication_id)
if network_authentication_key: if network_authentication_key:
container_stacks[0].addMetaDataEntry("network_authentication_key", network_authentication_key) container_stacks[0].addMetaDataEntry("network_authentication_key", network_authentication_key)
elif self._resolve_strategies["machine"] == "new": elif self._resolve_strategies["machine"] == "new":
# create a new global stack
stack = GlobalStack(global_stack_id_new) stack = GlobalStack(global_stack_id_new)
# Deserialize stack by converting read data from bytes to string
stack.deserialize(archive.open(global_stack_file).read().decode("utf-8")) stack.deserialize(archive.open(global_stack_file).read().decode("utf-8"))
# Ensure a unique ID and name # Ensure a unique ID and name
@ -611,18 +612,13 @@ class ThreeMFWorkspaceReader(WorkspaceReader):
# Only machines need a new name, stacks may be non-unique # Only machines need a new name, stacks may be non-unique
stack.setName(self._container_registry.uniqueName(stack.getName())) stack.setName(self._container_registry.uniqueName(stack.getName()))
container_stacks_added.append(stack)
self._container_registry.addContainer(stack)
else:
Logger.log("w", "Resolve strategy of %s for machine is not supported", self._resolve_strategies["machine"])
else:
# no existing container stack, so we create a new one
stack = GlobalStack(global_stack_id_new)
# Deserialize stack by converting read data from bytes to string
stack.deserialize(archive.open(global_stack_file).read().decode("utf-8"))
container_stacks_added.append(stack) container_stacks_added.append(stack)
self._container_registry.addContainer(stack) self._container_registry.addContainer(stack)
containers_added.append(stack) containers_added.append(stack)
else:
Logger.log("e", "Resolve strategy of %s for machine is not supported",
self._resolve_strategies["machine"])
global_stack = stack global_stack = stack
Job.yieldThread() Job.yieldThread()
@ -636,22 +632,18 @@ class ThreeMFWorkspaceReader(WorkspaceReader):
# -- # --
# load extruder stack files # load extruder stack files
try: try:
for index, extruder_stack_file in enumerate(extruder_stack_files): for extruder_stack_file in extruder_stack_files:
container_id = self._stripFileToId(extruder_stack_file) container_id = self._stripFileToId(extruder_stack_file)
extruder_file_content = archive.open(extruder_stack_file, "r").read().decode("utf-8") extruder_file_content = archive.open(extruder_stack_file, "r").read().decode("utf-8")
if self._resolve_strategies["machine"] == "override":
container_stacks = self._container_registry.findContainerStacks(id = container_id) container_stacks = self._container_registry.findContainerStacks(id = container_id)
if container_stacks:
# this container stack already exists, try to resolve
stack = container_stacks[0] stack = container_stacks[0]
if self._resolve_strategies["machine"] == "override":
# NOTE: This is the same code as those in the lower part
# deserialize new extruder stack over the current ones # deserialize new extruder stack over the current ones
stack = self._overrideExtruderStack(global_stack, extruder_file_content) stack = self._overrideExtruderStack(global_stack, extruder_file_content)
elif self._resolve_strategies["machine"] == "new": elif self._resolve_strategies["machine"] == "new":
# create a new extruder stack from this one
new_id = extruder_stack_id_map[container_id] new_id = extruder_stack_id_map[container_id]
stack = ExtruderStack(new_id) stack = ExtruderStack(new_id)
@ -676,33 +668,7 @@ class ThreeMFWorkspaceReader(WorkspaceReader):
extruder_stacks_added.append(stack) extruder_stacks_added.append(stack)
containers_added.append(stack) containers_added.append(stack)
else: else:
# No extruder stack with the same ID can be found Logger.log("w", "Unknown resolve strategy: %s", self._resolve_strategies["machine"])
if self._resolve_strategies["machine"] == "override":
# deserialize new extruder stack over the current ones
stack = self._overrideExtruderStack(global_stack, extruder_file_content)
elif self._resolve_strategies["machine"] == "new":
# container not found, create a new one
stack = ExtruderStack(container_id)
# HACK: the global stack can have a new name, so we need to make sure that this extruder stack
# references to the new name instead of the old one. Normally, this can be done after
# deserialize() by setting the metadata, but in the case of ExtruderStack, deserialize()
# also does addExtruder() to its machine stack, so we have to make sure that it's pointing
# to the right machine BEFORE deserialization.
extruder_config = configparser.ConfigParser()
extruder_config.read_string(extruder_file_content)
extruder_config.set("metadata", "machine", global_stack_id_new)
tmp_string_io = io.StringIO()
extruder_config.write(tmp_string_io)
extruder_file_content = tmp_string_io.getvalue()
stack.deserialize(extruder_file_content)
self._container_registry.addContainer(stack)
extruder_stacks_added.append(stack)
containers_added.append(stack)
else:
Logger.log("w", "Unknown resolve strategy: %s" % str(self._resolve_strategies["machine"]))
extruder_stacks.append(stack) extruder_stacks.append(stack)
except: except: