VersionUpgrade35to40.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import configparser
  2. from typing import Tuple, List, Set, Dict
  3. import io
  4. from UM.VersionUpgrade import VersionUpgrade
  5. from cura.PrinterOutputDevice import ConnectionType
  6. deleted_settings = {"bridge_wall_max_overhang"} # type: Set[str]
  7. renamed_configurations = {"connect_group_name": "group_name"} # type: Dict[str, str]
  8. class VersionUpgrade35to40(VersionUpgrade):
  9. # Upgrades stacks to have the new version number.
  10. def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  11. parser = configparser.ConfigParser(interpolation=None)
  12. parser.read_string(serialized)
  13. # Update version number.
  14. parser["general"]["version"] = "4"
  15. parser["metadata"]["setting_version"] = "6"
  16. if parser["metadata"].get("um_network_key") is not None or parser["metadata"].get("octoprint_api_key") is not None:
  17. # Set the connection type if um_network_key or the octoprint key is set.
  18. parser["metadata"]["connection_type"] = str(ConnectionType.NetworkConnection.value)
  19. if "metadata" in parser:
  20. for old_name, new_name in renamed_configurations.items():
  21. if old_name not in parser["metadata"]:
  22. continue
  23. parser["metadata"][new_name] = parser["metadata"][old_name]
  24. del parser["metadata"][old_name]
  25. result = io.StringIO()
  26. parser.write(result)
  27. return [filename], [result.getvalue()]
  28. def getCfgVersion(self, serialised: str) -> int:
  29. parser = configparser.ConfigParser(interpolation = None)
  30. parser.read_string(serialised)
  31. format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  32. setting_version = int(parser.get("metadata", "setting_version", fallback = "0"))
  33. return format_version * 1000000 + setting_version
  34. ## Upgrades Preferences to have the new version number.
  35. def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  36. parser = configparser.ConfigParser(interpolation=None)
  37. parser.read_string(serialized)
  38. if "metadata" not in parser:
  39. parser["metadata"] = {}
  40. parser["general"]["version"] = "6"
  41. parser["metadata"]["setting_version"] = "6"
  42. result = io.StringIO()
  43. parser.write(result)
  44. return [filename], [result.getvalue()]
  45. ## Upgrades instance containers to have the new version
  46. # number.
  47. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  48. parser = configparser.ConfigParser(interpolation=None)
  49. parser.read_string(serialized)
  50. # Update version number.
  51. parser["general"]["version"] = "4"
  52. parser["metadata"]["setting_version"] = "6"
  53. #self._resetConcentric3DInfillPattern(parser)
  54. if "values" in parser:
  55. for deleted_setting in deleted_settings:
  56. if deleted_setting not in parser["values"]:
  57. continue
  58. del parser["values"][deleted_setting]
  59. result = io.StringIO()
  60. parser.write(result)
  61. return [filename], [result.getvalue()]