Browse Source

Make version upgrade also translate file names

This was required since Cura 2.1 produced files with the same filename (bar extension). This then resulted in two containers with the same ID. If you had bad luck, an instance container was chosen as global container (depending on which was first in the unordered dictionary). This gives the current settings the postfix _current_settings, fixing that issue.

Contributes to issue CURA-844.
Ghostkeeper 8 years ago
parent
commit
e6efba3868

+ 11 - 7
plugins/VersionUpgrade/VersionUpgrade21to22/MachineInstance.py

@@ -10,11 +10,12 @@ import io #To write config files to strings as if they were files.
 #   instance in version 1 of the file format.
 #   instance in version 1 of the file format.
 #
 #
 #   \param serialised The serialised form of a machine instance in version 1.
 #   \param serialised The serialised form of a machine instance in version 1.
+#   \param filename The supposed file name of this machine instance.
 #   \return A machine instance instance, or None if the file format is
 #   \return A machine instance instance, or None if the file format is
 #   incorrect.
 #   incorrect.
-def importFrom(serialised):
+def importFrom(serialised, filename):
     try:
     try:
-        return MachineInstance(serialised)
+        return MachineInstance(serialised, filename)
     except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
     except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
         return None
         return None
 
 
@@ -24,7 +25,10 @@ class MachineInstance:
     ##  Reads version 1 of the file format, storing it in memory.
     ##  Reads version 1 of the file format, storing it in memory.
     #
     #
     #   \param serialised A string with the contents of a machine instance file.
     #   \param serialised A string with the contents of a machine instance file.
-    def __init__(self, serialised):
+    #   \param filename The supposed file name of this machine instance.
+    def __init__(self, serialised, filename):
+        self._filename = filename
+
         config = configparser.ConfigParser(interpolation = None)
         config = configparser.ConfigParser(interpolation = None)
         config.read_string(serialised) # Read the input string as config file.
         config.read_string(serialised) # Read the input string as config file.
 
 
@@ -55,8 +59,8 @@ class MachineInstance:
     #
     #
     #   This is where the actual translation happens in this case.
     #   This is where the actual translation happens in this case.
     #
     #
-    #   \return A serialised form of this machine instance, serialised in
-    #   version 2 of the file format.
+    #   \return A tuple containing the new filename and a serialised form of
+    #   this machine instance, serialised in version 2 of the file format.
     def export(self):
     def export(self):
         config = configparser.ConfigParser(interpolation = None) # Build a config file in the form of version 2.
         config = configparser.ConfigParser(interpolation = None) # Build a config file in the form of version 2.
 
 
