From 265a1b3fa0bf472da347328a46996df7f11a2527 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Thu, 27 Dec 2018 16:50:05 +0100 Subject: [PATCH] Add version upgrade plug-in for 4.0 to 4.1 This currently only updates the stack files, but all files have to be upgraded because of that stupid setting_version. Contributes to issue CURA-5848. --- .../VersionUpgrade34to35.py | 4 +- .../VersionUpgrade34to35/plugin.json | 2 +- .../VersionUpgrade40to41.py | 86 +++++++++++++++++++ .../VersionUpgrade40to41/__init__.py | 39 +++++++++ .../VersionUpgrade40to41/plugin.json | 8 ++ 5 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 plugins/VersionUpgrade/VersionUpgrade40to41/VersionUpgrade40to41.py create mode 100644 plugins/VersionUpgrade/VersionUpgrade40to41/__init__.py create mode 100644 plugins/VersionUpgrade/VersionUpgrade40to41/plugin.json diff --git a/plugins/VersionUpgrade/VersionUpgrade34to35/VersionUpgrade34to35.py b/plugins/VersionUpgrade/VersionUpgrade34to35/VersionUpgrade34to35.py index d930b6e217..8e45d7cf73 100644 --- a/plugins/VersionUpgrade/VersionUpgrade34to35/VersionUpgrade34to35.py +++ b/plugins/VersionUpgrade/VersionUpgrade34to35/VersionUpgrade34to35.py @@ -63,9 +63,9 @@ _RENAMED_MATERIAL_PROFILES = { ## Upgrades configurations from the state they were in at version 3.4 to the # state they should be in at version 3.5. class VersionUpgrade34to35(VersionUpgrade): - ## Gets the version number from a CFG file in Uranium's 3.3 format. + ## Gets the version number from a CFG file in Uranium's 3.4 format. # - # Since the format may change, this is implemented for the 3.3 format only + # Since the format may change, this is implemented for the 3.4 format only # and needs to be included in the version upgrade system rather than # globally in Uranium. # diff --git a/plugins/VersionUpgrade/VersionUpgrade34to35/plugin.json b/plugins/VersionUpgrade/VersionUpgrade34to35/plugin.json index 02635ec606..71b13ee5a9 100644 --- a/plugins/VersionUpgrade/VersionUpgrade34to35/plugin.json +++ b/plugins/VersionUpgrade/VersionUpgrade34to35/plugin.json @@ -1,4 +1,4 @@ - { +{ "name": "Version Upgrade 3.4 to 3.5", "author": "Ultimaker B.V.", "version": "1.0.1", diff --git a/plugins/VersionUpgrade/VersionUpgrade40to41/VersionUpgrade40to41.py b/plugins/VersionUpgrade/VersionUpgrade40to41/VersionUpgrade40to41.py new file mode 100644 index 0000000000..ac54b7c8e0 --- /dev/null +++ b/plugins/VersionUpgrade/VersionUpgrade40to41/VersionUpgrade40to41.py @@ -0,0 +1,86 @@ +# Copyright (c) 2018 Ultimaker B.V. +# Cura is released under the terms of the LGPLv3 or higher. + +import configparser +import io +from typing import Dict, List, Tuple + +from UM.VersionUpgrade import VersionUpgrade + +_renamed_quality_profiles = { + "gmax15plus_pla_dual_normal": "gmax15plus_global_dual_normal", + "gmax15plus_pla_dual_thick": "gmax15plus_global_dual_thick", + "gmax15plus_pla_dual_thin": "gmax15plus_global_dual_thin", + "gmax15plus_pla_dual_very_thick": "gmax15plus_global_dual_very_thick", + "gmax15plus_pla_normal": "gmax15plus_global_normal", + "gmax15plus_pla_thick": "gmax15plus_global_thick", + "gmax15plus_pla_thin": "gmax15plus_global_thin", + "gmax15plus_pla_very_thick": "gmax15plus_global_very_thick" +} # type: Dict[str, str] + +## Upgrades configurations from the state they were in at version 4.0 to the +# state they should be in at version 4.1. +class VersionUpgrade40to41(VersionUpgrade): + ## Gets the version number from a CFG file in Uranium's 4.0 format. + # + # Since the format may change, this is implemented for the 4.0 format only + # and needs to be included in the version upgrade system rather than + # globally in Uranium. + # + # \param serialised The serialised form of a CFG file. + # \return The version number stored in the CFG file. + # \raises ValueError The format of the version number in the file is + # incorrect. + # \raises KeyError The format of the file is incorrect. + def getCfgVersion(self, serialised: str) -> int: + parser = configparser.ConfigParser(interpolation = None) + parser.read_string(serialised) + format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised. + setting_version = int(parser.get("metadata", "setting_version", fallback = "0")) + return format_version * 1000000 + setting_version + + ## Upgrades instance containers to have the new version + # number. + def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]: + parser = configparser.ConfigParser(interpolation = None) + parser.read_string(serialized) + + # Update version number. + parser["general"]["version"] = "4" + parser["metadata"]["setting_version"] = "6" + + result = io.StringIO() + parser.write(result) + return [filename], [result.getvalue()] + + ## Upgrades Preferences to have the new version number. + def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]: + parser = configparser.ConfigParser(interpolation = None) + parser.read_string(serialized) + + # Update version number. + parser["general"]["version"] = "6" + if "metadata" not in parser: + parser["metadata"] = {} + parser["metadata"]["setting_version"] = "6" + + result = io.StringIO() + parser.write(result) + return [filename], [result.getvalue()] + + ## Upgrades stacks to have the new version number. + def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]: + parser = configparser.ConfigParser(interpolation = None) + parser.read_string(serialized) + + # Update version number. + parser["general"]["version"] = "4" + parser["metadata"]["setting_version"] = "6" + + #Update the name of the quality profile. + if parser["containers"]["4"] in _renamed_quality_profiles: + parser["containers"]["4"] = _renamed_quality_profiles[parser["containers"]["4"]] + + result = io.StringIO() + parser.write(result) + return [filename], [result.getvalue()] \ No newline at end of file diff --git a/plugins/VersionUpgrade/VersionUpgrade40to41/__init__.py b/plugins/VersionUpgrade/VersionUpgrade40to41/__init__.py new file mode 100644 index 0000000000..757a7a51c0 --- /dev/null +++ b/plugins/VersionUpgrade/VersionUpgrade40to41/__init__.py @@ -0,0 +1,39 @@ +# Copyright (c) 2018 Ultimaker B.V. +# Cura is released under the terms of the LGPLv3 or higher. + +from typing import Any, Dict, TYPE_CHECKING + +from . import VersionUpgrade40to41 + +if TYPE_CHECKING: + from UM.Application import Application + +upgrade = VersionUpgrade40to41.VersionUpgrade40to41() + +def getMetaData() -> Dict[str, Any]: + return { + "version_upgrade": { + # From To Upgrade function + ("machine_stack", 4000005): ("machine_stack", 4000006, upgrade.upgradeStack), + ("extruder_train", 4000005): ("extruder_train", 4000006, upgrade.upgradeStack), + ("preferences", 6000005): ("preferences", 6000006, upgrade.upgradePreferences), + ("definition_changes", 4000005): ("definition_changes", 4000006, upgrade.upgradeInstanceContainer), + ("quality_changes", 4000005): ("quality_changes", 4000006, upgrade.upgradeInstanceContainer), + ("quality", 4000005): ("quality", 4000006, upgrade.upgradeInstanceContainer), + ("user", 4000005): ("user", 4000006, upgrade.upgradeInstanceContainer), + }, + "sources": { + "machine_stack": { + "get_version": upgrade.getCfgVersion, + "location": {"./machine_instances"} + }, + "extruder_train": { + "get_version": upgrade.getCfgVersion, + "location": {"./extruders"} + } + } + } + + +def register(app: "Application") -> Dict[str, Any]: + return { "version_upgrade": upgrade } diff --git a/plugins/VersionUpgrade/VersionUpgrade40to41/plugin.json b/plugins/VersionUpgrade/VersionUpgrade40to41/plugin.json new file mode 100644 index 0000000000..b1c6d75669 --- /dev/null +++ b/plugins/VersionUpgrade/VersionUpgrade40to41/plugin.json @@ -0,0 +1,8 @@ +{ + "name": "Version Upgrade 4.0 to 4.1", + "author": "Ultimaker B.V.", + "version": "1.0.1", + "description": "Upgrades configurations from Cura 4.0 to Cura 4.1.", + "api": "6.0", + "i18n-catalog": "cura" +}