VersionUpgrade411to412.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser
  4. import io
  5. import json
  6. import os.path
  7. from typing import List, Tuple
  8. from UM.VersionUpgrade import VersionUpgrade
  9. class VersionUpgrade411to412(VersionUpgrade):
  10. """
  11. Upgrades configurations from the state they were in at version 4.11 to the
  12. state they should be in at version 4.12.
  13. """
  14. _flsun_profile_mapping = {
  15. "extra_coarse": "flsun_sr_normal",
  16. "coarse": "flsun_sr_normal",
  17. "extra_fast": "flsun_sr_normal",
  18. "draft": "flsun_sr_normal",
  19. "fast": "flsun_sr_normal",
  20. "normal": "flsun_sr_normal",
  21. "high": "flsun_sr_fine"
  22. }
  23. _flsun_quality_type_mapping = {
  24. "extra coarse": "normal",
  25. "coarse" : "normal",
  26. "verydraft" : "normal",
  27. "draft" : "normal",
  28. "fast" : "normal",
  29. "normal" : "normal",
  30. "high" : "fine"
  31. }
  32. def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  33. """
  34. Upgrades preferences to have the new version number.
  35. :param serialized: The original contents of the preferences file.
  36. :param filename: The file name of the preferences file.
  37. :return: A list of new file names, and a list of the new contents for
  38. those files.
  39. """
  40. parser = configparser.ConfigParser(interpolation = None)
  41. parser.read_string(serialized)
  42. # Update version number.
  43. parser["metadata"]["setting_version"] = "19"
  44. # If the account scope in 4.11 is outdated, delete it so that the user is enforced to log in again and get the
  45. # correct permissions.
  46. new_scopes = {"account.user.read",
  47. "drive.backup.read",
  48. "drive.backup.write",
  49. "packages.download",
  50. "packages.rating.read",
  51. "packages.rating.write",
  52. "connect.cluster.read",
  53. "connect.cluster.write",
  54. "library.project.read",
  55. "library.project.write",
  56. "cura.printjob.read",
  57. "cura.printjob.write",
  58. "cura.mesh.read",
  59. "cura.mesh.write",
  60. "cura.material.write"}
  61. if "ultimaker_auth_data" in parser["general"]:
  62. ultimaker_auth_data = json.loads(parser["general"]["ultimaker_auth_data"])
  63. if new_scopes - set(ultimaker_auth_data["scope"].split(" ")):
  64. parser["general"]["ultimaker_auth_data"] = "{}"
  65. result = io.StringIO()
  66. parser.write(result)
  67. return [filename], [result.getvalue()]
  68. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  69. """
  70. Upgrades instance containers to have the new version number.
  71. :param serialized: The original contents of the instance container.
  72. :param filename: The file name of the instance container.
  73. :return: A list of file names, and a list of the new contents for those
  74. files.
  75. """
  76. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  77. parser.read_string(serialized)
  78. # Update setting version number.
  79. if "metadata" not in parser:
  80. parser["metadata"] = {}
  81. parser["metadata"]["setting_version"] = "19"
  82. # Update user-made quality profiles of flsun_sr printers to use the flsun_sr-specific qualities instead of the
  83. # global ones as their base
  84. file_base_name = os.path.basename(filename) # Remove any path-related characters from the filename
  85. if file_base_name.startswith("flsun_sr_") and parser["metadata"].get("type") == "quality_changes":
  86. if "general" in parser and parser["general"].get("definition") == "fdmprinter":
  87. old_quality_type = parser["metadata"].get("quality_type", "normal")
  88. parser["general"]["definition"] = "flsun_sr"
  89. parser["metadata"]["quality_type"] = self._flsun_quality_type_mapping.get(old_quality_type, "normal")
  90. result = io.StringIO()
  91. parser.write(result)
  92. return [filename], [result.getvalue()]
  93. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  94. """
  95. Upgrades container stacks to have the new version number.
  96. Upgrades container stacks for FLSun Racer to change their profiles.
  97. :param serialized: The original contents of the container stack.
  98. :param filename: The file name of the container stack.
  99. :return: A list of file names, and a list of the new contents for those
  100. files.
  101. """
  102. parser = configparser.ConfigParser(interpolation = None)
  103. parser.read_string(serialized)
  104. # Update setting version number.
  105. if "metadata" not in parser:
  106. parser["metadata"] = {}
  107. parser["metadata"]["setting_version"] = "19"
  108. # Change renamed profiles.
  109. if "containers" in parser:
  110. definition_id = parser["containers"].get("7")
  111. if definition_id == "flsun_sr":
  112. if parser["metadata"].get("type", "machine") == "machine": # Only global stacks.
  113. old_quality = parser["containers"].get("3")
  114. new_quality = self._flsun_profile_mapping.get(old_quality, "flsun_sr_normal")
  115. parser["containers"]["3"] = new_quality
  116. result = io.StringIO()
  117. parser.write(result)
  118. return [filename], [result.getvalue()]