diff options
author | Nino van Hooff <ninovanhooff@gmail.com> | 2019-11-01 11:58:47 +0300 |
---|---|---|
committer | Nino van Hooff <ninovanhooff@gmail.com> | 2019-11-01 11:58:47 +0300 |
commit | 88b3a2506daf4e531317882c8e8cdad2fe6530bd (patch) | |
tree | 3d603b29c4ab537c981e09b96d2a8fbe7eeb4a46 /plugins/VersionUpgrade | |
parent | e5c9bca3d072318fbcaedc41e7ab9dabe16ec7c5 (diff) |
Version Upgrade 44 to 45
Includes test for changes made in CURA-6522
CURA-6522
Diffstat (limited to 'plugins/VersionUpgrade')
4 files changed, 180 insertions, 0 deletions
diff --git a/plugins/VersionUpgrade/VersionUpgrade44to45/VersionUpgrade44to45.py b/plugins/VersionUpgrade/VersionUpgrade44to45/VersionUpgrade44to45.py new file mode 100644 index 0000000000..36ee2bac16 --- /dev/null +++ b/plugins/VersionUpgrade/VersionUpgrade44to45/VersionUpgrade44to45.py @@ -0,0 +1,69 @@ +import configparser +from typing import Tuple, List +import io +from UM.VersionUpgrade import VersionUpgrade + +# Merged preferences: machine_head_polygon and machine_head_with_fans_polygon -> machine_head_with_fans_polygon +# When both are present, machine_head_polygon will be removed +# When only one of the two is present, it's value will be used + + +class VersionUpgrade44to45(VersionUpgrade): + 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 Preferences to have the new version number. + # + # This renames the renamed settings in the list of visible settings. + 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["metadata"]["setting_version"] = "11" + + result = io.StringIO() + parser.write(result) + return [filename], [result.getvalue()] + + ## Upgrades instance containers to have the new version + # number. + # + # This renames the renamed settings in the containers. + 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["metadata"]["setting_version"] = "11" + + if "values" in parser: + # merge machine_head_with_fans_polygon (preferred) and machine_head_polygon + if "machine_head_with_fans_polygon" in parser["values"]: + if "machine_head_polygon" in parser["values"]: + del parser["values"]["machine_head_polygon"] + elif "machine_head_polygon" in parser["values"]: + parser["values"]["machine_head_with_fans_polygon"] = parser["values"]["machine_head_polygon"] + del parser["values"]["machine_head_polygon"] + + 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. + if "metadata" not in parser: + parser["metadata"] = {} + parser["metadata"]["setting_version"] = "11" + + result = io.StringIO() + parser.write(result) + return [filename], [result.getvalue()] diff --git a/plugins/VersionUpgrade/VersionUpgrade44to45/__init__.py b/plugins/VersionUpgrade/VersionUpgrade44to45/__init__.py new file mode 100644 index 0000000000..06a9d66c50 --- /dev/null +++ b/plugins/VersionUpgrade/VersionUpgrade44to45/__init__.py @@ -0,0 +1,61 @@ +# Copyright (c) 2019 Ultimaker B.V. +# Cura is released under the terms of the LGPLv3 or higher. + +from typing import Any, Dict, TYPE_CHECKING + +from . import VersionUpgrade44to45 + + +if TYPE_CHECKING: + from UM.Application import Application + +upgrade = VersionUpgrade44to45.VersionUpgrade44to45() + + +def getMetaData() -> Dict[str, Any]: + return { + "version_upgrade": { + # From To Upgrade function + ("preferences", 6000010): ("preferences", 6000011, upgrade.upgradePreferences), + ("machine_stack", 4000010): ("machine_stack", 4000011, upgrade.upgradeStack), + ("extruder_train", 4000010): ("extruder_train", 4000011, upgrade.upgradeStack), + ("definition_changes", 4000010): ("definition_changes", 4000011, upgrade.upgradeInstanceContainer), + ("quality_changes", 4000010): ("quality_changes", 4000011, upgrade.upgradeInstanceContainer), + ("quality", 4000010): ("quality", 4000011, upgrade.upgradeInstanceContainer), + ("user", 4000010): ("user", 4000011, upgrade.upgradeInstanceContainer), + }, + "sources": { + "preferences": { + "get_version": upgrade.getCfgVersion, + "location": {"."} + }, + "machine_stack": { + "get_version": upgrade.getCfgVersion, + "location": {"./machine_instances"} + }, + "extruder_train": { + "get_version": upgrade.getCfgVersion, + "location": {"./extruders"} + }, + "definition_changes": { + "get_version": upgrade.getCfgVersion, + "location": {"./definition_changes"} + }, + "quality_changes": { + "get_version": upgrade.getCfgVersion, + "location": {"./quality_changes"} + }, + "quality": { + "get_version": upgrade.getCfgVersion, + "location": {"./quality"} + }, + "user": { + "get_version": upgrade.getCfgVersion, + "location": {"./user"} + } + } + } + + +def register(app: "Application") -> Dict[str, Any]: + return {"version_upgrade": upgrade} diff --git a/plugins/VersionUpgrade/VersionUpgrade44to45/plugin.json b/plugins/VersionUpgrade/VersionUpgrade44to45/plugin.json new file mode 100644 index 0000000000..f7b6157118 --- /dev/null +++ b/plugins/VersionUpgrade/VersionUpgrade44to45/plugin.json @@ -0,0 +1,8 @@ +{ + "name": "Version Upgrade 4.4 to 4.5", + "author": "Ultimaker B.V.", + "version": "1.0.0", + "description": "Upgrades configurations from Cura 4.4 to Cura 4.5.", + "api": "7.0", + "i18n-catalog": "cura" +} diff --git a/plugins/VersionUpgrade/VersionUpgrade44to45/tests/TestVersionUpgrade44To45.py b/plugins/VersionUpgrade/VersionUpgrade44to45/tests/TestVersionUpgrade44To45.py new file mode 100644 index 0000000000..60b5494f84 --- /dev/null +++ b/plugins/VersionUpgrade/VersionUpgrade44to45/tests/TestVersionUpgrade44To45.py @@ -0,0 +1,42 @@ +import configparser + +import VersionUpgrade44to45 +import pytest + +before_update = """[general] +version = 4 +name = Creality CR-10S_settings +definition = creality_cr10s + +[metadata] +type = definition_changes +setting_version = 10 + +[values] +%s +""" +before_after_list = [ + ("machine_head_with_fans_polygon = [[-99, 99], [-99, -44], [45, 99], [45, -44]]", "[[-99, 99], [-99, -44], [45, 99], [45, -44]]"), + ("", None), + ("machine_head_polygon = [[-98, 99], [-99, -44], [45, 99], [45, -44]]", "[[-98, 99], [-99, -44], [45, 99], [45, -44]]"), + ("machine_head_polygon = [[-87, 99], [-99, -44], [45, 99], [45, -44]]\nmachine_head_with_fans_polygon = [[-99, 99], [-99, -44], [45, 99], [45, -44]]", "[[-99, 99], [-99, -44], [45, 99], [45, -44]]"), + ] + + +class TestVersionUpgrade44to45: + + @pytest.mark.parametrize("after_string, after_value", before_after_list) + def test_upgrade(self, after_string, after_value): + upgrader = VersionUpgrade44to45.VersionUpgrade44to45() + + + file_name, new_data = upgrader.upgradeInstanceContainer(before_update % after_string, "whatever") + parser = configparser.ConfigParser(interpolation=None) + parser.read_string(new_data[0]) + + if after_value is None: + assert "machine_head_with_fans_polygon" not in parser["values"] + else: + assert parser["values"]["machine_head_with_fans_polygon"] == after_value + + assert "machine_head_polygon" not in parser["values"]
\ No newline at end of file |