@@ -73,7 +77,7 @@ class MachineInstance:
         variant = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateVariant(self._variant_name, type_name)
         variant = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateVariant(self._variant_name, type_name)
 
 
         containers = [
         containers = [
-            self._name,
+            self._name + "_current_settings",
             active_profile,
             active_profile,
             active_material,
             active_material,
             variant,
             variant,
@@ -91,4 +95,4 @@ class MachineInstance:
 
 
         output = io.StringIO()
         output = io.StringIO()
         config.write(output)
         config.write(output)
-        return output.getvalue()
+        return self._filename, output.getvalue()

+ 10 - 5
plugins/VersionUpgrade/VersionUpgrade21to22/Preferences.py

@@ -10,11 +10,12 @@ import UM.VersionUpgrade #To indicate that a file is of the wrong format.
 #   in version 1 of the file format.
 #   in version 1 of the file format.
 #
 #
 #   \param serialised The serialised form of a preferences file in version 1.
 #   \param serialised The serialised form of a preferences file in version 1.
+#   \param filename The supposed filename of the preferences file.
 #   \return A representation of those preferences, or None if the file format is
 #   \return A representation of those preferences, or None if the file format is
 #   incorrect.
 #   incorrect.
-def importFrom(serialised):
+def importFrom(serialised, filename):
     try:
     try:
-        return Preferences(serialised)
+        return Preferences(serialised, filename)
     except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
     except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
         return None
         return None
 
 
@@ -24,7 +25,10 @@ class Preferences:
     ##  Reads version 2 of the preferences file format, storing it in memory.
     ##  Reads version 2 of the preferences file format, storing it in memory.
     #
     #
     #   \param serialised A serialised version 2 preferences file.
     #   \param serialised A serialised version 2 preferences file.
-    def __init__(self, serialised):
+    #   \param filename The supposed filename of the preferences file.
+    def __init__(self, serialised, filename):
+        self._filename = filename
+
         self._config = configparser.ConfigParser(interpolation = None)
         self._config = configparser.ConfigParser(interpolation = None)
         self._config.read_string(serialised)
         self._config.read_string(serialised)
 
 
@@ -42,7 +46,8 @@ class Preferences:
     #
     #
     #   This is where the actual translation happens.
     #   This is where the actual translation happens.
     #
     #
-    #   \return A serialised version of a preferences file in version 3.
+    #   \return A tuple containing the new filename and a serialised version of
+    #   a preferences file in version 3.
     def export(self):
     def export(self):
         #Reset the cura/categories_expanded property since it works differently now.
         #Reset the cura/categories_expanded property since it works differently now.
         if self._config.has_section("cura") and self._config.has_option("cura", "categories_expanded"):
         if self._config.has_section("cura") and self._config.has_option("cura", "categories_expanded"):
@@ -70,4 +75,4 @@ class Preferences:
         #Output the result as a string.
         #Output the result as a string.
         output = io.StringIO()
         output = io.StringIO()
         self._config.write(output)
         self._config.write(output)
-        return output.getvalue()
+        return self._filename, output.getvalue()

+ 14 - 7
plugins/VersionUpgrade/VersionUpgrade21to22/Profile.py

@@ -10,10 +10,11 @@ import UM.VersionUpgrade
 #   of the file format.
 #   of the file format.
 #
 #
 #   \param serialised The serialised form of a profile in version 1.
 #   \param serialised The serialised form of a profile in version 1.
+#   \param filename The supposed filename of the profile.
 #   \return A profile instance, or None if the file format is incorrect.
 #   \return A profile instance, or None if the file format is incorrect.
-def importFrom(serialised):
+def importFrom(serialised, filename):
     try:
     try:
-        return Profile(serialised)
+        return Profile(serialised, filename)
     except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
     except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
         return None
         return None
 
 
@@ -22,8 +23,11 @@ def importFrom(serialised):
 class Profile:
 class Profile:
     ##  Reads version 1 of the file format, storing it in memory.
     ##  Reads version 1 of the file format, storing it in memory.
     #
     #
-    #   \param serialised A string with the contents of a machine instance file.
-    def __init__(self, serialised):
+    #   \param serialised A string with the contents of a profile.
+    #   \param filename The supposed filename of the profile.
+    def __init__(self, serialised, filename):
+        self._filename = filename
+
         parser = configparser.ConfigParser(interpolation = None)
         parser = configparser.ConfigParser(interpolation = None)
         parser.read_string(serialised)
         parser.read_string(serialised)
 
 
@@ -70,11 +74,14 @@ class Profile:
 
 
     ##  Serialises this profile as file format version 2.
     ##  Serialises this profile as file format version 2.
     #
     #
-    #   \return A serialised form of this profile, serialised in version 2 of
-    #   the file format.
+    #   \return A tuple containing the new filename and a serialised form of
+    #   this profile, serialised in version 2 of the file format.
     def export(self):
     def export(self):
         import VersionUpgrade21to22 # Import here to prevent circular dependencies.
         import VersionUpgrade21to22 # Import here to prevent circular dependencies.
 
 
+        if self._name == "Current settings":
+            self._filename += "_current_settings" #This resolves a duplicate ID arising from how Cura 2.1 stores its current settings.
+
         config = configparser.ConfigParser(interpolation = None)
         config = configparser.ConfigParser(interpolation = None)
 
 
         config.add_section("general")
         config.add_section("general")
@@ -123,4 +130,4 @@ class Profile:
 
 
         output = io.StringIO()
         output = io.StringIO()
         config.write(output)
         config.write(output)
-        return output.getvalue()
+        return self._filename, output.getvalue()

+ 20 - 15
plugins/VersionUpgrade/VersionUpgrade21to22/VersionUpgrade21to22.py

@@ -66,34 +66,39 @@ class VersionUpgrade21to22(VersionUpgrade):
     ##  Converts machine instances from format version 1 to version 2.
     ##  Converts machine instances from format version 1 to version 2.
     #
     #
     #   \param serialised The serialised machine instance in version 1.
     #   \param serialised The serialised machine instance in version 1.
-    #   \return The serialised machine instance in version 2, or None if the
-    #   input was not of the correct format.
-    def upgradeMachineInstance(self, serialised):
-        machine_instance = MachineInstance.importFrom(serialised)
+    #   \param filename The supposed file name of the machine instance.
+    #   \return A tuple containing the new filename and the serialised machine
+    #   instance in version 2, or None if the input was not of the correct
+    #   format.
+    def upgradeMachineInstance(self, serialised, filename):
+        machine_instance = MachineInstance.importFrom(serialised, filename)
         if not machine_instance: #Invalid file format.
         if not machine_instance: #Invalid file format.
-            return None
+            return filename, None
         return machine_instance.export()
         return machine_instance.export()
 
 
     ##  Converts preferences from format version 2 to version 3.
     ##  Converts preferences from format version 2 to version 3.
     #
     #
     #   \param serialised The serialised preferences file in version 2.
     #   \param serialised The serialised preferences file in version 2.
-    #   \return The serialised preferences in version 3, or None if the input
-    #   was not of the correct format.
-    def upgradePreferences(self, serialised):
-        preferences = Preferences.importFrom(serialised)
+    #   \param filename THe supposed file name of the preferences file.
+    #   \return A tuple containing the new filename and the serialised
+    #   preferences in version 3, or None if the input was not of the correct
+    #   format.
+    def upgradePreferences(self, serialised, filename):
+        preferences = Preferences.importFrom(serialised, filename)
         if not preferences: #Invalid file format.
         if not preferences: #Invalid file format.
-            return None
+            return filename, None
         return preferences.export()
         return preferences.export()
 
 
     ##  Converts profiles from format version 1 to version 2.
     ##  Converts profiles from format version 1 to version 2.
     #
     #
     #   \param serialised The serialised profile in version 1.
     #   \param serialised The serialised profile in version 1.
-    #   \return The serialised profile in version 2, or None if the input was
-    #   not of the correct format.
-    def upgradeProfile(self, serialised):
-        profile = Profile.importFrom(serialised)
+    #   \param filename The supposed file name of the profile.
+    #   \return A tuple containing the new filename and the serialised profile
+    #   in version 2, or None if the input was not of the correct format.
+    def upgradeProfile(self, serialised, filename):
+        profile = Profile.importFrom(serialised, filename)
         if not profile: # Invalid file format.
         if not profile: # Invalid file format.
-            return None
+            return filename, None
         return profile.export()
         return profile.export()
 
 
     ##  Translates a printer name that might have changed since the last
     ##  Translates a printer name that might have changed since the last