VersionUpgrade32to33.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. ## Gets the version number from a CFG file in Uranium's 3.2 format.
  65. #
  66. # Since the format may change, this is implemented for the 3.2 format only
  67. # and needs to be included in the version upgrade system rather than
  68. # globally in Uranium.
  69. #
  70. # \param serialised The serialised form of a CFG file.
  71. # \return The version number stored in the CFG file.
  72. # \raises ValueError The format of the version number in the file is
  73. # incorrect.
  74. # \raises KeyError The format of the file is incorrect.
  75. def getCfgVersion(self, serialised: str) -> int:
  76. parser = configparser.ConfigParser(interpolation = None)
  77. parser.read_string(serialised)
  78. format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  79. setting_version = int(parser.get("metadata", "setting_version", fallback = "0"))
  80. return format_version * 1000000 + setting_version
  81. ## Upgrades a preferences file from version 3.2 to 3.3.
  82. #
  83. # \param serialised The serialised form of a preferences file.
  84. # \param filename The name of the file to upgrade.
  85. def upgradePreferences(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  86. parser = configparser.ConfigParser(interpolation = None)
  87. parser.read_string(serialised)
  88. # Update version numbers
  89. if "general" not in parser:
  90. parser["general"] = {}
  91. parser["general"]["version"] = "6"
  92. if "metadata" not in parser:
  93. parser["metadata"] = {}
  94. parser["metadata"]["setting_version"] = "4"
  95. # The auto_slice preference changed its default value to "disabled" so if there is no value in previous versions,
  96. # then it means the desired value is auto_slice "enabled"
  97. if "auto_slice" not in parser["general"]:
  98. parser["general"]["auto_slice"] = "True"
  99. elif parser["general"]["auto_slice"] == "False": # If the value is False, then remove the entry
  100. del parser["general"]["auto_slice"]
  101. # Re-serialise the file.
  102. output = io.StringIO()
  103. parser.write(output)
  104. return [filename], [output.getvalue()]
  105. ## Upgrades a container stack from version 3.2 to 3.3.
  106. #
  107. # \param serialised The serialised form of a container stack.
  108. # \param filename The name of the file to upgrade.
  109. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  110. parser = configparser.ConfigParser(interpolation = None)
  111. parser.read_string(serialized)
  112. if "metadata" in parser and "um_network_key" in parser["metadata"]:
  113. if "hidden" not in parser["metadata"]:
  114. parser["metadata"]["hidden"] = "False"
  115. if "connect_group_name" not in parser["metadata"]:
  116. parser["metadata"]["connect_group_name"] = "Temporary group name #" + str(self.temporary_group_name_counter)
  117. self.temporary_group_name_counter += 1
  118. #Update version number.
  119. parser["general"]["version"] = "4"
  120. #Update the name of the quality profile.
  121. if parser["containers"]["2"] in _RENAMED_QUALITY_PROFILES:
  122. parser["containers"]["2"] = _RENAMED_QUALITY_PROFILES[parser["containers"]["2"]]
  123. result = io.StringIO()
  124. parser.write(result)
  125. return [filename], [result.getvalue()]
  126. ## Upgrades non-quality-changes instance containers to have the new version
  127. # number.
  128. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  129. parser = configparser.ConfigParser(interpolation = None)
  130. parser.read_string(serialized)
  131. #Update version number.
  132. parser["general"]["version"] = "3"
  133. result = io.StringIO()
  134. parser.write(result)
  135. return [filename], [result.getvalue()]
  136. ## Upgrades a quality changes container to the new format.
  137. def upgradeQualityChanges(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  138. parser = configparser.ConfigParser(interpolation = None)
  139. parser.read_string(serialized)
  140. #Extruder quality changes profiles have the extruder position instead of the ID of the extruder definition.
  141. if "metadata" in parser and "extruder" in parser["metadata"]: #Only do this for extruder profiles.
  142. extruder_id = parser["metadata"]["extruder"]
  143. if extruder_id in _EXTRUDER_TO_POSITION:
  144. extruder_position = _EXTRUDER_TO_POSITION[extruder_id]
  145. else:
  146. extruder_position = 0 #The user was using custom extruder definitions. He's on his own then.
  147. parser["metadata"]["position"] = str(extruder_position)
  148. del parser["metadata"]["extruder"]
  149. quality_type = parser["metadata"]["quality_type"]
  150. quality_type = quality_type.lower()
  151. if quality_type in _RENAMED_QUALITY_TYPES:
  152. quality_type = _RENAMED_QUALITY_TYPES[quality_type]
  153. parser["metadata"]["quality_type"] = quality_type
  154. #Update version number.
  155. parser["general"]["version"] = "3"
  156. result = io.StringIO()
  157. parser.write(result)
  158. return [filename], [result.getvalue()]
  159. ## Upgrades a variant container to the new format.
  160. def upgradeVariants(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  161. parser = configparser.ConfigParser(interpolation = None)
  162. parser.read_string(serialized)
  163. #Add the hardware type to the variants
  164. if "metadata" in parser and "hardware_type" not in parser["metadata"]:
  165. parser["metadata"]["hardware_type"] = "nozzle"
  166. #Update version number.
  167. parser["general"]["version"] = "3"
  168. result = io.StringIO()
  169. parser.write(result)
  170. return [filename], [result.getvalue()]