VersionUpgrade32to33.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser #To parse preference files.
  4. import io #To serialise the preference files afterwards.
  5. from UM.VersionUpgrade import VersionUpgrade #We're inheriting from this.
  6. ## Mapping extruder definition IDs to the positions that they are in.
  7. _EXTRUDER_TO_POSITION = {
  8. "builder_premium_large_front": 1,
  9. "builder_premium_large_rear": 0,
  10. "builder_premium_medium_front": 1,
  11. "builder_premium_medium_rear": 0,
  12. "builder_premium_small_front": 1,
  13. "builder_premium_small_rear": 0,
  14. "cartesio_extruder_0": 0,
  15. "cartesio_extruder_1": 1,
  16. "cartesio_extruder_2": 2,
  17. "cartesio_extruder_3": 3,
  18. "custom_extruder_1": 0, #Warning, non-programmers are attempting to count here.
  19. "custom_extruder_2": 1,
  20. "custom_extruder_3": 2,
  21. "custom_extruder_4": 3,
  22. "custom_extruder_5": 4,
  23. "custom_extruder_6": 5,
  24. "custom_extruder_7": 6,
  25. "custom_extruder_8": 7,
  26. "hBp_extruder_left": 0,
  27. "hBp_extruder_right": 1,
  28. "makeit_dual_1st": 0,
  29. "makeit_dual_2nd": 1,
  30. "makeit_l_dual_1st": 0,
  31. "makeit_l_dual_2nd": 1,
  32. "ord_extruder_0": 0,
  33. "ord_extruder_1": 1,
  34. "ord_extruder_2": 2,
  35. "ord_extruder_3": 3,
  36. "ord_extruder_4": 4,
  37. "punchtec_connect_xl_extruder_left": 0,
  38. "punchtec_connect_xl_extruder_right": 1,
  39. "raise3D_N2_dual_extruder_0": 0,
  40. "raise3D_N2_dual_extruder_1": 1,
  41. "raise3D_N2_plus_dual_extruder_0": 0,
  42. "raise3D_N2_plus_dual_extruder_1": 1,
  43. "ultimaker3_extended_extruder_left": 0,
  44. "ultimaker3_extended_extruder_right": 1,
  45. "ultimaker3_extruder_left": 0,
  46. "ultimaker3_extruder_right": 1,
  47. "ultimaker_original_dual_1st": 0,
  48. "ultimaker_original_dual_2nd": 1,
  49. "vertex_k8400_dual_1st": 0,
  50. "vertex_k8400_dual_2nd": 1
  51. }
  52. ## Upgrades configurations from the state they were in at version 3.2 to the
  53. # state they should be in at version 3.3.
  54. class VersionUpgrade32to33(VersionUpgrade):
  55. temporary_group_name_counter = 1
  56. ## Gets the version number from a CFG file in Uranium's 3.2 format.
  57. #
  58. # Since the format may change, this is implemented for the 3.2 format only
  59. # and needs to be included in the version upgrade system rather than
  60. # globally in Uranium.
  61. #
  62. # \param serialised The serialised form of a CFG file.
  63. # \return The version number stored in the CFG file.
  64. # \raises ValueError The format of the version number in the file is
  65. # incorrect.
  66. # \raises KeyError The format of the file is incorrect.
  67. def getCfgVersion(self, serialised):
  68. parser = configparser.ConfigParser(interpolation = None)
  69. parser.read_string(serialised)
  70. format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  71. setting_version = int(parser.get("metadata", "setting_version", fallback = 0))
  72. return format_version * 1000000 + setting_version
  73. ## Upgrades a container stack from version 3.2 to 3.3.
  74. #
  75. # \param serialised The serialised form of a container stack.
  76. # \param filename The name of the file to upgrade.
  77. def upgradeStack(self, serialized, filename):
  78. parser = configparser.ConfigParser(interpolation = None)
  79. parser.read_string(serialized)
  80. if "metadata" in parser and "um_network_key" in parser["metadata"]:
  81. if "hidden" not in parser["metadata"]:
  82. parser["metadata"]["hidden"] = "False"
  83. if "connect_group_name" not in parser["metadata"]:
  84. parser["metadata"]["connect_group_name"] = "Temporary group name #" + str(self.temporary_group_name_counter)
  85. self.temporary_group_name_counter += 1
  86. #Update version number.
  87. parser["general"]["version"] = "4"
  88. result = io.StringIO()
  89. parser.write(result)
  90. return [filename], [result.getvalue()]
  91. ## Upgrades non-quality-changes instance containers to have the new version
  92. # number.
  93. def upgradeInstanceContainer(self, serialized, filename):
  94. parser = configparser.ConfigParser(interpolation = None)
  95. parser.read_string(serialized)
  96. #Update version number.
  97. parser["general"]["version"] = "3"
  98. result = io.StringIO()
  99. parser.write(result)
  100. return [filename], [result.getvalue()]
  101. ## Upgrades a quality changes container to the new format.
  102. def upgradeQualityChanges(self, serialized, filename):
  103. parser = configparser.ConfigParser(interpolation = None)
  104. parser.read_string(serialized)
  105. #Extruder quality changes profiles have the extruder position instead of the ID of the extruder definition.
  106. if "metadata" in parser and "extruder" in parser["metadata"]: #Only do this for extruder profiles.
  107. extruder_id = parser["metadata"]["extruder"]
  108. if extruder_id in _EXTRUDER_TO_POSITION:
  109. extruder_position = _EXTRUDER_TO_POSITION[extruder_id]
  110. else:
  111. extruder_position = 0 #The user was using custom extruder definitions. He's on his own then.
  112. parser["metadata"]["position"] = str(extruder_position)
  113. del parser["metadata"]["extruder"]
  114. #Update version number.
  115. parser["general"]["version"] = "3"
  116. result = io.StringIO()
  117. parser.write(result)
  118. return [filename], [result.getvalue()]