VersionUpgrade48to49.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. if "visible_settings" in parser["general"]:
  26. parser["general"]["visible_settings"] += ";top_bottom"
  27. if "cura" in parser and "categories_expanded" in parser["cura"] and any([setting in parser["cura"]["categories_expanded"] for setting in self._moved_visibility_settings]):
  28. parser["cura"]["categories_expanded"] += ";top_bottom"
  29. # If the account scope in 4.8 is outdated, delete it so that the user is enforced to log in again and get the
  30. # correct permissions.
  31. if "ultimaker_auth_data" in parser["general"]:
  32. ultimaker_auth_data = json.loads(parser["general"]["ultimaker_auth_data"])
  33. if set(Account.CLIENT_SCOPES.split(" ")) - set(ultimaker_auth_data["scope"].split(" ")):
  34. parser["general"]["ultimaker_auth_data"] = "{}"
  35. result = io.StringIO()
  36. parser.write(result)
  37. return [filename], [result.getvalue()]
  38. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  39. """
  40. Upgrades stacks to have the new version number.
  41. This updates the post-processing scripts with new parameters.
  42. :param serialized: The original contents of the stack.
  43. :param filename: The original file name of the stack.
  44. :return: A list of new file names, and a list of the new contents for
  45. those files.
  46. """
  47. parser = configparser.ConfigParser(interpolation = None)
  48. parser.read_string(serialized)
  49. # Update version number.
  50. if "general" not in parser:
  51. parser["general"] = {}
  52. parser["general"]["version"] = "5"
  53. # Update Display Progress on LCD script parameters if present.
  54. if "post_processing_scripts" in parser["metadata"]:
  55. new_scripts_entries = []
  56. for script_str in parser["metadata"]["post_processing_scripts"].split("\n"):
  57. if not script_str:
  58. continue
  59. script_str = script_str.replace(r"\\\n", "\n").replace(r"\\\\", "\\\\") # Unescape escape sequences.
  60. script_parser = configparser.ConfigParser(interpolation=None)
  61. script_parser.optionxform = str # type: ignore # Don't transform the setting keys as they are case-sensitive.
  62. script_parser.read_string(script_str)
  63. # Update Display Progress on LCD parameters.
  64. script_id = script_parser.sections()[0]
  65. if script_id == "DisplayProgressOnLCD":
  66. script_parser[script_id]["time_remaining_method"] = "m117" if script_parser[script_id]["time_remaining"] == "True" else "none"
  67. script_io = io.StringIO()
  68. script_parser.write(script_io)
  69. script_str = script_io.getvalue()
  70. script_str = script_str.replace("\\\\", r"\\\\").replace("\n", r"\\\n") # Escape newlines because configparser sees those as section delimiters.
  71. new_scripts_entries.append(script_str)
  72. parser["metadata"]["post_processing_scripts"] = "\n".join(new_scripts_entries)
  73. result = io.StringIO()
  74. parser.write(result)
  75. return [filename], [result.getvalue()]
  76. def upgradeSettingVisibility(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  77. """
  78. Upgrades setting visibility to have a version number and move moved settings to a different category
  79. :param serialized: The original contents of the stack.
  80. :param filename: The original file name of the stack.
  81. :return: A list of new file names, and a list of the new contents for
  82. those files.
  83. """
  84. parser = configparser.ConfigParser(interpolation = None, allow_no_value=True)
  85. parser.read_string(serialized)
  86. # add version number for the first time
  87. parser["general"]["version"] = "2"
  88. if "top_bottom" not in parser:
  89. parser["top_bottom"] = {}
  90. if "shell" in parser:
  91. for setting in parser["shell"]:
  92. if setting in self._moved_visibility_settings:
  93. parser["top_bottom"][setting] = None # type: ignore
  94. del parser["shell"][setting]
  95. result = io.StringIO()
  96. parser.write(result)
  97. return [filename], [result.getvalue()]