VersionUpgrade25to26.py 4.5 KB

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