VersionUpgrade44to45.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import configparser
  2. from typing import Tuple, List
  3. import io
  4. from UM.VersionUpgrade import VersionUpgrade
  5. # Settings that were merged into one. Each one is a pair of settings. If both
  6. # are overwritten, the key wins. If only the key or the value is overwritten,
  7. # that value is used in the key.
  8. _merged_settings = {
  9. "machine_head_with_fans_polygon": "machine_head_polygon",
  10. "support_wall_count": "support_tree_wall_count"
  11. }
  12. _removed_settings = {
  13. "support_tree_wall_thickness"
  14. }
  15. class VersionUpgrade44to45(VersionUpgrade):
  16. def getCfgVersion(self, serialised: str) -> int:
  17. parser = configparser.ConfigParser(interpolation = None)
  18. parser.read_string(serialised)
  19. format_version = int(parser.get("general", "version")) # Explicitly give an exception when this fails. That means that the file format is not recognised.
  20. setting_version = int(parser.get("metadata", "setting_version", fallback = "0"))
  21. return format_version * 1000000 + setting_version
  22. ## Upgrades Preferences to have the new version number.
  23. #
  24. # This renames the renamed settings in the list of visible settings.
  25. def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  26. parser = configparser.ConfigParser(interpolation = None)
  27. parser.read_string(serialized)
  28. # Update version number.
  29. parser["metadata"]["setting_version"] = "11"
  30. result = io.StringIO()
  31. parser.write(result)
  32. return [filename], [result.getvalue()]
  33. ## Upgrades instance containers to have the new version
  34. # number.
  35. #
  36. # This renames the renamed settings in the containers.
  37. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  38. parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
  39. parser.read_string(serialized)
  40. # Update version number.
  41. parser["metadata"]["setting_version"] = "11"
  42. if "values" in parser:
  43. # Merged settings: When two settings are merged, one is preferred.
  44. # If the preferred one is available, that value is taken regardless
  45. # of the other one. If only the non-preferred one is available, that
  46. # value is moved to the preferred setting value.
  47. for preferred, removed in _merged_settings.items():
  48. if removed in parser["values"]:
  49. if preferred not in parser["values"]:
  50. parser["values"][preferred] = parser["values"][removed]
  51. del parser["values"][removed]
  52. for removed in _removed_settings:
  53. if removed in parser["values"]:
  54. del parser["values"][removed]
  55. result = io.StringIO()
  56. parser.write(result)
  57. return [filename], [result.getvalue()]
  58. ## Upgrades stacks to have the new version number.
  59. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  60. parser = configparser.ConfigParser(interpolation = None)
  61. parser.read_string(serialized)
  62. # Update version number.
  63. if "metadata" not in parser:
  64. parser["metadata"] = {}
  65. parser["metadata"]["setting_version"] = "11"
  66. result = io.StringIO()
  67. parser.write(result)
  68. return [filename], [result.getvalue()]