VersionUpgrade30to31.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # Copyright (c) 2017 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. import os
  6. from urllib.parse import quote_plus
  7. from UM.Resources import Resources
  8. from UM.VersionUpgrade import VersionUpgrade #We're inheriting from this.
  9. from cura.CuraApplication import CuraApplication
  10. # a list of all legacy "Not Supported" quality profiles
  11. _OLD_NOT_SUPPORTED_PROFILES = [
  12. "um2p_pp_0.25_normal",
  13. "um2p_tpu_0.8_normal",
  14. "um3_bb0.4_ABS_Fast_Print",
  15. "um3_bb0.4_ABS_Superdraft_Print",
  16. "um3_bb0.4_CPEP_Fast_Print",
  17. "um3_bb0.4_CPEP_Superdraft_Print",
  18. "um3_bb0.4_CPE_Fast_Print",
  19. "um3_bb0.4_CPE_Superdraft_Print",
  20. "um3_bb0.4_Nylon_Fast_Print",
  21. "um3_bb0.4_Nylon_Superdraft_Print",
  22. "um3_bb0.4_PC_Fast_Print",
  23. "um3_bb0.4_PLA_Fast_Print",
  24. "um3_bb0.4_PLA_Superdraft_Print",
  25. "um3_bb0.4_PP_Fast_Print",
  26. "um3_bb0.4_PP_Superdraft_Print",
  27. "um3_bb0.4_TPU_Fast_Print",
  28. "um3_bb0.4_TPU_Superdraft_Print",
  29. "um3_bb0.8_ABS_Fast_Print",
  30. "um3_bb0.8_ABS_Superdraft_Print",
  31. "um3_bb0.8_CPEP_Fast_Print",
  32. "um3_bb0.8_CPEP_Superdraft_Print",
  33. "um3_bb0.8_CPE_Fast_Print",
  34. "um3_bb0.8_CPE_Superdraft_Print",
  35. "um3_bb0.8_Nylon_Fast_Print",
  36. "um3_bb0.8_Nylon_Superdraft_Print",
  37. "um3_bb0.8_PC_Fast_Print",
  38. "um3_bb0.8_PC_Superdraft_Print",
  39. "um3_bb0.8_PLA_Fast_Print",
  40. "um3_bb0.8_PLA_Superdraft_Print",
  41. "um3_bb0.8_PP_Fast_Print",
  42. "um3_bb0.8_PP_Superdraft_Print",
  43. "um3_bb0.8_TPU_Fast_print",
  44. "um3_bb0.8_TPU_Superdraft_Print",
  45. ]
  46. # Some containers have their specific empty containers, those need to be set correctly.
  47. _EMPTY_CONTAINER_DICT = {
  48. "1": "empty_quality_changes",
  49. "2": "empty_quality",
  50. "3": "empty_material",
  51. "4": "empty_variant",
  52. }
  53. class VersionUpgrade30to31(VersionUpgrade):
  54. ## Gets the version number from a CFG file in Uranium's 3.0 format.
  55. #
  56. # Since the format may change, this is implemented for the 3.0 format only
  57. # and needs to be included in the version upgrade system rather than
  58. # globally in Uranium.
  59. #
  60. # \param serialised The serialised form of a CFG file.
  61. # \return The version number stored in the CFG file.
  62. # \raises ValueError The format of the version number in the file is
  63. # incorrect.
  64. # \raises KeyError The format of the file is incorrect.
  65. def getCfgVersion(self, serialised):
  66. parser = configparser.ConfigParser(interpolation = None)
  67. parser.read_string(serialised)
  68. format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  69. setting_version = int(parser.get("metadata", "setting_version", fallback = 0))
  70. return format_version * 1000000 + setting_version
  71. ## Upgrades a preferences file from version 3.0 to 3.1.
  72. #
  73. # \param serialised The serialised form of a preferences file.
  74. # \param filename The name of the file to upgrade.
  75. def upgradePreferences(self, serialised, filename):
  76. parser = configparser.ConfigParser(interpolation=None)
  77. parser.read_string(serialised)
  78. # Update version numbers
  79. if "general" not in parser:
  80. parser["general"] = {}
  81. parser["general"]["version"] = "5"
  82. if "metadata" not in parser:
  83. parser["metadata"] = {}
  84. parser["metadata"]["setting_version"] = "4"
  85. # Re-serialise the file.
  86. output = io.StringIO()
  87. parser.write(output)
  88. return [filename], [output.getvalue()]
  89. ## Upgrades the given instance container file from version 3.0 to 3.1.
  90. #
  91. # \param serialised The serialised form of the container file.
  92. # \param filename The name of the file to upgrade.
  93. def upgradeInstanceContainer(self, serialised, filename):
  94. parser = configparser.ConfigParser(interpolation=None)
  95. parser.read_string(serialised)
  96. for each_section in ("general", "metadata"):
  97. if not parser.has_section(each_section):
  98. parser.add_section(each_section)
  99. # Copy global quality changes to extruder quality changes for single extrusion machines
  100. if parser["metadata"]["type"] == "quality_changes":
  101. all_quality_changes = self._getSingleExtrusionMachineQualityChanges(parser)
  102. if len(all_quality_changes) == 1 and not all_quality_changes[0].has_option("metadata", "extruder"):
  103. self._createExtruderQualityChangesForSingleExtrusionMachine(filename, all_quality_changes[0])
  104. # Update version numbers
  105. parser["general"]["version"] = "2"
  106. parser["metadata"]["setting_version"] = "4"
  107. # Re-serialise the file.
  108. output = io.StringIO()
  109. parser.write(output)
  110. return [filename], [output.getvalue()]
  111. ## Upgrades a container stack from version 3.0 to 3.1.
  112. #
  113. # \param serialised The serialised form of a container stack.
  114. # \param filename The name of the file to upgrade.
  115. def upgradeStack(self, serialised, filename):
  116. parser = configparser.ConfigParser(interpolation=None)
  117. parser.read_string(serialised)
  118. for each_section in ("general", "metadata"):
  119. if not parser.has_section(each_section):
  120. parser.add_section(each_section)
  121. # change "not supported" quality profiles to empty because they no longer exist
  122. if parser.has_section("containers"):
  123. if parser.has_option("containers", "2"):
  124. quality_profile_id = parser["containers"]["2"]
  125. if quality_profile_id in _OLD_NOT_SUPPORTED_PROFILES:
  126. parser["containers"]["2"] = "empty_quality"
  127. # fix empty containers
  128. for key, specific_empty_container in _EMPTY_CONTAINER_DICT.items():
  129. if parser.has_option("containers", key) and parser["containers"][key] == "empty":
  130. parser["containers"][key] = specific_empty_container
  131. # Update version numbers
  132. if "general" not in parser:
  133. parser["general"] = {}
  134. parser["general"]["version"] = "3"
  135. if "metadata" not in parser:
  136. parser["metadata"] = {}
  137. parser["metadata"]["setting_version"] = "4"
  138. # Re-serialise the file.
  139. output = io.StringIO()
  140. parser.write(output)
  141. return [filename], [output.getvalue()]
  142. def _getSingleExtrusionMachineQualityChanges(self, quality_changes_container):
  143. quality_changes_dir = Resources.getPath(CuraApplication.ResourceTypes.QualityInstanceContainer)
  144. quality_changes_containers = []
  145. for item in os.listdir(quality_changes_dir):
  146. file_path = os.path.join(quality_changes_dir, item)
  147. if not os.path.isfile(file_path):
  148. continue
  149. parser = configparser.ConfigParser()
  150. try:
  151. parser.read([file_path])
  152. except:
  153. # skip, it is not a valid stack file
  154. continue
  155. if not parser.has_option("metadata", "type"):
  156. continue
  157. if "quality_changes" != parser["metadata"]["type"]:
  158. continue
  159. if not parser.has_option("general", "name"):
  160. continue
  161. if quality_changes_container["general"]["name"] != parser["general"]["name"]:
  162. continue
  163. quality_changes_containers.append(parser)
  164. return quality_changes_containers
  165. def _createExtruderQualityChangesForSingleExtrusionMachine(self, filename, global_quality_changes):
  166. suffix = "_" + quote_plus(global_quality_changes["general"]["name"].lower())
  167. machine_name = filename.strip("." + os.sep).replace(suffix, "")
  168. new_filename = machine_name + "_" + "fdmextruder" + suffix
  169. extruder_quality_changes_parser = configparser.ConfigParser()
  170. extruder_quality_changes_parser.add_section("general")
  171. extruder_quality_changes_parser["general"]["version"] = str(2)
  172. extruder_quality_changes_parser["general"]["name"] = global_quality_changes["general"]["name"]
  173. extruder_quality_changes_parser["general"]["definition"] = global_quality_changes["general"]["definition"]
  174. extruder_quality_changes_parser.add_section("metadata")
  175. extruder_quality_changes_parser["metadata"]["quality_type"] = global_quality_changes["metadata"]["quality_type"]
  176. extruder_quality_changes_parser["metadata"]["type"] = global_quality_changes["metadata"]["type"]
  177. extruder_quality_changes_parser["metadata"]["setting_version"] = str(4)
  178. extruder_quality_changes_parser["metadata"]["extruder"] = "fdmextruder"
  179. extruder_quality_changes_output = io.StringIO()
  180. extruder_quality_changes_parser.write(extruder_quality_changes_output)
  181. extruder_quality_changes_filename = quote_plus(new_filename) + ".inst.cfg"
  182. quality_changes_dir = Resources.getPath(CuraApplication.ResourceTypes.QualityInstanceContainer)
  183. with open(os.path.join(quality_changes_dir, extruder_quality_changes_filename), "w") as f:
  184. f.write(extruder_quality_changes_output.getvalue())