Preferences.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser #To read config files.
  4. import io #To output config files to string.
  5. from typing import List, Optional, Tuple
  6. import UM.VersionUpgrade #To indicate that a file is of the wrong format.
  7. ## Creates a new preferences instance by parsing a serialised preferences file
  8. # in version 1 of the file format.
  9. #
  10. # \param serialised The serialised form of a preferences file in version 1.
  11. # \param filename The supposed filename of the preferences file, without
  12. # extension.
  13. # \return A representation of those preferences, or None if the file format is
  14. # incorrect.
  15. def importFrom(serialised: str, filename: str) -> Optional["Preferences"]:
  16. try:
  17. return Preferences(serialised, filename)
  18. except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
  19. return None
  20. ## A representation of preferences files as intermediary form for conversion
  21. # from one format to the other.
  22. class Preferences:
  23. ## Reads version 2 of the preferences file format, storing it in memory.
  24. #
  25. # \param serialised A serialised version 2 preferences file.
  26. # \param filename The supposed filename of the preferences file, without
  27. # extension.
  28. def __init__(self, serialised: str, filename: str) -> None:
  29. self._filename = filename
  30. self._config = configparser.ConfigParser(interpolation = None)
  31. self._config.read_string(serialised)
  32. #Checking file correctness.
  33. if not self._config.has_section("general"):
  34. raise UM.VersionUpgrade.FormatException("No \"general\" section.")
  35. if not self._config.has_option("general", "version"):
  36. raise UM.VersionUpgrade.FormatException("No \"version\" in \"general\" section.")
  37. 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.
  38. raise UM.VersionUpgrade.InvalidVersionException("The version of this preferences file is wrong. It must be 2.")
  39. if self._config.has_option("general", "name"): #This is probably a machine instance.
  40. raise UM.VersionUpgrade.FormatException("There is a \"name\" field in this configuration file. I suspect it is not a preferences file.")
  41. ## Serialises these preferences as a preferences file of version 3.
  42. #
  43. # This is where the actual translation happens.
  44. #
  45. # \return A tuple containing the new filename and a serialised version of
  46. # a preferences file in version 3.
  47. def export(self) -> Tuple[List[str], List[str]]:
  48. #Reset the cura/categories_expanded property since it works differently now.
  49. if self._config.has_section("cura") and self._config.has_option("cura", "categories_expanded"):
  50. self._config.remove_option("cura", "categories_expanded")
  51. #Translate the setting names in the visible settings.
  52. if self._config.has_section("machines") and self._config.has_option("machines", "setting_visibility"):
  53. visible_settings = self._config.get("machines", "setting_visibility")
  54. visible_settings_list = visible_settings.split(",")
  55. import VersionUpgrade21to22 #Import here to prevent a circular dependency.
  56. visible_settings_list = [VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateSettingName(setting_name)
  57. for setting_name in visible_settings_list]
  58. visible_settings = ",".join(visible_settings_list)
  59. self._config.set("machines", "setting_visibility", value = visible_settings)
  60. #Translate the active_instance key.
  61. if self._config.has_section("machines") and self._config.has_option("machines", "active_instance"):
  62. active_machine = self._config.get("machines", "active_instance")
  63. self._config.remove_option("machines", "active_instance")
  64. self._config.set("cura", "active_machine", active_machine)
  65. #Update the version number itself.
  66. self._config.set("general", "version", value = "3")
  67. #Output the result as a string.
  68. output = io.StringIO()
  69. self._config.write(output)
  70. return [self._filename], [output.getvalue()]