Preferences.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import configparser #To read config files.
  4. import io #To output config files to string.
  5. import UM.VersionUpgrade #To indicate that a file is of the wrong format.
  6. ## Creates a new preferences instance by parsing a serialised preferences file
  7. # in version 1 of the file format.
  8. #
  9. # \param serialised The serialised form of a preferences file in version 1.
  10. # \return A representation of those preferences, or None if the file format is
  11. # incorrect.
  12. def importFrom(serialised):
  13. try:
  14. return Preferences(serialised)
  15. except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
  16. return None
  17. ## A representation of preferences files as intermediary form for conversion
  18. # from one format to the other.
  19. class Preferences:
  20. ## Reads version 2 of the preferences file format, storing it in memory.
  21. #
  22. # \param serialised A serialised version 2 preferences file.
  23. def __init__(self, serialised):
  24. self._config = configparser.ConfigParser(interpolation = None)
  25. self._config.read_string(serialised)
  26. #Checking file correctness.
  27. if not self._config.has_section("general"):
  28. raise UM.VersionUpgrade.FormatException("No \"general\" section.")
  29. if not self._config.has_option("general", "version"):
  30. raise UM.VersionUpgrade.FormatException("No \"version\" in \"general\" section.")
  31. if int(self._config.get("general", "version")) != 2: # Explicitly hard-code version 2, since if this number changes the programmer MUST change this entire function.
  32. raise UM.VersionUpgrade.InvalidVersionException("The version of this preferences file is wrong. It must be 2.")
  33. ## Serialises these preferences as a preferences file of version 3.
  34. #
  35. # This is where the actual translation happens.
  36. #
  37. # \return A serialised version of a preferences file in version 3.
  38. def export(self):
  39. #Reset the cura/categories_expanded property since it works differently now.
  40. if self._config.has_section("cura") and self._config.has_option("cura", "categories_expanded"):
  41. self._config.remove_option("cura", "categories_expanded")
  42. #Translate the setting names in the visible settings.
  43. if self._config.has_section("machines") and self._config.has_option("machines", "setting_visibility"):
  44. visible_settings = self._config.get("machines", "setting_visibility")
  45. visible_settings = visible_settings.split(",")
  46. import VersionUpgrade21to22 #Import here to prevent a circular dependency.
  47. visible_settings = VersionUpgrade21to22.translateSettingNames(visible_settings)
  48. visible_settings = visible_settings.join(",")
  49. self._config.set("machines", "setting_visibility", value = visible_settings)
  50. #Output the result as a string.
  51. output = io.StringIO()
  52. self._config.write(output)
  53. return output.getvalue()