Add version upgrade 4.1 to 4.2

CURA-6460
This commit is contained in:
Lipu Fei 2019-06-06 13:11:54 +02:00
parent e28ba497d3
commit 9911f98deb
3 changed files with 146 additions and 0 deletions

View file

@ -0,0 +1,79 @@
# Copyright (c) 2019 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
import configparser
import io
from typing import List, Tuple
from UM.VersionUpgrade import VersionUpgrade
_REMOVED_SETTING_KEYS = ["prime_tower_circular"] # type: List[str]
## Upgrades configurations from the state they were in at version 4.0 to the
# state they should be in at version 4.2.
class VersionUpgrade41to42(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"] = "8"
if "values" in parser:
for key in _REMOVED_SETTING_KEYS:
if key in parser["values"]:
del parser["values"][key]
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"] = "8"
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"] = "8"
result = io.StringIO()
parser.write(result)
return [filename], [result.getvalue()]

View file

@ -0,0 +1,59 @@
# 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 VersionUpgrade41to42
if TYPE_CHECKING:
from UM.Application import Application
upgrade = VersionUpgrade41to42.VersionUpgrade41to42()
def getMetaData() -> Dict[str, Any]:
return {
"version_upgrade": {
# From To Upgrade function
("preferences", 6000007): ("preferences", 6000008, upgrade.upgradePreferences),
("machine_stack", 4000007): ("machine_stack", 4000008, upgrade.upgradeStack),
("extruder_train", 4000007): ("extruder_train", 4000008, upgrade.upgradeStack),
("definition_changes", 4000007): ("definition_changes", 4000008, upgrade.upgradeInstanceContainer),
("quality_changes", 4000007): ("quality_changes", 4000008, upgrade.upgradeInstanceContainer),
("quality", 4000007): ("quality", 4000008, upgrade.upgradeInstanceContainer),
("user", 4000007): ("user", 4000008, 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 }

View file

@ -0,0 +1,8 @@
{
"name": "Version Upgrade 4.1 to 4.2",
"author": "Ultimaker B.V.",
"version": "1.0.0",
"description": "Upgrades configurations from Cura 4.1 to Cura 4.2.",
"api": "6.0",
"i18n-catalog": "cura"
}