XmlMaterialUpgrader.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import xml.etree.ElementTree as ET
  4. from UM.VersionUpgrade import VersionUpgrade
  5. class XmlMaterialUpgrader(VersionUpgrade):
  6. def getXmlVersion(self, serialized):
  7. data = ET.fromstring(serialized)
  8. version = 1
  9. # get setting version
  10. if "version" in data.attrib:
  11. setting_version = self._xmlVersionToSettingVersion(data.attrib["version"])
  12. else:
  13. setting_version = self._xmlVersionToSettingVersion("1.2")
  14. return version * 1000000 + setting_version
  15. def _xmlVersionToSettingVersion(self, xml_version: str) -> int:
  16. if xml_version == "1.3":
  17. return 2
  18. return 0 #Older than 1.3.
  19. def upgradeMaterial(self, serialised, filename):
  20. data = ET.fromstring(serialised)
  21. # update version
  22. metadata = data.iterfind("./um:metadata/*", {"um": "http://www.ultimaker.com/material"})
  23. for entry in metadata:
  24. if _tag_without_namespace(entry) == "version":
  25. entry.text = "2"
  26. break
  27. data.attrib["version"] = "1.3"
  28. # this makes sure that the XML header states encoding="utf-8"
  29. new_serialised = ET.tostring(data, encoding="utf-8").decode("utf-8")
  30. return [filename], [new_serialised]
  31. def _tag_without_namespace(element):
  32. return element.tag[element.tag.rfind("}") + 1:]