VersionUpgrade30to31.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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, Set, Tuple
  6. from UM.VersionUpgrade import VersionUpgrade #We're inheriting from this.
  7. # a list of all legacy "Not Supported" quality profiles
  8. _OLD_NOT_SUPPORTED_PROFILES = {
  9. "um2p_pp_0.25_normal",
  10. "um2p_tpu_0.8_normal",
  11. "um3_bb0.4_ABS_Fast_Print",
  12. "um3_bb0.4_ABS_Superdraft_Print",
  13. "um3_bb0.4_CPEP_Fast_Print",
  14. "um3_bb0.4_CPEP_Superdraft_Print",
  15. "um3_bb0.4_CPE_Fast_Print",
  16. "um3_bb0.4_CPE_Superdraft_Print",
  17. "um3_bb0.4_Nylon_Fast_Print",
  18. "um3_bb0.4_Nylon_Superdraft_Print",
  19. "um3_bb0.4_PC_Fast_Print",
  20. "um3_bb0.4_PLA_Fast_Print",
  21. "um3_bb0.4_PLA_Superdraft_Print",
  22. "um3_bb0.4_PP_Fast_Print",
  23. "um3_bb0.4_PP_Superdraft_Print",
  24. "um3_bb0.4_TPU_Fast_Print",
  25. "um3_bb0.4_TPU_Superdraft_Print",
  26. "um3_bb0.8_ABS_Fast_Print",
  27. "um3_bb0.8_ABS_Superdraft_Print",
  28. "um3_bb0.8_CPEP_Fast_Print",
  29. "um3_bb0.8_CPEP_Superdraft_Print",
  30. "um3_bb0.8_CPE_Fast_Print",
  31. "um3_bb0.8_CPE_Superdraft_Print",
  32. "um3_bb0.8_Nylon_Fast_Print",
  33. "um3_bb0.8_Nylon_Superdraft_Print",
  34. "um3_bb0.8_PC_Fast_Print",
  35. "um3_bb0.8_PC_Superdraft_Print",
  36. "um3_bb0.8_PLA_Fast_Print",
  37. "um3_bb0.8_PLA_Superdraft_Print",
  38. "um3_bb0.8_PP_Fast_Print",
  39. "um3_bb0.8_PP_Superdraft_Print",
  40. "um3_bb0.8_TPU_Fast_print",
  41. "um3_bb0.8_TPU_Superdraft_Print",
  42. } # type: Set[str]
  43. # Some containers have their specific empty containers, those need to be set correctly.
  44. _EMPTY_CONTAINER_DICT = {
  45. "1": "empty_quality_changes",
  46. "2": "empty_quality",
  47. "3": "empty_material",
  48. "4": "empty_variant",
  49. } # type: Dict[str, str]
  50. # Renamed definition files
  51. _RENAMED_DEFINITION_DICT = {
  52. "jellybox": "imade3d_jellybox",
  53. } # type: Dict[str, str]
  54. class VersionUpgrade30to31(VersionUpgrade):
  55. ## Upgrades a preferences file from version 3.0 to 3.1.
  56. #
  57. # \param serialised The serialised form of a preferences file.
  58. # \param filename The name of the file to upgrade.
  59. def upgradePreferences(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  60. parser = configparser.ConfigParser(interpolation = None)
  61. parser.read_string(serialised)
  62. # Update version numbers
  63. if "general" not in parser:
  64. parser["general"] = {}
  65. parser["general"]["version"] = "5"
  66. if "metadata" not in parser:
  67. parser["metadata"] = {}
  68. parser["metadata"]["setting_version"] = "4"
  69. # Re-serialise the file.
  70. output = io.StringIO()
  71. parser.write(output)
  72. return [filename], [output.getvalue()]
  73. ## Upgrades the given instance container file from version 3.0 to 3.1.
  74. #
  75. # \param serialised The serialised form of the container file.
  76. # \param filename The name of the file to upgrade.
  77. def upgradeInstanceContainer(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  78. parser = configparser.ConfigParser(interpolation = None)
  79. parser.read_string(serialised)
  80. for each_section in ("general", "metadata"):
  81. if not parser.has_section(each_section):
  82. parser.add_section(each_section)
  83. # Check renamed definitions
  84. if "definition" in parser["general"] and parser["general"]["definition"] in _RENAMED_DEFINITION_DICT:
  85. parser["general"]["definition"] = _RENAMED_DEFINITION_DICT[parser["general"]["definition"]]
  86. # Update version numbers
  87. parser["general"]["version"] = "2"
  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 a container stack from version 3.0 to 3.1.
  94. #
  95. # \param serialised The serialised form of a container stack.
  96. # \param filename The name of the file to upgrade.
  97. def upgradeStack(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  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. # change "not supported" quality profiles to empty because they no longer exist
  104. if parser.has_section("containers"):
  105. if parser.has_option("containers", "2"):
  106. quality_profile_id = parser["containers"]["2"]
  107. if quality_profile_id in _OLD_NOT_SUPPORTED_PROFILES:
  108. parser["containers"]["2"] = "empty_quality"
  109. # fix empty containers
  110. for key, specific_empty_container in _EMPTY_CONTAINER_DICT.items():
  111. if parser.has_option("containers", key) and parser["containers"][key] == "empty":
  112. parser["containers"][key] = specific_empty_container
  113. # check renamed definition
  114. if parser.has_option("containers", "6") and parser["containers"]["6"] in _RENAMED_DEFINITION_DICT:
  115. parser["containers"]["6"] = _RENAMED_DEFINITION_DICT[parser["containers"]["6"]]
  116. # Update version numbers
  117. if "general" not in parser:
  118. parser["general"] = {}
  119. parser["general"]["version"] = "3"
  120. if "metadata" not in parser:
  121. parser["metadata"] = {}
  122. parser["metadata"]["setting_version"] = "4"
  123. # Re-serialise the file.
  124. output = io.StringIO()
  125. parser.write(output)
  126. return [filename], [output.getvalue()]