VersionUpgrade49to410.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser
  4. import io
  5. import os.path # To get the file ID.
  6. from typing import Dict, List, Tuple
  7. from UM.VersionUpgrade import VersionUpgrade
  8. class VersionUpgrade49to410(VersionUpgrade):
  9. """
  10. Upgrades configurations from the state they were in at version 4.9 to the state they should be in at version 4.10.
  11. """
  12. _renamed_profiles = {
  13. # Definitions.
  14. "twotrees_bluer" : "two_trees_bluer",
  15. # Upgrade for people who had the original TwoTrees Bluer profiles from 4.9 and earlier.
  16. "twotrees_bluer_extruder_0": "two_trees_base_extruder_0",
  17. "twotrees_bluer_extruder_1": "two_trees_base_extruder_0"
  18. }
  19. _quality_changes_to_two_trees_base = {
  20. "twotrees_bluer_extruder_0",
  21. "twotrees_bluer_extruder_1",
  22. "twotrees_bluer"
  23. }
  24. # Default variant to select for legacy TwoTrees printers, now that we have variants.
  25. _default_variants = {
  26. "twotrees_bluer_extruder_0" : "two_trees_bluer_0.4",
  27. "twotrees_bluer_extruder_1" : "two_trees_bluer_0.4"
  28. }
  29. _two_trees_bluer_quality_type_conversion = {
  30. "high" : "ultra",
  31. "normal" : "super",
  32. "fast" : "standard",
  33. "draft" : "standard",
  34. "extra_fast" : "low",
  35. "coarse" : "draft",
  36. "extra_coarse": "draft"
  37. }
  38. _two_trees_quality_per_material = {
  39. # Since legacy TwoTrees printers didn't have different variants, we always pick the 0.4mm variant.
  40. "generic_abs_175" : {
  41. "high" : "two_trees_0.4_ABS_super",
  42. "normal" : "two_trees_0.4_ABS_super",
  43. "fast" : "two_trees_0.4_ABS_super",
  44. "draft" : "two_trees_0.4_ABS_standard",
  45. "extra_fast" : "two_trees_0.4_ABS_low",
  46. "coarse" : "two_trees_0.4_ABS_low",
  47. "extra_coarse": "two_trees_0.4_ABS_low"
  48. },
  49. "generic_petg_175": {
  50. "high" : "two_trees_0.4_PETG_super",
  51. "normal" : "two_trees_0.4_PETG_super",
  52. "fast" : "two_trees_0.4_PETG_super",
  53. "draft" : "two_trees_0.4_PETG_standard",
  54. "extra_fast" : "two_trees_0.4_PETG_low",
  55. "coarse" : "two_trees_0.4_PETG_low",
  56. "extra_coarse": "two_trees_0.4_PETG_low"
  57. },
  58. "generic_pla_175" : {
  59. "high" : "two_trees_0.4_PLA_super",
  60. "normal" : "two_trees_0.4_PLA_super",
  61. "fast" : "two_trees_0.4_PLA_super",
  62. "draft" : "two_trees_0.4_PLA_standard",
  63. "extra_fast" : "two_trees_0.4_PLA_low",
  64. "coarse" : "two_trees_0.4_PLA_low",
  65. "extra_coarse": "two_trees_0.4_PLA_low"
  66. },
  67. "generic_tpu_175" : {
  68. "high" : "two_trees_0.4_TPU_super",
  69. "normal" : "two_trees_0.4_TPU_super",
  70. "fast" : "two_trees_0.4_TPU_super",
  71. "draft" : "two_trees_0.4_TPU_standard",
  72. "extra_fast" : "two_trees_0.4_TPU_standard",
  73. "coarse" : "two_trees_0.4_TPU_standard",
  74. "extra_coarse": "two_trees_0.4_TPU_standard"
  75. },
  76. "empty_material" : { # For the global stack.
  77. "high" : "two_trees_global_super",
  78. "normal" : "two_trees_global_super",
  79. "fast" : "two_trees_global_super",
  80. "draft" : "two_trees_global_standard",
  81. "extra_fast" : "two_trees_global_low",
  82. "coarse" : "two_trees_global_low",
  83. "extra_coarse": "two_trees_global_low"
  84. }
  85. }
  86. _deltacomb_quality_type_conversion = {
  87. "a" : "D005",
  88. "b" : "D010",
  89. "c" : "D015",
  90. "d" : "D020",
  91. "e" : "D030",
  92. "f" : "D045",
  93. "g" : "D060"
  94. }
  95. def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  96. """
  97. Upgrades preferences to have the new version number.
  98. :param serialized: The original contents of the preferences file.
  99. :param filename: The file name of the preferences file.
  100. :return: A list of new file names, and a list of the new contents for
  101. those files.
  102. """
  103. parser = configparser.ConfigParser(interpolation = None)
  104. parser.read_string(serialized)
  105. # Update version number.
  106. parser["metadata"]["setting_version"] = "17"
  107. result = io.StringIO()
  108. parser.write(result)
  109. return [filename], [result.getvalue()]
  110. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  111. """Upgrades instance containers to have the new version number.
  112. This renames the renamed settings in the containers.
  113. """
  114. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  115. parser.read_string(serialized)
  116. # Update setting version number.
  117. if "metadata" not in parser:
  118. parser["metadata"] = {}
  119. parser["metadata"]["setting_version"] = "17"
  120. if "general" not in parser:
  121. parser["general"] = {}
  122. # Certain instance containers (such as definition changes) reference to a certain definition container
  123. # Since a number of those changed name, we also need to update those.
  124. old_definition = parser.get("general", "definition", fallback = "")
  125. if old_definition in self._renamed_profiles:
  126. parser["general"]["definition"] = self._renamed_profiles[old_definition]
  127. # For quality-changes profiles made for TwoTrees Bluer printers, change the definition to the two_trees_base and make sure that the quality is something we have a profile for.
  128. if parser.get("metadata", "type", fallback = "") == "quality_changes":
  129. for possible_printer in self._quality_changes_to_two_trees_base:
  130. if os.path.basename(filename).startswith(possible_printer + "_"):
  131. parser["general"]["definition"] = "two_trees_base"
  132. parser["metadata"]["quality_type"] = self._two_trees_bluer_quality_type_conversion.get(parser.get("metadata", "quality_type", fallback = "fast"), "standard")
  133. break
  134. if os.path.basename(filename).startswith("deltacomb_"):
  135. parser["general"]["definition"] = "deltacomb_base"
  136. parser["metadata"]["quality_type"] = self._deltacomb_quality_type_conversion.get(parser.get("metadata", "quality_type", fallback = "c"), "D015")
  137. break
  138. result = io.StringIO()
  139. parser.write(result)
  140. return [filename], [result.getvalue()]
  141. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  142. """Upgrades stacks to have the new version number."""
  143. parser = configparser.ConfigParser(interpolation = None)
  144. parser.read_string(serialized)
  145. # Update setting version number.
  146. if "metadata" not in parser:
  147. parser["metadata"] = {}
  148. parser["metadata"]["setting_version"] = "17"
  149. # Change renamed profiles.
  150. if "containers" in parser:
  151. definition_id = parser["containers"]["7"]
  152. if definition_id in self._quality_changes_to_two_trees_base:
  153. material_id = parser["containers"]["4"]
  154. old_quality_id = parser["containers"]["3"]
  155. if parser["metadata"].get("type", "machine") == "extruder_train":
  156. if parser["containers"]["5"] == "empty_variant":
  157. if definition_id in self._default_variants:
  158. parser["containers"]["5"] = self._default_variants[definition_id] # For legacy TwoTrees printers, change the variant to 0.4.
  159. # Also change the quality to go along with it.
  160. if material_id in self._two_trees_quality_per_material and old_quality_id in self._two_trees_quality_per_material[material_id]:
  161. parser["containers"]["3"] = self._two_trees_quality_per_material[material_id][old_quality_id]
  162. stack_copy = {} # type: Dict[str, str] # Make a copy so that we don't modify the dict we're iterating over.
  163. stack_copy.update(parser["containers"])
  164. for position, profile_id in stack_copy.items():
  165. if profile_id in self._renamed_profiles:
  166. parser["containers"][position] = self._renamed_profiles[profile_id]
  167. result = io.StringIO()
  168. parser.write(result)
  169. return [filename], [result.getvalue()]