VersionUpgrade24to25.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import configparser #To parse the files we need to upgrade and write the new files.
  4. import io #To serialise configparser output to a string.
  5. from UM.VersionUpgrade import VersionUpgrade
  6. _removed_settings = { #Settings that were removed in 2.5.
  7. "start_layers_at_same_position"
  8. }
  9. ## A collection of functions that convert the configuration of the user in Cura
  10. # 2.4 to a configuration for Cura 2.5.
  11. #
  12. # All of these methods are essentially stateless.
  13. class VersionUpgrade24to25(VersionUpgrade):
  14. ## Gets the version number from a CFG file in Uranium's 2.4 format.
  15. #
  16. # Since the format may change, this is implemented for the 2.4 format only
  17. # and needs to be included in the version upgrade system rather than
  18. # globally in Uranium.
  19. #
  20. # \param serialised The serialised form of a CFG file.
  21. # \return The version number stored in the CFG file.
  22. # \raises ValueError The format of the version number in the file is
  23. # incorrect.
  24. # \raises KeyError The format of the file is incorrect.
  25. def getCfgVersion(self, serialised):
  26. parser = configparser.ConfigParser(interpolation = None)
  27. parser.read_string(serialised)
  28. return int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  29. ## Upgrades the preferences file from version 2.4 to 2.5.
  30. #
  31. # \param serialised The serialised form of a preferences file.
  32. # \param filename The name of the file to upgrade.
  33. def upgradePreferences(self, serialised, filename):
  34. parser = configparser.ConfigParser(interpolation = None)
  35. parser.read_string(serialised)
  36. #Remove settings from the visible_settings.
  37. if parser.has_section("general") and "visible_settings" in parser["general"]:
  38. visible_settings = parser["general"]["visible_settings"].split(";")
  39. visible_settings = filter(lambda setting: setting not in _removed_settings, visible_settings)
  40. parser["general"]["visible_settings"] = ";".join(visible_settings)
  41. #Change the version number in the file.
  42. if parser.has_section("general"): #It better have!
  43. parser["general"]["version"] = "5"
  44. #Re-serialise the file.
  45. output = io.StringIO()
  46. parser.write(output)
  47. return [filename], [output.getvalue()]
  48. ## Upgrades an instance container from version 2.4 to 2.5.
  49. #
  50. # \param serialised The serialised form of a quality profile.
  51. # \param filename The name of the file to upgrade.
  52. def upgradeInstanceContainer(self, serialised, filename):
  53. parser = configparser.ConfigParser(interpolation = None)
  54. parser.read_string(serialised)
  55. #Remove settings from the [values] section.
  56. if parser.has_section("values"):
  57. for removed_setting in (_removed_settings & parser["values"].keys()): #Both in keys that need to be removed and in keys present in the file.
  58. del parser["values"][removed_setting]
  59. #Change the version number in the file.
  60. if parser.has_section("general"):
  61. parser["general"]["version"] = "3"
  62. #Re-serialise the file.
  63. output = io.StringIO()
  64. parser.write(output)
  65. return [filename], [output.getvalue()]