VersionUpgrade54to55.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (c) 2023 UltiMaker
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser
  4. from typing import Tuple, List
  5. import io
  6. from UM.VersionUpgrade import VersionUpgrade
  7. import re
  8. class VersionUpgrade54to55(VersionUpgrade):
  9. profile_regex = re.compile(
  10. r"um\_(?P<machine>s(3|5|7))_(?P<core_type>aa|cc|bb)(?P<nozzle_size>0\.(6|4|8))_(?P<material>pla|petg|abs|tough_pla)_(?P<layer_height>0\.\d{1,2}mm)")
  11. @staticmethod
  12. def _isUpgradedUltimakerDefinitionId(definition_id: str) -> bool:
  13. if definition_id.startswith("ultimaker_s5"):
  14. return True
  15. if definition_id.startswith("ultimaker_s3"):
  16. return True
  17. if definition_id.startswith("ultimaker_s7"):
  18. return True
  19. return False
  20. @staticmethod
  21. def _isBrandedMaterialID(material_id: str) -> bool:
  22. return material_id.startswith("ultimaker_")
  23. @staticmethod
  24. def upgradeStack(serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  25. """
  26. Upgrades stacks to have the new version number.
  27. :param serialized: The original contents of the stack.
  28. :param filename: The original file name of the stack.
  29. :return: A list of new file names, and a list of the new contents for
  30. those files.
  31. """
  32. parser = configparser.ConfigParser(interpolation = None)
  33. parser.read_string(serialized)
  34. # Update version number.
  35. if "general" not in parser:
  36. parser["general"] = {}
  37. extruder_definition_id = parser["containers"]["7"]
  38. if parser["metadata"]["type"] == "extruder_train" and VersionUpgrade54to55._isUpgradedUltimakerDefinitionId(extruder_definition_id):
  39. # We only need to update certain Ultimaker extruder ID's
  40. material_id = parser["containers"]["4"]
  41. quality_id = parser["containers"]["3"]
  42. intent_id = parser["containers"]["2"]
  43. if VersionUpgrade54to55._isBrandedMaterialID(material_id):
  44. # We have an Ultimaker branded material ID, so we should change the intent & quality!
  45. quality_id = VersionUpgrade54to55.profile_regex.sub(
  46. r"um_\g<machine>_\g<core_type>\g<nozzle_size>_um-\g<material>_\g<layer_height>", quality_id)
  47. intent_id = VersionUpgrade54to55.profile_regex.sub(
  48. r"um_\g<machine>_\g<core_type>\g<nozzle_size>_um-\g<material>_\g<layer_height>", intent_id)
  49. parser["containers"]["3"] = quality_id
  50. parser["containers"]["2"] = intent_id
  51. # We're not changing any settings, but we are changing how certain stacks are handled.
  52. parser["general"]["version"] = "6"
  53. result = io.StringIO()
  54. parser.write(result)
  55. return [filename], [result.getvalue()]