VersionUpgrade32to33.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 typing import Dict, List, Tuple
  6. from UM.VersionUpgrade import VersionUpgrade #We're inheriting from this.
  7. ## Mapping extruder definition IDs to the positions that they are in.
  8. _EXTRUDER_TO_POSITION = {
  9. "builder_premium_large_front": 1,
  10. "builder_premium_large_rear": 0,
  11. "builder_premium_medium_front": 1,
  12. "builder_premium_medium_rear": 0,
  13. "builder_premium_small_front": 1,
  14. "builder_premium_small_rear": 0,
  15. "cartesio_extruder_0": 0,
  16. "cartesio_extruder_1": 1,
  17. "cartesio_extruder_2": 2,
  18. "cartesio_extruder_3": 3,
  19. "custom_extruder_1": 0, #Warning, non-programmers are attempting to count here.
  20. "custom_extruder_2": 1,
  21. "custom_extruder_3": 2,
  22. "custom_extruder_4": 3,
  23. "custom_extruder_5": 4,
  24. "custom_extruder_6": 5,
  25. "custom_extruder_7": 6,
  26. "custom_extruder_8": 7,
  27. "hBp_extruder_left": 0,
  28. "hBp_extruder_right": 1,
  29. "makeit_dual_1st": 0,
  30. "makeit_dual_2nd": 1,
  31. "makeit_l_dual_1st": 0,
  32. "makeit_l_dual_2nd": 1,
  33. "ord_extruder_0": 0,
  34. "ord_extruder_1": 1,
  35. "ord_extruder_2": 2,
  36. "ord_extruder_3": 3,
  37. "ord_extruder_4": 4,
  38. "punchtec_connect_xl_extruder_left": 0,
  39. "punchtec_connect_xl_extruder_right": 1,
  40. "raise3D_N2_dual_extruder_0": 0,
  41. "raise3D_N2_dual_extruder_1": 1,
  42. "raise3D_N2_plus_dual_extruder_0": 0,
  43. "raise3D_N2_plus_dual_extruder_1": 1,
  44. "ultimaker3_extended_extruder_left": 0,
  45. "ultimaker3_extended_extruder_right": 1,
  46. "ultimaker3_extruder_left": 0,
  47. "ultimaker3_extruder_right": 1,
  48. "ultimaker_original_dual_1st": 0,
  49. "ultimaker_original_dual_2nd": 1,
  50. "vertex_k8400_dual_1st": 0,
  51. "vertex_k8400_dual_2nd": 1
  52. } # type: Dict[str, int]
  53. _RENAMED_QUALITY_PROFILES = {
  54. "low": "fast",
  55. "um2_low": "um2_fast"
  56. } # type: Dict[str, str]
  57. _RENAMED_QUALITY_TYPES = {
  58. "low": "fast"
  59. } # type: Dict[str, str]
  60. ## Upgrades configurations from the state they were in at version 3.2 to the
  61. # state they should be in at version 3.3.
  62. class VersionUpgrade32to33(VersionUpgrade):
  63. temporary_group_name_counter = 1
  64. ## Upgrades a preferences file from version 3.2 to 3.3.
  65. #
  66. # \param serialised The serialised form of a preferences file.
  67. # \param filename The name of the file to upgrade.
  68. def upgradePreferences(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  69. parser = configparser.ConfigParser(interpolation = None)
  70. parser.read_string(serialised)
  71. # Update version numbers
  72. if "general" not in parser:
  73. parser["general"] = {}
  74. parser["general"]["version"] = "6"
  75. if "metadata" not in parser:
  76. parser["metadata"] = {}
  77. parser["metadata"]["setting_version"] = "4"
  78. # The auto_slice preference changed its default value to "disabled" so if there is no value in previous versions,
  79. # then it means the desired value is auto_slice "enabled"
  80. if "auto_slice" not in parser["general"]:
  81. parser["general"]["auto_slice"] = "True"
  82. elif parser["general"]["auto_slice"] == "False": # If the value is False, then remove the entry
  83. del parser["general"]["auto_slice"]
  84. # Re-serialise the file.
  85. output = io.StringIO()
  86. parser.write(output)
  87. return [filename], [output.getvalue()]
  88. ## Upgrades a container stack from version 3.2 to 3.3.
  89. #
  90. # \param serialised The serialised form of a container stack.
  91. # \param filename The name of the file to upgrade.
  92. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  93. parser = configparser.ConfigParser(interpolation = None)
  94. parser.read_string(serialized)
  95. if "metadata" in parser and "um_network_key" in parser["metadata"]:
  96. if "hidden" not in parser["metadata"]:
  97. parser["metadata"]["hidden"] = "False"
  98. if "connect_group_name" not in parser["metadata"]:
  99. parser["metadata"]["connect_group_name"] = "Temporary group name #" + str(self.temporary_group_name_counter)
  100. self.temporary_group_name_counter += 1
  101. #Update version number.
  102. parser["general"]["version"] = "4"
  103. #Update the name of the quality profile.
  104. if parser["containers"]["2"] in _RENAMED_QUALITY_PROFILES:
  105. parser["containers"]["2"] = _RENAMED_QUALITY_PROFILES[parser["containers"]["2"]]
  106. result = io.StringIO()
  107. parser.write(result)
  108. return [filename], [result.getvalue()]
  109. ## Upgrades non-quality-changes instance containers to have the new version
  110. # number.
  111. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  112. parser = configparser.ConfigParser(interpolation = None)
  113. parser.read_string(serialized)
  114. #Update version number.
  115. parser["general"]["version"] = "3"
  116. result = io.StringIO()
  117. parser.write(result)
  118. return [filename], [result.getvalue()]
  119. ## Upgrades a quality changes container to the new format.
  120. def upgradeQualityChanges(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  121. parser = configparser.ConfigParser(interpolation = None)
  122. parser.read_string(serialized)
  123. #Extruder quality changes profiles have the extruder position instead of the ID of the extruder definition.
  124. if "metadata" in parser and "extruder" in parser["metadata"]: #Only do this for extruder profiles.
  125. extruder_id = parser["metadata"]["extruder"]
  126. if extruder_id in _EXTRUDER_TO_POSITION:
  127. extruder_position = _EXTRUDER_TO_POSITION[extruder_id]
  128. else:
  129. extruder_position = 0 #The user was using custom extruder definitions. He's on his own then.
  130. parser["metadata"]["position"] = str(extruder_position)
  131. del parser["metadata"]["extruder"]
  132. quality_type = parser["metadata"]["quality_type"]
  133. quality_type = quality_type.lower()
  134. if quality_type in _RENAMED_QUALITY_TYPES:
  135. quality_type = _RENAMED_QUALITY_TYPES[quality_type]
  136. parser["metadata"]["quality_type"] = quality_type
  137. #Update version number.
  138. parser["general"]["version"] = "3"
  139. result = io.StringIO()
  140. parser.write(result)
  141. return [filename], [result.getvalue()]
  142. ## Upgrades a variant container to the new format.
  143. def upgradeVariants(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  144. parser = configparser.ConfigParser(interpolation = None)
  145. parser.read_string(serialized)
  146. #Add the hardware type to the variants
  147. if "metadata" in parser and "hardware_type" not in parser["metadata"]:
  148. parser["metadata"]["hardware_type"] = "nozzle"
  149. #Update version number.
  150. parser["general"]["version"] = "3"
  151. result = io.StringIO()
  152. parser.write(result)
  153. return [filename], [result.getvalue()]