Add version upgrade plug-in for 4.9

Needed to happen at some point...

Contributes to issue CURA-7787.
This commit is contained in:
Ghostkeeper 2020-10-20 17:20:51 +02:00 committed by Jelle Spijker
parent 0787594a3d
commit c85cf7b9cb
No known key found for this signature in database
GPG key ID: 6662DC033BE6B99A
3 changed files with 158 additions and 0 deletions

View file

@ -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()]