VersionUpgrade25to26.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. from cura.CuraApplication import CuraApplication
  7. _removed_settings = { #Settings that were removed in 2.5.
  8. "start_layers_at_same_position",
  9. "sub_div_rad_mult"
  10. }
  11. _split_settings = { #These settings should be copied to all settings it was split into.
  12. "support_interface_line_distance": {"support_roof_line_distance", "support_bottom_line_distance"}
  13. }
  14. ## A collection of functions that convert the configuration of the user in Cura
  15. # 2.5 to a configuration for Cura 2.6.
  16. #
  17. # All of these methods are essentially stateless.
  18. class VersionUpgrade25to26(VersionUpgrade):
  19. ## Gets the version number from a CFG file in Uranium's 2.5 format.
  20. #
  21. # Since the format may change, this is implemented for the 2.5 format only
  22. # and needs to be included in the version upgrade system rather than
  23. # globally in Uranium.
  24. #
  25. # \param serialised The serialised form of a CFG file.
  26. # \return The version number stored in the CFG file.
  27. # \raises ValueError The format of the version number in the file is
  28. # incorrect.
  29. # \raises KeyError The format of the file is incorrect.
  30. def getCfgVersion(self, serialised):
  31. parser = configparser.ConfigParser(interpolation = None)
  32. parser.read_string(serialised)
  33. format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  34. setting_version = int(parser.get("metadata", "setting_version", fallback = 0))
  35. return format_version * 1000000 + setting_version
  36. ## Upgrades the preferences file from version 2.5 to 2.6.
  37. #
  38. # \param serialised The serialised form of a preferences file.
  39. # \param filename The name of the file to upgrade.
  40. def upgradePreferences(self, serialised, filename):
  41. parser = configparser.ConfigParser(interpolation = None)
  42. parser.read_string(serialised)
  43. #Remove settings from the visible_settings.
  44. if parser.has_section("general") and "visible_settings" in parser["general"]:
  45. visible_settings = parser["general"]["visible_settings"].split(";")
  46. new_visible_settings = []
  47. for setting in visible_settings:
  48. if setting in _removed_settings:
  49. continue #Skip.
  50. if setting in _split_settings:
  51. for replaced_setting in _split_settings[setting]:
  52. new_visible_settings.append(replaced_setting)
  53. continue #Don't add the original.
  54. new_visible_settings.append(setting) #No special handling, so just add the original visible setting back.
  55. parser["general"]["visible_settings"] = ";".join(new_visible_settings)
  56. #Change the version number in the file.
  57. if parser.has_section("general"): #It better have!
  58. parser["general"]["version"] = "5"
  59. #Re-serialise the file.
  60. output = io.StringIO()
  61. parser.write(output)
  62. return [filename], [output.getvalue()]
  63. ## Upgrades an instance container from version 2.5 to 2.6.
  64. #
  65. # \param serialised The serialised form of a quality profile.
  66. # \param filename The name of the file to upgrade.
  67. def upgradeInstanceContainer(self, serialised, filename):
  68. parser = configparser.ConfigParser(interpolation = None)
  69. parser.read_string(serialised)
  70. #Remove settings from the [values] section.
  71. if parser.has_section("values"):
  72. for removed_setting in (_removed_settings & parser["values"].keys()): #Both in keys that need to be removed and in keys present in the file.
  73. del parser["values"][removed_setting]
  74. for replaced_setting in (_split_settings.keys() & parser["values"].keys()):
  75. for replacement in _split_settings[replaced_setting]:
  76. parser["values"][replacement] = parser["values"][replaced_setting] #Copy to replacement before removing the original!
  77. del replaced_setting
  78. for each_section in ("general", "metadata"):
  79. if not parser.has_section(each_section):
  80. parser.add_section(each_section)
  81. # Change the version number in the file.
  82. parser["metadata"]["setting_version"] = str(CuraApplication.SettingVersion)
  83. # Update version
  84. parser["general"]["version"] = "2"
  85. #Re-serialise the file.
  86. output = io.StringIO()
  87. parser.write(output)
  88. return [filename], [output.getvalue()]