VersionUpgrade48to49.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser
  4. from typing import Tuple, List
  5. import io
  6. import json
  7. from UM.VersionUpgrade import VersionUpgrade
  8. from cura.API import Account
  9. class VersionUpgrade48to49(VersionUpgrade):
  10. _moved_visibility_settings = ["top_bottom_extruder_nr", "top_bottom_thickness", "top_thickness", "top_layers",
  11. "bottom_thickness", "bottom_layers", "ironing_enabled"]
  12. def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  13. """
  14. Upgrades preferences to have the new version number.
  15. :param serialized: The original contents of the preferences file.
  16. :param filename: The file name of the preferences file.
  17. :return: A list of new file names, and a list of the new contents for
  18. those files.
  19. """
  20. parser = configparser.ConfigParser(interpolation = None)
  21. parser.read_string(serialized)
  22. # Update version number.
  23. parser["general"]["version"] = "7"
  24. # Update visibility settings to include new top_bottom category
  25. parser["general"]["visible_settings"] += ";top_bottom"
  26. if "categories_expanded" in parser["cura"] and any([setting in parser["cura"]["categories_expanded"] for setting in self._moved_visibility_settings]):
  27. parser["cura"]["categories_expanded"] += ";top_bottom"
  28. # If the account scope in 4.8 is outdated, delete it so that the user is enforced to log in again and get the
  29. # correct permissions.
  30. if "ultimaker_auth_data" in parser["general"]:
  31. ultimaker_auth_data = json.loads(parser["general"]["ultimaker_auth_data"])
  32. if set(Account.CLIENT_SCOPES.split(" ")) - set(ultimaker_auth_data["scope"].split(" ")):
  33. parser["general"]["ultimaker_auth_data"] = "{}"
  34. result = io.StringIO()
  35. parser.write(result)
  36. return [filename], [result.getvalue()]
  37. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  38. """
  39. Upgrades stacks to have the new version number.
  40. This updates the post-processing scripts with new parameters.
  41. :param serialized: The original contents of the stack.
  42. :param filename: The original file name of the stack.
  43. :return: A list of new file names, and a list of the new contents for
  44. those files.
  45. """
  46. parser = configparser.ConfigParser(interpolation = None)
  47. parser.read_string(serialized)
  48. # Update version number.
  49. if "general" not in parser:
  50. parser["general"] = {}
  51. parser["general"]["version"] = "5"
  52. # Update Display Progress on LCD script parameters if present.
  53. if "post_processing_scripts" in parser["metadata"]:
  54. new_scripts_entries = []
  55. for script_str in parser["metadata"]["post_processing_scripts"].split("\n"):
  56. if not script_str:
  57. continue
  58. script_str = script_str.replace(r"\\\n", "\n").replace(r"\\\\", "\\\\") # Unescape escape sequences.
  59. script_parser = configparser.ConfigParser(interpolation=None)
  60. script_parser.optionxform = str # type: ignore # Don't transform the setting keys as they are case-sensitive.
  61. script_parser.read_string(script_str)
  62. # Update Display Progress on LCD parameters.
  63. script_id = script_parser.sections()[0]
  64. if script_id == "DisplayProgressOnLCD":
  65. script_parser[script_id]["time_remaining_method"] = "m117" if script_parser[script_id]["time_remaining"] == "True" else "none"
  66. script_io = io.StringIO()
  67. script_parser.write(script_io)
  68. script_str = script_io.getvalue()
  69. script_str = script_str.replace("\\\\", r"\\\\").replace("\n", r"\\\n") # Escape newlines because configparser sees those as section delimiters.
  70. new_scripts_entries.append(script_str)
  71. parser["metadata"]["post_processing_scripts"] = "\n".join(new_scripts_entries)
  72. result = io.StringIO()
  73. parser.write(result)
  74. return [filename], [result.getvalue()]
  75. def upgradeSettingVisibility(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  76. """
  77. Upgrades setting visibility to have a version number and move moved settings to a different category
  78. :param serialized: The original contents of the stack.
  79. :param filename: The original file name of the stack.
  80. :return: A list of new file names, and a list of the new contents for
  81. those files.
  82. """
  83. parser = configparser.ConfigParser(interpolation = None, allow_no_value=True)
  84. parser.read_string(serialized)
  85. # add version number for the first time
  86. parser["general"]["version"] = "2"
  87. if "top_bottom" not in parser:
  88. parser["top_bottom"] = {}
  89. if "shell" in parser:
  90. for setting in parser["shell"]:
  91. if setting in self._moved_visibility_settings:
  92. parser["top_bottom"][setting] = None # type: ignore
  93. del parser["shell"][setting]
  94. result = io.StringIO()
  95. parser.write(result)
  96. return [filename], [result.getvalue()]