VersionUpgrade58to59.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (c) 2024 UltiMaker
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser
  4. from typing import Dict, List, Tuple
  5. import io
  6. from UM.VersionUpgrade import VersionUpgrade
  7. # Just to be sure, since in my testing there were both 0.1.0 and 0.2.0 settings about.
  8. _PLUGIN_NAME = "_plugin__curaenginegradualflow"
  9. _FROM_PLUGINS_SETTINGS = {
  10. "gradual_flow_enabled",
  11. "max_flow_acceleration",
  12. "layer_0_max_flow_acceleration",
  13. "gradual_flow_discretisation_step_size",
  14. "reset_flow_duration",
  15. } # type: Set[str]
  16. _NEW_SETTING_VERSION = "24"
  17. class VersionUpgrade58to59(VersionUpgrade):
  18. def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  19. """
  20. Upgrades preferences to remove from the visibility list the settings that were removed in this version.
  21. It also changes the preferences to have the new version number.
  22. This removes any settings that were removed in the new Cura version.
  23. :param serialized: The original contents of the preferences file.
  24. :param filename: The file name of the preferences file.
  25. :return: A list of new file names, and a list of the new contents for
  26. those files.
  27. """
  28. parser = configparser.ConfigParser(interpolation = None)
  29. parser.read_string(serialized)
  30. # Update version number.
  31. parser["metadata"]["setting_version"] = _NEW_SETTING_VERSION
  32. # Fix renamed settings for visibility
  33. if "visible_settings" in parser["general"]:
  34. all_setting_keys = parser["general"]["visible_settings"].strip().split(";")
  35. if all_setting_keys:
  36. for idx, key in enumerate(all_setting_keys):
  37. if key.startswith(_PLUGIN_NAME):
  38. all_setting_keys[idx] = key.split("__")[-1]
  39. parser["general"]["visible_settings"] = ";".join(all_setting_keys)
  40. result = io.StringIO()
  41. parser.write(result)
  42. return [filename], [result.getvalue()]
  43. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  44. """
  45. Upgrades instance containers to remove the settings that were removed in this version.
  46. It also changes the instance containers to have the new version number.
  47. This removes any settings that were removed in the new Cura version and updates settings that need to be updated
  48. with a new value.
  49. :param serialized: The original contents of the instance container.
  50. :param filename: The original file name of the instance container.
  51. :return: A list of new file names, and a list of the new contents for
  52. those files.
  53. """
  54. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  55. parser.read_string(serialized)
  56. # Update version number.
  57. parser["metadata"]["setting_version"] = _NEW_SETTING_VERSION
  58. # Rename settings.
  59. if "values" in parser:
  60. for key, value in parser["values"].items():
  61. if key.startswith(_PLUGIN_NAME):
  62. parser["values"][key.split("__")[-1]] = parser["values"][key]
  63. del parser["values"][key]
  64. result = io.StringIO()
  65. parser.write(result)
  66. return [filename], [result.getvalue()]
  67. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  68. """
  69. Upgrades stacks to have the new version number.
  70. :param serialized: The original contents of the stack.
  71. :param filename: The original file name of the stack.
  72. :return: A list of new file names, and a list of the new contents for
  73. those files.
  74. """
  75. parser = configparser.ConfigParser(interpolation = None)
  76. parser.read_string(serialized)
  77. # Update version number.
  78. if "metadata" not in parser:
  79. parser["metadata"] = {}
  80. parser["metadata"]["setting_version"] = _NEW_SETTING_VERSION
  81. result = io.StringIO()
  82. parser.write(result)
  83. return [filename], [result.getvalue()]