diff options
author | Simon Edwards <s.edwards@ultimaker.com> | 2016-12-05 13:00:33 +0300 |
---|---|---|
committer | Simon Edwards <s.edwards@ultimaker.com> | 2016-12-05 13:00:33 +0300 |
commit | 3d9c27a7f7d4d0b6dd3016fc0b1adf5684194e59 (patch) | |
tree | dc050f5a89abef9739e67a4913cf3a2247e1ed44 /plugins/VersionUpgrade/VersionUpgrade22to24 | |
parent | e941d417f1fd570ed836d05b5816cf3e588e94b5 (diff) |
CURA-2953 Version upgrade 2.3 to 2.4
Diffstat (limited to 'plugins/VersionUpgrade/VersionUpgrade22to24')
-rw-r--r-- | plugins/VersionUpgrade/VersionUpgrade22to24/VersionUpgrade.py | 90 | ||||
-rw-r--r-- | plugins/VersionUpgrade/VersionUpgrade22to24/__init__.py | 33 |
2 files changed, 123 insertions, 0 deletions
diff --git a/plugins/VersionUpgrade/VersionUpgrade22to24/VersionUpgrade.py b/plugins/VersionUpgrade/VersionUpgrade22to24/VersionUpgrade.py new file mode 100644 index 0000000000..38710026ac --- /dev/null +++ b/plugins/VersionUpgrade/VersionUpgrade22to24/VersionUpgrade.py @@ -0,0 +1,90 @@ +# Copyright (c) 2016 Ultimaker B.V.
+# Cura is released under the terms of the AGPLv3 or higher.
+
+import configparser #To get version numbers from config files.
+import os.path
+import io
+
+from UM import Resources
+from UM.VersionUpgrade import VersionUpgrade # Superclass of the plugin.
+
+class VersionUpgrade22to24(VersionUpgrade):
+
+ def upgradeMachineInstance(self, serialised, filename):
+ config = configparser.ConfigParser(interpolation = None)
+ config.read_string(serialised) # Read the input string as config file.
+ config.set("general", "version", "3")
+ containers = config.get("general", "containers")
+ container_list = containers.split(",")
+
+ user_variants = self.__getUserVariants()
+ name_path_dict = {}
+ for variant in user_variants:
+ name_path_dict[variant.get("name")] = variant.get("path")
+
+ user_variant_names = set(container_list).intersection(name_path_dict.keys())
+ if len(user_variant_names):
+ # One of the user defined variants appears in the list of containers in the stack.
+
+ for variant_name in user_variant_names:
+ # Copy the variant to the machine_instances/*_settings.inst.cfg
+ variant_config = configparser.ConfigParser(interpolation=None)
+ variant_path = name_path_dict.get(variant_name)
+ with open(variant_path, "r") as fhandle:
+ variant_config.read_file(fhandle)
+
+ if variant_config.has_section("general") and variant_config.has_option("general", "name"):
+ config_name = variant_config.get("general", "name")
+ if config_name.endswith("_variant"):
+ config_name = config_name[:-len("_variant")] + "_settings"
+ variant_config.set("general", "name", config_name)
+
+ if not variant_config.has_section("metadata"):
+ variant_config.add_section("metadata")
+ variant_config.set("metadata", "type", "definition_changes")
+
+ # "_settings.inst.cfg"
+ resource_path = Resources.getDataStoragePath()
+ machine_instances_dir = os.path.join(resource_path, "machine_instances")
+
+ if variant_path.endswith("_variant.inst.cfg"):
+ variant_path = variant_path[:-len("_variant.inst.cfg")] + "_settings.inst.cfg"
+
+ with open(os.path.join(machine_instances_dir, os.path.basename(variant_path)), "w") as fp:
+ variant_config.write(fp)
+
+ # Change the name of variant and insert empty_variant into the stack.
+ new_container_list = []
+ for item in container_list:
+ if item == variant_name:
+ new_container_list.append(config_name)
+ new_container_list.append("empty_variant")
+ else:
+ new_container_list.append(item)
+
+ container_list = new_container_list
+
+ config.set("general", "containers", ",".join(container_list))
+
+ output = io.StringIO()
+ config.write(output)
+ return [filename], [output.getvalue()]
+
+ def __getUserVariants(self):
+ resource_path = Resources.getDataStoragePath()
+ variants_dir = os.path.join(resource_path, "variants")
+
+ result = []
+ for entry in os.scandir(variants_dir):
+ if entry.name.endswith('.inst.cfg') and entry.is_file():
+ config = configparser.ConfigParser(interpolation = None)
+ with open(entry.path, "r") as fhandle:
+ config.read_file(fhandle)
+ if config.has_section("general") and config.has_option("general", "name"):
+ result.append( { "path": entry.path, "name": config.get("general", "name") } )
+ return result
+
+ def getCfgVersion(self, serialised):
+ parser = configparser.ConfigParser(interpolation = None)
+ parser.read_string(serialised)
+ return int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
diff --git a/plugins/VersionUpgrade/VersionUpgrade22to24/__init__.py b/plugins/VersionUpgrade/VersionUpgrade22to24/__init__.py new file mode 100644 index 0000000000..1a562abbd5 --- /dev/null +++ b/plugins/VersionUpgrade/VersionUpgrade22to24/__init__.py @@ -0,0 +1,33 @@ +# Copyright (c) 2016 Ultimaker B.V.
+# Cura is released under the terms of the AGPLv3 or higher.
+
+from . import VersionUpgrade
+
+from UM.i18n import i18nCatalog
+catalog = i18nCatalog("cura")
+
+upgrade = VersionUpgrade.VersionUpgrade22to24()
+
+def getMetaData():
+ return {
+ "plugin": {
+ "name": catalog.i18nc("@label", "Version Upgrade 2.2 to 2.4"),
+ "author": "Ultimaker",
+ "version": "1.0",
+ "description": catalog.i18nc("@info:whatsthis", "Upgrades configurations from Cura 2.2 to Cura 2.4."),
+ "api": 3
+ },
+ "version_upgrade": {
+ # From To Upgrade function
+ ("machine_instance", 2): ("machine_stack", 3, upgrade.upgradeMachineInstance)
+ },
+ "sources": {
+ "machine_instance": {
+ "get_version": upgrade.getCfgVersion,
+ "location": {"./machine_instances"}
+ }
+ }
+ }
+
+def register(app):
+ return { "version_upgrade": upgrade }
|