VersionUpgrade30to31.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. ## Gets the version number from a CFG file in Uranium's 3.0 format.
  56. #
  57. # Since the format may change, this is implemented for the 3.0 format only
  58. # and needs to be included in the version upgrade system rather than
  59. # globally in Uranium.
  60. #
  61. # \param serialised The serialised form of a CFG file.
  62. # \return The version number stored in the CFG file.
  63. # \raises ValueError The format of the version number in the file is
  64. # incorrect.
  65. # \raises KeyError The format of the file is incorrect.
  66. def getCfgVersion(self, serialised: str) -> int:
  67. parser = configparser.ConfigParser(interpolation = None)
  68. parser.read_string(serialised)
  69. format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  70. setting_version = int(parser.get("metadata", "setting_version", fallback = "0"))
  71. return format_version * 1000000 + setting_version
  72. ## Upgrades a preferences file from version 3.0 to 3.1.
  73. #
  74. # \param serialised The serialised form of a preferences file.
  75. # \param filename The name of the file to upgrade.
  76. def upgradePreferences(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  77. parser = configparser.ConfigParser(interpolation = None)
  78. parser.read_string(serialised)
  79. # Update version numbers
  80. if "general" not in parser:
  81. parser["general"] = {}
  82. parser["general"]["version"] = "5"
  83. if "metadata" not in parser:
  84. parser["metadata"] = {}
  85. parser["metadata"]["setting_version"] = "4"
  86. # Re-serialise the file.
  87. output = io.StringIO()
  88. parser.write(output)
  89. return [filename], [output.getvalue()]
  90. ## Upgrades the given instance container file from version 3.0 to 3.1.
  91. #
  92. # \param serialised The serialised form of the container file.
  93. # \param filename The name of the file to upgrade.
  94. def upgradeInstanceContainer(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  95. parser = configparser.ConfigParser(interpolation = None)
  96. parser.read_string(serialised)
  97. for each_section in ("general", "metadata"):
  98. if not parser.has_section(each_section):
  99. parser.add_section(each_section)
  100. # Check renamed definitions
  101. if "definition" in parser["general"] and parser["general"]["definition"] in _RENAMED_DEFINITION_DICT:
  102. parser["general"]["definition"] = _RENAMED_DEFINITION_DICT[parser["general"]["definition"]]
  103. # Update version numbers
  104. parser["general"]["version"] = "2"
  105. parser["metadata"]["setting_version"] = "4"
  106. # Re-serialise the file.
  107. output = io.StringIO()
  108. parser.write(output)
  109. return [filename], [output.getvalue()]
  110. ## Upgrades a container stack from version 3.0 to 3.1.
  111. #
  112. # \param serialised The serialised form of a container stack.
  113. # \param filename The name of the file to upgrade.
  114. def upgradeStack(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  115. parser = configparser.ConfigParser(interpolation = None)
  116. parser.read_string(serialised)
  117. for each_section in ("general", "metadata"):
  118. if not parser.has_section(each_section):
  119. parser.add_section(each_section)
  120. # change "not supported" quality profiles to empty because they no longer exist
  121. if parser.has_section("containers"):
  122. if parser.has_option("containers", "2"):
  123. quality_profile_id = parser["containers"]["2"]
  124. if quality_profile_id in _OLD_NOT_SUPPORTED_PROFILES:
  125. parser["containers"]["2"] = "empty_quality"
  126. # fix empty containers
  127. for key, specific_empty_container in _EMPTY_CONTAINER_DICT.items():
  128. if parser.has_option("containers", key) and parser["containers"][key] == "empty":
  129. parser["containers"][key] = specific_empty_container
  130. # check renamed definition
  131. if parser.has_option("containers", "6") and parser["containers"]["6"] in _RENAMED_DEFINITION_DICT:
  132. parser["containers"]["6"] = _RENAMED_DEFINITION_DICT[parser["containers"]["6"]]
  133. # Update version numbers
  134. if "general" not in parser:
  135. parser["general"] = {}
  136. parser["general"]["version"] = "3"
  137. if "metadata" not in parser:
  138. parser["metadata"] = {}
  139. parser["metadata"]["setting_version"] = "4"
  140. # Re-serialise the file.
  141. output = io.StringIO()
  142. parser.write(output)
  143. return [filename], [output.getvalue()]