From c85cf7b9cb62eef9d4b39311c6da6f1dd7e0f0cc Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Tue, 20 Oct 2020 17:20:51 +0200 Subject: [PATCH] Add version upgrade plug-in for 4.9 Needed to happen at some point... Contributes to issue CURA-7787. --- .../VersionUpgrade48to49.py | 91 +++++++++++++++++++ .../VersionUpgrade48to49/__init__.py | 59 ++++++++++++ .../VersionUpgrade48to49/plugin.json | 8 ++ 3 files changed, 158 insertions(+) create mode 100644 plugins/VersionUpgrade/VersionUpgrade48to49/VersionUpgrade48to49.py create mode 100644 plugins/VersionUpgrade/VersionUpgrade48to49/__init__.py create mode 100644 plugins/VersionUpgrade/VersionUpgrade48to49/plugin.json diff --git a/plugins/VersionUpgrade/VersionUpgrade48to49/VersionUpgrade48to49.py b/plugins/VersionUpgrade/VersionUpgrade48to49/VersionUpgrade48to49.py new file mode 100644 index 0000000000..7ee2a23449 --- /dev/null +++ b/plugins/VersionUpgrade/VersionUpgrade48to49/VersionUpgrade48to49.py @@ -0,0 +1,91 @@ +# Copyright (c) 2020 Ultimaker B.V. +# Cura is released under the terms of the LGPLv3 or higher. + +import configparser +from typing import Tuple, List +import io + +from UM.VersionUpgrade import VersionUpgrade + + +class VersionUpgrade48to49(VersionUpgrade): + def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]: + """ + Upgrades preferences to have the new version number. + :param serialized: The original contents of the preferences file. + :param filename: The file name of the preferences file. + :return: A list of new file names, and a list of the new contents for + those files. + """ + parser = configparser.ConfigParser(interpolation = None) + parser.read_string(serialized) + + # Update version number. + parser["metadata"]["setting_version"] = "17" + + result = io.StringIO() + parser.write(result) + return [filename], [result.getvalue()] + + def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]: + """ + Upgrades instance containers to have the new version number. + :param serialized: The original contents of the instance container. + :param filename: The original file name of the instance container. + :return: A list of new file names, and a list of the new contents for + those files. + """ + parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ()) + parser.read_string(serialized) + + # Update version number. + parser["metadata"]["setting_version"] = "17" + + result = io.StringIO() + parser.write(result) + return [filename], [result.getvalue()] + + def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]: + """ + Upgrades stacks to have the new version number. + + This updates the post-processing scripts with new parameters. + :param serialized: The original contents of the stack. + :param filename: The original file name of the stack. + :return: A list of new file names, and a list of the new contents for + those files. + """ + parser = configparser.ConfigParser(interpolation = None) + parser.read_string(serialized) + + # Update version number. + if "metadata" not in parser: + parser["metadata"] = {} + parser["metadata"]["setting_version"] = "17" + + # Update Display Progress on LCD script parameters if present. + if "post_processing_scripts" in parser["metadata"]: + new_scripts_entries = [] + for script_str in parser["metadata"]["post_processing_scripts"].split("\n"): + if not script_str: + continue + script_str = script_str.replace(r"\\\n", "\n").replace(r"\\\\", "\\\\") # Unescape escape sequences. + script_parser = configparser.ConfigParser(interpolation=None) + script_parser.optionxform = str # type: ignore # Don't transform the setting keys as they are case-sensitive. + script_parser.read_string(script_str) + + # Update Display Progress on LCD parameters. + script_id = script_parser.sections()[0] + if script_id == "DisplayProgressOnLCD": + script_parser[script_id]["time_remaining"] = "m117" if script_parser[script_id]["time_remaining"] == "True" else "none" + + script_io = io.StringIO() + script_parser.write(script_io) + script_str = script_io.getvalue() + script_str = script_str.replace("\\\\", r"\\\\").replace("\n", r"\\\n") # Escape newlines because configparser sees those as section delimiters. + new_scripts_entries.append(script_str) + parser["metadata"]["post_processing_scripts"] = "\n".join(new_scripts_entries) + + result = io.StringIO() + parser.write(result) + return [filename], [result.getvalue()] diff --git a/plugins/VersionUpgrade/VersionUpgrade48to49/__init__.py b/plugins/VersionUpgrade/VersionUpgrade48to49/__init__.py new file mode 100644 index 0000000000..10bc84197f --- /dev/null +++ b/plugins/VersionUpgrade/VersionUpgrade48to49/__init__.py @@ -0,0 +1,59 @@ +# Copyright (c) 2020 Ultimaker B.V. +# Cura is released under the terms of the LGPLv3 or higher. + +from typing import Any, Dict, TYPE_CHECKING + +from . import VersionUpgrade48to49 + +if TYPE_CHECKING: + from UM.Application import Application + +upgrade = VersionUpgrade48to49.VersionUpgrade48to49() + +def getMetaData() -> Dict[str, Any]: + return { + "version_upgrade": { + # From To Upgrade function + ("preferences", 6000016): ("preferences", 6000017, upgrade.upgradePreferences), + ("machine_stack", 4000016): ("machine_stack", 4000017, upgrade.upgradeStack), + ("extruder_train", 4000016): ("extruder_train", 4000017, upgrade.upgradeStack), + ("definition_changes", 4000016): ("definition_changes", 4000017, upgrade.upgradeInstanceContainer), + ("quality_changes", 4000016): ("quality_changes", 4000017, upgrade.upgradeInstanceContainer), + ("quality", 4000016): ("quality", 4000017, upgrade.upgradeInstanceContainer), + ("user", 4000016): ("user", 4000017, 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/VersionUpgrade48to49/plugin.json b/plugins/VersionUpgrade/VersionUpgrade48to49/plugin.json new file mode 100644 index 0000000000..a70638d2d7 --- /dev/null +++ b/plugins/VersionUpgrade/VersionUpgrade48to49/plugin.json @@ -0,0 +1,8 @@ +{ + "name": "Version Upgrade 4.8 to 4.9", + "author": "Ultimaker B.V.", + "version": "1.0.0", + "description": "Upgrades configurations from Cura 4.8 to Cura 4.9.", + "api": "7.3.0", + "i18n-catalog": "cura" +}