VersionUpgrade26to27.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser #To parse the files we need to upgrade and write the new files.
  4. import io #To serialise configparser output to a string.
  5. from typing import Dict, List, Tuple
  6. from UM.VersionUpgrade import VersionUpgrade
  7. # a dict of renamed quality profiles: <old_id> : <new_id>
  8. _renamed_quality_profiles = {
  9. "um3_aa0.4_PVA_Not_Supported_Quality": "um3_aa0.4_PVA_Fast_Print",
  10. "um3_aa0.8_CPEP_Not_Supported_Quality": "um3_aa0.8_CPEP_Fast_Print",
  11. "um3_aa0.8_CPEP_Not_Supported_Superdraft_Quality": "um3_aa0.8_CPEP_Superdraft_Print",
  12. "um3_aa0.8_CPEP_Not_Supported_Verydraft_Quality": "um3_aa0.8_CPEP_Verydraft_Print",
  13. "um3_aa0.8_PC_Not_Supported_Quality": "um3_aa0.8_PC_Fast_Print",
  14. "um3_aa0.8_PC_Not_Supported_Superdraft_Quality": "um3_aa0.8_PC_Superdraft_Print",
  15. "um3_aa0.8_PC_Not_Supported_Verydraft_Quality": "um3_aa0.8_PC_Verydraft_Print",
  16. "um3_aa0.8_PVA_Not_Supported_Quality": "um3_aa0.8_PVA_Fast_Print",
  17. "um3_aa0.8_PVA_Not_Supported_Superdraft_Quality": "um3_aa0.8_PVA_Superdraft_Print",
  18. "um3_bb0.4_ABS_Not_Supported_Quality": "um3_bb0.4_ABS_Fast_print",
  19. "um3_bb0.4_ABS_Not_Supported_Superdraft_Quality": "um3_bb0.4_ABS_Superdraft_Print",
  20. "um3_bb0.4_CPE_Not_Supported_Quality": "um3_bb0.4_CPE_Fast_Print",
  21. "um3_bb0.4_CPE_Not_Supported_Superdraft_Quality": "um3_bb0.4_CPE_Superdraft_Print",
  22. "um3_bb0.4_CPEP_Not_Supported_Quality": "um3_bb0.4_CPEP_Fast_Print",
  23. "um3_bb0.4_CPEP_Not_Supported_Superdraft_Quality": "um3_bb0.4_CPEP_Superdraft_Print",
  24. "um3_bb0.4_Nylon_Not_Supported_Quality": "um3_bb0.4_Nylon_Fast_Print",
  25. "um3_bb0.4_Nylon_Not_Supported_Superdraft_Quality": "um3_bb0.4_Nylon_Superdraft_Print",
  26. "um3_bb0.4_PC_Not_Supported_Quality": "um3_bb0.4_PC_Fast_Print",
  27. "um3_bb0.4_PLA_Not_Supported_Quality": "um3_bb0.4_PLA_Fast_Print",
  28. "um3_bb0.4_PLA_Not_Supported_Superdraft_Quality": "um3_bb0.4_PLA_Superdraft_Print",
  29. "um3_bb0.4_TPU_Not_Supported_Quality": "um3_bb0.4_TPU_Fast_Print",
  30. "um3_bb0.4_TPU_Not_Supported_Superdraft_Quality": "um3_bb0.4_TPU_Superdraft_Print",
  31. "um3_bb0.8_ABS_Not_Supported_Quality": "um3_bb0.8_ABS_Fast_Print",
  32. "um3_bb0.8_ABS_Not_Supported_Superdraft_Quality": "um3_bb0.8_ABS_Superdraft_Print",
  33. "um3_bb0.8_CPE_Not_Supported_Quality": "um3_bb0.8_CPE_Fast_Print",
  34. "um3_bb0.8_CPE_Not_Supported_Superdraft_Quality": "um3_bb0.8_CPE_Superdraft_Print",
  35. "um3_bb0.8_CPEP_Not_Supported_Quality": "um3_bb0.um3_bb0.8_CPEP_Fast_Print",
  36. "um3_bb0.8_CPEP_Not_Supported_Superdraft_Quality": "um3_bb0.8_CPEP_Superdraft_Print",
  37. "um3_bb0.8_Nylon_Not_Supported_Quality": "um3_bb0.8_Nylon_Fast_Print",
  38. "um3_bb0.8_Nylon_Not_Supported_Superdraft_Quality": "um3_bb0.8_Nylon_Superdraft_Print",
  39. "um3_bb0.8_PC_Not_Supported_Quality": "um3_bb0.8_PC_Fast_Print",
  40. "um3_bb0.8_PC_Not_Supported_Superdraft_Quality": "um3_bb0.8_PC_Superdraft_Print",
  41. "um3_bb0.8_PLA_Not_Supported_Quality": "um3_bb0.8_PLA_Fast_Print",
  42. "um3_bb0.8_PLA_Not_Supported_Superdraft_Quality": "um3_bb0.8_PLA_Superdraft_Print",
  43. "um3_bb0.8_TPU_Not_Supported_Quality": "um3_bb0.8_TPU_Fast_print",
  44. "um3_bb0.8_TPU_Not_Supported_Superdraft_Quality": "um3_bb0.8_TPU_Superdraft_Print",
  45. } # type: Dict[str, str]
  46. ## A collection of functions that convert the configuration of the user in Cura
  47. # 2.6 to a configuration for Cura 2.7.
  48. #
  49. # All of these methods are essentially stateless.
  50. class VersionUpgrade26to27(VersionUpgrade):
  51. ## Gets the version number from a CFG file in Uranium's 2.6 format.
  52. #
  53. # Since the format may change, this is implemented for the 2.6 format only
  54. # and needs to be included in the version upgrade system rather than
  55. # globally in Uranium.
  56. #
  57. # \param serialised The serialised form of a CFG file.
  58. # \return The version number stored in the CFG file.
  59. # \raises ValueError The format of the version number in the file is
  60. # incorrect.
  61. # \raises KeyError The format of the file is incorrect.
  62. def getCfgVersion(self, serialised: str) -> int:
  63. parser = configparser.ConfigParser(interpolation = None)
  64. parser.read_string(serialised)
  65. format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  66. setting_version = int(parser.get("metadata", "setting_version", fallback = "0"))
  67. return format_version * 1000000 + setting_version
  68. ## Upgrades a preferences file from version 2.6 to 2.7.
  69. #
  70. # \param serialised The serialised form of a preferences file.
  71. # \param filename The name of the file to upgrade.
  72. def upgradePreferences(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  73. parser = configparser.ConfigParser(interpolation = None)
  74. parser.read_string(serialised)
  75. # Update version numbers
  76. if "general" not in parser:
  77. parser["general"] = {}
  78. parser["general"]["version"] = "4"
  79. if "metadata" not in parser:
  80. parser["metadata"] = {}
  81. parser["metadata"]["setting_version"] = "2"
  82. #Renamed setting value for g-code flavour.
  83. if "values" in parser and "machine_gcode_flavor" in parser["values"]:
  84. if parser["values"]["machine_gcode_flavor"] == "RepRap (Volumatric)":
  85. parser["values"]["machine_gcode_flavor"] = "RepRap (Volumetric)"
  86. # Re-serialise the file.
  87. output = io.StringIO()
  88. parser.write(output)
  89. return [filename], [output.getvalue()]
  90. ## Upgrades a container file other than a container stack file from version 2.6 to 2.7.
  91. #
  92. # \param serialised The serialised form of a container file.
  93. # \param filename The name of the file to upgrade.
  94. def upgradeOtherContainer(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  95. parser = configparser.ConfigParser(interpolation = None)
  96. parser.read_string(serialised)
  97. # Update version numbers
  98. if "general" not in parser:
  99. parser["general"] = {}
  100. parser["general"]["version"] = "2"
  101. if "metadata" not in parser:
  102. parser["metadata"] = {}
  103. parser["metadata"]["setting_version"] = "2"
  104. # Re-serialise the file.
  105. output = io.StringIO()
  106. parser.write(output)
  107. return [filename], [output.getvalue()]
  108. ## Upgrades a container stack from version 2.6 to 2.7.
  109. #
  110. # \param serialised The serialised form of a container stack.
  111. # \param filename The name of the file to upgrade.
  112. def upgradeStack(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  113. parser = configparser.ConfigParser(interpolation = None)
  114. parser.read_string(serialised)
  115. # Update IDs of the renamed quality profiles
  116. if parser.has_section("containers"):
  117. key_list = [key for key in parser["containers"].keys()]
  118. for key in key_list:
  119. container_id = parser.get("containers", key)
  120. new_id = _renamed_quality_profiles.get(container_id)
  121. if new_id is not None:
  122. parser.set("containers", key, new_id)
  123. if "6" not in parser["containers"]:
  124. parser["containers"]["6"] = parser["containers"]["5"]
  125. parser["containers"]["5"] = "empty"
  126. for each_section in ("general", "metadata"):
  127. if not parser.has_section(each_section):
  128. parser.add_section(each_section)
  129. # Update version numbers
  130. if "general" not in parser:
  131. parser["general"] = {}
  132. parser["general"]["version"] = "3"
  133. if "metadata" not in parser:
  134. parser["metadata"] = {}
  135. parser["metadata"]["setting_version"] = "2"
  136. # Re-serialise the file.
  137. output = io.StringIO()
  138. parser.write(output)
  139. return [filename], [output.getvalue()]