VersionUpgrade45to46.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (c) 2020 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. from UM.VersionUpgrade import VersionUpgrade
  7. _removed_settings = {
  8. "machine_filament_park_distance",
  9. }
  10. class VersionUpgrade45to46(VersionUpgrade):
  11. def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  12. """
  13. Upgrades preferences to have the new version number.
  14. This removes any settings that were removed in the new Cura version.
  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["metadata"]["setting_version"] = "13"
  24. # Remove deleted settings from the visible settings list.
  25. if "general" in parser and "visible_settings" in parser["general"]:
  26. visible_settings = set(parser["general"]["visible_settings"].split(";"))
  27. for removed in _removed_settings:
  28. if removed in visible_settings:
  29. visible_settings.remove(removed)
  30. parser["general"]["visible_settings"] = ";".join(visible_settings)
  31. result = io.StringIO()
  32. parser.write(result)
  33. return [filename], [result.getvalue()]
  34. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  35. """
  36. Upgrades instance containers to have the new version number.
  37. This removes any settings that were removed in the new Cura version.
  38. :param serialized: The original contents of the instance container.
  39. :param filename: The original file name of the instance container.
  40. :return: A list of new file names, and a list of the new contents for
  41. those files.
  42. """
  43. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  44. parser.read_string(serialized)
  45. # Update version number.
  46. parser["metadata"]["setting_version"] = "13"
  47. if "values" in parser:
  48. for removed in _removed_settings:
  49. if removed in parser["values"]:
  50. del parser["values"][removed]
  51. if "meshfix_maximum_deviation" in parser["values"]:
  52. maximum_deviation = parser["values"]["meshfix_maximum_deviation"]
  53. if maximum_deviation.startswith("="):
  54. maximum_deviation = maximum_deviation[1:]
  55. maximum_deviation = "=(" + maximum_deviation + ") / 2"
  56. parser["values"]["meshfix_maximum_deviation"] = maximum_deviation
  57. result = io.StringIO()
  58. parser.write(result)
  59. return [filename], [result.getvalue()]
  60. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  61. """
  62. Upgrades stacks to have the new version number.
  63. :param serialized: The original contents of the stack.
  64. :param filename: The original file name of the stack.
  65. :return: A list of new file names, and a list of the new contents for
  66. those files.
  67. """
  68. parser = configparser.ConfigParser(interpolation = None)
  69. parser.read_string(serialized)
  70. # Update version number.
  71. if "metadata" not in parser:
  72. parser["metadata"] = {}
  73. parser["metadata"]["setting_version"] = "13"
  74. result = io.StringIO()
  75. parser.write(result)
  76. return [filename], [result.getvalue()]