VersionUpgrade30to31.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. from UM.VersionUpgrade import VersionUpgrade #We're inheriting from this.
  6. # a list of all legacy "Not Supported" quality profiles
  7. _OLD_NOT_SUPPORTED_PROFILES = [
  8. "um2p_pp_0.25_normal",
  9. "um2p_tpu_0.8_normal",
  10. "um3_bb0.4_ABS_Fast_Print",
  11. "um3_bb0.4_ABS_Superdraft_Print",
  12. "um3_bb0.4_CPEP_Fast_Print",
  13. "um3_bb0.4_CPEP_Superdraft_Print",
  14. "um3_bb0.4_CPE_Fast_Print",
  15. "um3_bb0.4_CPE_Superdraft_Print",
  16. "um3_bb0.4_Nylon_Fast_Print",
  17. "um3_bb0.4_Nylon_Superdraft_Print",
  18. "um3_bb0.4_PC_Fast_Print",
  19. "um3_bb0.4_PLA_Fast_Print",
  20. "um3_bb0.4_PLA_Superdraft_Print",
  21. "um3_bb0.4_PP_Fast_Print",
  22. "um3_bb0.4_PP_Superdraft_Print",
  23. "um3_bb0.4_TPU_Fast_Print",
  24. "um3_bb0.4_TPU_Superdraft_Print",
  25. "um3_bb0.8_ABS_Fast_Print",
  26. "um3_bb0.8_ABS_Superdraft_Print",
  27. "um3_bb0.8_CPEP_Fast_Print",
  28. "um3_bb0.8_CPEP_Superdraft_Print",
  29. "um3_bb0.8_CPE_Fast_Print",
  30. "um3_bb0.8_CPE_Superdraft_Print",
  31. "um3_bb0.8_Nylon_Fast_Print",
  32. "um3_bb0.8_Nylon_Superdraft_Print",
  33. "um3_bb0.8_PC_Fast_Print",
  34. "um3_bb0.8_PC_Superdraft_Print",
  35. "um3_bb0.8_PLA_Fast_Print",
  36. "um3_bb0.8_PLA_Superdraft_Print",
  37. "um3_bb0.8_PP_Fast_Print",
  38. "um3_bb0.8_PP_Superdraft_Print",
  39. "um3_bb0.8_TPU_Fast_print",
  40. "um3_bb0.8_TPU_Superdraft_Print",
  41. ]
  42. # Some containers have their specific empty containers, those need to be set correctly.
  43. _EMPTY_CONTAINER_DICT = {
  44. "1": "empty_quality_changes",
  45. "2": "empty_quality",
  46. "3": "empty_material",
  47. "4": "empty_variant",
  48. }
  49. # Renamed definition files
  50. _RENAMED_DEFINITION_DICT = {
  51. "jellybox": "imade3d_jellybox",
  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. # Check renamed definitions
  100. if "definition" in parser["general"] and parser["general"]["definition"] in _RENAMED_DEFINITION_DICT:
  101. parser["general"]["definition"] = _RENAMED_DEFINITION_DICT[parser["general"]["definition"]]
  102. # Update version numbers
  103. parser["general"]["version"] = "2"
  104. parser["metadata"]["setting_version"] = "4"
  105. # Re-serialise the file.
  106. output = io.StringIO()
  107. parser.write(output)
  108. return [filename], [output.getvalue()]
  109. ## Upgrades a container stack from version 3.0 to 3.1.
  110. #
  111. # \param serialised The serialised form of a container stack.
  112. # \param filename The name of the file to upgrade.
  113. def upgradeStack(self, serialised, filename):
  114. parser = configparser.ConfigParser(interpolation=None)
  115. parser.read_string(serialised)
  116. for each_section in ("general", "metadata"):
  117. if not parser.has_section(each_section):
  118. parser.add_section(each_section)
  119. # change "not supported" quality profiles to empty because they no longer exist
  120. if parser.has_section("containers"):
  121. if parser.has_option("containers", "2"):
  122. quality_profile_id = parser["containers"]["2"]
  123. if quality_profile_id in _OLD_NOT_SUPPORTED_PROFILES:
  124. parser["containers"]["2"] = "empty_quality"
  125. # fix empty containers
  126. for key, specific_empty_container in _EMPTY_CONTAINER_DICT.items():
  127. if parser.has_option("containers", key) and parser["containers"][key] == "empty":
  128. parser["containers"][key] = specific_empty_container
  129. # check renamed definition
  130. if parser.has_option("containers", "6") and parser["containers"]["6"] in _RENAMED_DEFINITION_DICT:
  131. parser["containers"]["6"] = _RENAMED_DEFINITION_DICT[parser["containers"]["6"]]
  132. # Update version numbers
  133. if "general" not in parser:
  134. parser["general"] = {}
  135. parser["general"]["version"] = "3"
  136. if "metadata" not in parser:
  137. parser["metadata"] = {}
  138. parser["metadata"]["setting_version"] = "4"
  139. # Re-serialise the file.
  140. output = io.StringIO()
  141. parser.write(output)
  142. return [filename], [output.getvalue()]