VersionUpgrade30to31.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. # Note that DO NOT!!! use the quality_changes returned from _getSingleExtrusionMachineQualityChanges().
  103. # Those are loaded from the hard drive which are original files that haven't been upgraded yet.
  104. # NOTE 2: The number can be 0 or 1 depends on whether you are loading it from the qualities folder or
  105. # from a project file. When you load from a project file, the custom profile may not be in cura
  106. # yet, so you will get 0.
  107. if len(all_quality_changes) <= 1 and not parser.has_option("metadata", "extruder"):
  108. self._createExtruderQualityChangesForSingleExtrusionMachine(filename, parser)
  109. # Update version numbers
  110. parser["general"]["version"] = "2"
  111. parser["metadata"]["setting_version"] = "4"
  112. # Re-serialise the file.
  113. output = io.StringIO()
  114. parser.write(output)
  115. return [filename], [output.getvalue()]
  116. ## Upgrades a container stack from version 3.0 to 3.1.
  117. #
  118. # \param serialised The serialised form of a container stack.
  119. # \param filename The name of the file to upgrade.
  120. def upgradeStack(self, serialised, filename):
  121. parser = configparser.ConfigParser(interpolation=None)
  122. parser.read_string(serialised)
  123. for each_section in ("general", "metadata"):
  124. if not parser.has_section(each_section):
  125. parser.add_section(each_section)
  126. # change "not supported" quality profiles to empty because they no longer exist
  127. if parser.has_section("containers"):
  128. if parser.has_option("containers", "2"):
  129. quality_profile_id = parser["containers"]["2"]
  130. if quality_profile_id in _OLD_NOT_SUPPORTED_PROFILES:
  131. parser["containers"]["2"] = "empty_quality"
  132. # fix empty containers
  133. for key, specific_empty_container in _EMPTY_CONTAINER_DICT.items():
  134. if parser.has_option("containers", key) and parser["containers"][key] == "empty":
  135. parser["containers"][key] = specific_empty_container
  136. # Update version numbers
  137. if "general" not in parser:
  138. parser["general"] = {}
  139. parser["general"]["version"] = "3"
  140. if "metadata" not in parser:
  141. parser["metadata"] = {}
  142. parser["metadata"]["setting_version"] = "4"
  143. # Re-serialise the file.
  144. output = io.StringIO()
  145. parser.write(output)
  146. return [filename], [output.getvalue()]
  147. def _getSingleExtrusionMachineQualityChanges(self, quality_changes_container):
  148. quality_changes_dir = Resources.getPath(CuraApplication.ResourceTypes.QualityInstanceContainer)
  149. quality_changes_containers = []
  150. for item in os.listdir(quality_changes_dir):
  151. file_path = os.path.join(quality_changes_dir, item)
  152. if not os.path.isfile(file_path):
  153. continue
  154. parser = configparser.ConfigParser(interpolation = None)
  155. try:
  156. parser.read([file_path])
  157. except:
  158. # skip, it is not a valid stack file
  159. continue
  160. if not parser.has_option("metadata", "type"):
  161. continue
  162. if "quality_changes" != parser["metadata"]["type"]:
  163. continue
  164. if not parser.has_option("general", "name"):
  165. continue
  166. if quality_changes_container["general"]["name"] != parser["general"]["name"]:
  167. continue
  168. quality_changes_containers.append(parser)
  169. return quality_changes_containers
  170. def _createExtruderQualityChangesForSingleExtrusionMachine(self, filename, global_quality_changes):
  171. suffix = "_" + quote_plus(global_quality_changes["general"]["name"].lower())
  172. machine_name = os.path.os.path.basename(filename).replace(".inst.cfg", "").replace(suffix, "")
  173. # Why is this here?!
  174. # When we load a .curaprofile file the deserialize will trigger a version upgrade, creating a dangling file.
  175. # This file can be recognized by it's lack of a machine name in the target filename.
  176. # So when we detect that situation here, we don't create the file and return.
  177. if machine_name == "":
  178. return
  179. new_filename = machine_name + "_" + "fdmextruder" + suffix
  180. extruder_quality_changes_parser = configparser.ConfigParser(interpolation = None)
  181. extruder_quality_changes_parser.add_section("general")
  182. extruder_quality_changes_parser["general"]["version"] = str(2)
  183. extruder_quality_changes_parser["general"]["name"] = global_quality_changes["general"]["name"]
  184. extruder_quality_changes_parser["general"]["definition"] = global_quality_changes["general"]["definition"]
  185. extruder_quality_changes_parser.add_section("metadata")
  186. extruder_quality_changes_parser["metadata"]["quality_type"] = global_quality_changes["metadata"]["quality_type"]
  187. extruder_quality_changes_parser["metadata"]["type"] = global_quality_changes["metadata"]["type"]
  188. extruder_quality_changes_parser["metadata"]["setting_version"] = str(4)
  189. extruder_quality_changes_parser["metadata"]["extruder"] = "fdmextruder"
  190. extruder_quality_changes_output = io.StringIO()
  191. extruder_quality_changes_parser.write(extruder_quality_changes_output)
  192. extruder_quality_changes_filename = quote_plus(new_filename) + ".inst.cfg"
  193. quality_changes_dir = Resources.getPath(CuraApplication.ResourceTypes.QualityInstanceContainer)
  194. with open(os.path.join(quality_changes_dir, extruder_quality_changes_filename), "w") as f:
  195. f.write(extruder_quality_changes_output.getvalue())