XmlMaterialUpgrader.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright (c) 2019 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. from .XmlMaterialProfile import XmlMaterialProfile
  6. class XmlMaterialUpgrader(VersionUpgrade):
  7. def getXmlVersion(self, serialized):
  8. return XmlMaterialProfile.getVersionFromSerialized(serialized)
  9. def _xmlVersionToSettingVersion(self, xml_version: str) -> int:
  10. return XmlMaterialProfile.xmlVersionToSettingVersion(xml_version)
  11. def upgradeMaterial(self, serialised, filename):
  12. data = ET.fromstring(serialised)
  13. # update version
  14. metadata = data.iterfind("./um:metadata/*", {"um": "http://www.ultimaker.com/material"})
  15. for entry in metadata:
  16. if _tag_without_namespace(entry) == "version":
  17. entry.text = "2"
  18. break
  19. data.attrib["version"] = "1.3"
  20. # this makes sure that the XML header states encoding="utf-8"
  21. new_serialised = ET.tostring(data, encoding="utf-8").decode("utf-8")
  22. return [filename], [new_serialised]
  23. def _tag_without_namespace(element):
  24. return element.tag[element.tag.rfind("}") + 1:]