XmlMaterialUpgrader.py 1.2 KB

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