1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import configparser
- import io
- import UM.VersionUpgrade
- def importFrom(serialised, filename):
- try:
- return Preferences(serialised, filename)
- except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
- return None
- class Preferences:
-
-
-
-
-
- def __init__(self, serialised, filename):
- self._filename = filename
- self._config = configparser.ConfigParser(interpolation = None)
- self._config.read_string(serialised)
-
- if not self._config.has_section("general"):
- raise UM.VersionUpgrade.FormatException("No \"general\" section.")
- if not self._config.has_option("general", "version"):
- raise UM.VersionUpgrade.FormatException("No \"version\" in \"general\" section.")
- if int(self._config.get("general", "version")) != 2:
- raise UM.VersionUpgrade.InvalidVersionException("The version of this preferences file is wrong. It must be 2.")
- if self._config.has_option("general", "name"):
- raise UM.VersionUpgrade.FormatException("There is a \"name\" field in this configuration file. I suspect it is not a preferences file.")
-
-
-
-
-
-
- def export(self):
-
- if self._config.has_section("cura") and self._config.has_option("cura", "categories_expanded"):
- self._config.remove_option("cura", "categories_expanded")
-
- if self._config.has_section("machines") and self._config.has_option("machines", "setting_visibility"):
- visible_settings = self._config.get("machines", "setting_visibility")
- visible_settings = visible_settings.split(",")
- import VersionUpgrade21to22
- visible_settings = [VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateSettingName(setting_name)
- for setting_name in visible_settings]
- visible_settings = ",".join(visible_settings)
- self._config.set("machines", "setting_visibility", value = visible_settings)
-
- if self._config.has_section("machines") and self._config.has_option("machines", "active_instance"):
- active_machine = self._config.get("machines", "active_instance")
- self._config.remove_option("machines", "active_instance")
- self._config.set("cura", "active_machine", active_machine)
-
- self._config.set("general", "version", value = "3")
-
- output = io.StringIO()
- self._config.write(output)
- return [self._filename], [output.getvalue()]
|