VersionUpgrade30to31.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. # Renamed definition files
  54. _RENAMED_DEFINITION_DICT = {
  55. "jellybox": "imade3d_jellybox",
  56. }
  57. class VersionUpgrade30to31(VersionUpgrade):
  58. ## Gets the version number from a CFG file in Uranium's 3.0 format.
  59. #
  60. # Since the format may change, this is implemented for the 3.0 format only
  61. # and needs to be included in the version upgrade system rather than
  62. # globally in Uranium.
  63. #
  64. # \param serialised The serialised form of a CFG file.
  65. # \return The version number stored in the CFG file.
  66. # \raises ValueError The format of the version number in the file is
  67. # incorrect.
  68. # \raises KeyError The format of the file is incorrect.
  69. def getCfgVersion(self, serialised):
  70. parser = configparser.ConfigParser(interpolation = None)
  71. parser.read_string(serialised)
  72. format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  73. setting_version = int(parser.get("metadata", "setting_version", fallback = 0))
  74. return format_version * 1000000 + setting_version
  75. ## Upgrades a preferences file from version 3.0 to 3.1.
  76. #
  77. # \param serialised The serialised form of a preferences file.
  78. # \param filename The name of the file to upgrade.
  79. def upgradePreferences(self, serialised, filename):
  80. parser = configparser.ConfigParser(interpolation=None)
  81. parser.read_string(serialised)
  82. # Update version numbers
  83. if "general" not in parser:
  84. parser["general"] = {}
  85. parser["general"]["version"] = "5"
  86. if "metadata" not in parser:
  87. parser["metadata"] = {}
  88. parser["metadata"]["setting_version"] = "4"
  89. # Re-serialise the file.
  90. output = io.StringIO()
  91. parser.write(output)
  92. return [filename], [output.getvalue()]
  93. ## Upgrades the given instance container file from version 3.0 to 3.1.
  94. #
  95. # \param serialised The serialised form of the container file.
  96. # \param filename The name of the file to upgrade.
  97. def upgradeInstanceContainer(self, serialised, filename):
  98. parser = configparser.ConfigParser(interpolation=None)
  99. parser.read_string(serialised)
  100. for each_section in ("general", "metadata"):
  101. if not parser.has_section(each_section):
  102. parser.add_section(each_section)
  103. # Check renamed definitions
  104. if "definition" in parser["general"] and parser["general"]["definition"] in _RENAMED_DEFINITION_DICT:
  105. parser["general"]["definition"] = _RENAMED_DEFINITION_DICT[parser["general"]["definition"]]
  106. # Update version numbers
  107. parser["general"]["version"] = "2"
  108. parser["metadata"]["setting_version"] = "4"
  109. # Re-serialise the file.
  110. output = io.StringIO()
  111. parser.write(output)
  112. return [filename], [output.getvalue()]
  113. ## Upgrades a container stack from version 3.0 to 3.1.
  114. #
  115. # \param serialised The serialised form of a container stack.
  116. # \param filename The name of the file to upgrade.
  117. def upgradeStack(self, serialised, filename):
  118. parser = configparser.ConfigParser(interpolation=None)
  119. parser.read_string(serialised)
  120. for each_section in ("general", "metadata"):
  121. if not parser.has_section(each_section):
  122. parser.add_section(each_section)
  123. # change "not supported" quality profiles to empty because they no longer exist
  124. if parser.has_section("containers"):
  125. if parser.has_option("containers", "2"):
  126. quality_profile_id = parser["containers"]["2"]
  127. if quality_profile_id in _OLD_NOT_SUPPORTED_PROFILES:
  128. parser["containers"]["2"] = "empty_quality"
  129. # fix empty containers
  130. for key, specific_empty_container in _EMPTY_CONTAINER_DICT.items():
  131. if parser.has_option("containers", key) and parser["containers"][key] == "empty":
  132. parser["containers"][key] = specific_empty_container
  133. # check renamed definition
  134. if parser.has_option("containers", "6") and parser["containers"]["6"] in _RENAMED_DEFINITION_DICT:
  135. parser["containers"]["6"] = _RENAMED_DEFINITION_DICT[parser["containers"]["6"]]
  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. # check renamed definition
  186. if extruder_quality_changes_parser["general"]["definition"] in _RENAMED_DEFINITION_DICT:
  187. extruder_quality_changes_parser["general"]["definition"] = _RENAMED_DEFINITION_DICT[extruder_quality_changes_parser["general"]["definition"]]
  188. extruder_quality_changes_parser.add_section("metadata")
  189. extruder_quality_changes_parser["metadata"]["quality_type"] = global_quality_changes["metadata"]["quality_type"]
  190. extruder_quality_changes_parser["metadata"]["type"] = global_quality_changes["metadata"]["type"]
  191. extruder_quality_changes_parser["metadata"]["setting_version"] = str(4)
  192. extruder_quality_changes_parser["metadata"]["extruder"] = "fdmextruder"
  193. extruder_quality_changes_output = io.StringIO()
  194. extruder_quality_changes_parser.write(extruder_quality_changes_output)
  195. extruder_quality_changes_filename = quote_plus(new_filename) + ".inst.cfg"
  196. quality_changes_dir = Resources.getPath(CuraApplication.ResourceTypes.QualityInstanceContainer)
  197. with open(os.path.join(quality_changes_dir, extruder_quality_changes_filename), "w", encoding = "utf-8") as f:
  198. f.write(extruder_quality_changes_output.getvalue())