VersionUpgrade25to26.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 "general" not in parser:
  57. parser["general"] = {}
  58. parser.set("general", "version", "4")
  59. if "metadata" not in parser:
  60. parser["metadata"] = {}
  61. parser.set("metadata", "setting_version", "1")
  62. #Re-serialise the file.
  63. output = io.StringIO()
  64. parser.write(output)
  65. return [filename], [output.getvalue()]
  66. ## Upgrades an instance container from version 2.5 to 2.6.
  67. #
  68. # \param serialised The serialised form of a quality profile.
  69. # \param filename The name of the file to upgrade.
  70. def upgradeInstanceContainer(self, serialised, filename):
  71. parser = configparser.ConfigParser(interpolation = None)
  72. parser.read_string(serialised)
  73. #Remove settings from the [values] section.
  74. if parser.has_section("values"):
  75. for removed_setting in (_removed_settings & parser["values"].keys()): #Both in keys that need to be removed and in keys present in the file.
  76. del parser["values"][removed_setting]
  77. for replaced_setting in (_split_settings.keys() & parser["values"].keys()):
  78. for replacement in _split_settings[replaced_setting]:
  79. parser["values"][replacement] = parser["values"][replaced_setting] #Copy to replacement before removing the original!
  80. del replaced_setting
  81. for each_section in ("general", "metadata"):
  82. if not parser.has_section(each_section):
  83. parser.add_section(each_section)
  84. # Update version numbers
  85. parser["general"]["version"] = "2"
  86. parser["metadata"]["setting_version"] = "1"
  87. #Re-serialise the file.
  88. output = io.StringIO()
  89. parser.write(output)
  90. return [filename], [output.getvalue()]