XML material profile merging is now far less naive.

It's still a work in progress, as it doesn't succeed in all cases just yet.

CURA-2108
This commit is contained in:
Jaime van Kessel 2016-08-31 17:18:33 +02:00
parent 1fda24c43f
commit bacac1e42e

View file

@ -58,9 +58,9 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer):
def setReadOnly(self, read_only):
super().setReadOnly(read_only)
basefile = self.getMetaDataEntry("base_file", self._id) #if basefile is none, this is a basefile.
basefile = self.getMetaDataEntry("base_file", self._id) # if basefile is self.id, this is a basefile.
for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
container._read_only = read_only
container._read_only = read_only # prevent loop instead of calling setReadOnly
## Overridden from InstanceContainer
def setMetaDataEntry(self, key, value):
@ -69,7 +69,7 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer):
super().setMetaDataEntry(key, value)
basefile = self.getMetaDataEntry("base_file", self._id) #if basefile is none, this is a basefile.
basefile = self.getMetaDataEntry("base_file", self._id) #if basefile is self.id, this is a basefile.
# Update all containers that share GUID and basefile
for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
container.setMetaData(copy.deepcopy(self._metadata))
@ -95,15 +95,15 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer):
container.setName(new_name)
## Overridden from InstanceContainer
def setProperty(self, key, property_name, property_value, container = None):
if self.isReadOnly():
return
super().setProperty(key, property_name, property_value)
basefile = self.getMetaDataEntry("base_file", self._id) #if basefile is none, this is a basefile.
for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
container._dirty = True
# def setProperty(self, key, property_name, property_value, container = None):
# if self.isReadOnly():
# return
#
# super().setProperty(key, property_name, property_value)
#
# basefile = self.getMetaDataEntry("base_file", self._id) #if basefile is self.id, this is a basefile.
# for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
# container._dirty = True
## Overridden from InstanceContainer
def serialize(self):
@ -277,24 +277,50 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer):
self._combineElement(result, second)
return result
def _createKey(self, element):
key = element.tag.split("}")[-1]
if "key" in element.attrib:
key += " key:" + element.attrib["key"]
if "manufacturer" in element.attrib:
key += " manufacturer:" + element.attrib["manufacturer"]
if "product" in element.attrib:
key += " product:" + element.attrib["product"]
if key == "machine":
for item in element:
if "machine_identifier" in item.tag:
key += " " + item.attrib["product"]
return key
# Recursively merges XML elements. Updates either the text or children if another element is found in first.
# If it does not exist, copies it from second.
def _combineElement(self, first, second):
# Create a mapping from tag name to element.
mapping = {el.tag: el for el in first}
for el in second:
if len(el): # Check if element has children.
mapping = {}
for element in first:
key = self._createKey(element)
mapping[key] = element
for element in second:
key = self._createKey(element)
if len(element): # Check if element has children.
try:
self._combineElement(mapping[el.tag], el) # Multiple elements, handle those.
if "setting " in key:
# Setting can have points in it. In that case, delete all values and override them.
for child in list(mapping[key]):
mapping[key].remove(child)
for child in element:
mapping[key].append(child)
else:
self._combineElement(mapping[key], element) # Multiple elements, handle those.
except KeyError:
mapping[el.tag] = el
first.append(el)
mapping[key] = element
first.append(element)
else:
try:
mapping[el.tag].text = el.text
mapping[key].text = element.text
except KeyError: # Not in the mapping, so simply add it
mapping[el.tag] = el
first.append(el)
mapping[key] = element
first.append(element)
## Overridden from InstanceContainer
def deserialize(self, serialized):
@ -305,7 +331,7 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer):
# TODO: Add material verfication
self.addMetaDataEntry("status", "unknown")
inherits = data.find("./um:inherits", self.__namespaces)
if inherits is not None:
inherited = self._resolveInheritance(inherits.text)