mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-24 23:23:57 -06:00
Rename 24to25 to 25to26
The upgrade now upgrades profiles from the 2.5 format to 2.6, since we delayed the update for the 2.5 release. Contributes to issue CURA-3427.
This commit is contained in:
parent
99cb8bc169
commit
b57cb16f6d
3 changed files with 9 additions and 9 deletions
|
@ -0,0 +1,93 @@
|
|||
# Copyright (c) 2017 Ultimaker B.V.
|
||||
# Cura is released under the terms of the AGPLv3 or higher.
|
||||
|
||||
import configparser #To parse the files we need to upgrade and write the new files.
|
||||
import io #To serialise configparser output to a string.
|
||||
|
||||
from UM.VersionUpgrade import VersionUpgrade
|
||||
|
||||
_removed_settings = { #Settings that were removed in 2.5.
|
||||
"start_layers_at_same_position"
|
||||
}
|
||||
|
||||
_split_settings = { #These settings should be copied to all settings it was split into.
|
||||
"support_interface_line_distance": {"support_roof_line_distance", "support_bottom_line_distance"}
|
||||
}
|
||||
|
||||
## A collection of functions that convert the configuration of the user in Cura
|
||||
# 2.5 to a configuration for Cura 2.6.
|
||||
#
|
||||
# All of these methods are essentially stateless.
|
||||
class VersionUpgrade25to26(VersionUpgrade):
|
||||
## Gets the version number from a CFG file in Uranium's 2.5 format.
|
||||
#
|
||||
# Since the format may change, this is implemented for the 2.5 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):
|
||||
parser = configparser.ConfigParser(interpolation = None)
|
||||
parser.read_string(serialised)
|
||||
return int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
|
||||
|
||||
## Upgrades the preferences file from version 2.5 to 2.6.
|
||||
#
|
||||
# \param serialised The serialised form of a preferences file.
|
||||
# \param filename The name of the file to upgrade.
|
||||
def upgradePreferences(self, serialised, filename):
|
||||
parser = configparser.ConfigParser(interpolation = None)
|
||||
parser.read_string(serialised)
|
||||
|
||||
#Remove settings from the visible_settings.
|
||||
if parser.has_section("general") and "visible_settings" in parser["general"]:
|
||||
visible_settings = parser["general"]["visible_settings"].split(";")
|
||||
new_visible_settings = []
|
||||
for setting in visible_settings:
|
||||
if setting in _removed_settings:
|
||||
continue #Skip.
|
||||
if setting in _split_settings:
|
||||
for replaced_setting in _split_settings[setting]:
|
||||
new_visible_settings.append(replaced_setting)
|
||||
continue #Don't add the original.
|
||||
new_visible_settings.append(setting) #No special handling, so just add the original visible setting back.
|
||||
parser["general"]["visible_settings"] = ";".join(new_visible_settings)
|
||||
|
||||
#Change the version number in the file.
|
||||
if parser.has_section("general"): #It better have!
|
||||
parser["general"]["version"] = "5"
|
||||
|
||||
#Re-serialise the file.
|
||||
output = io.StringIO()
|
||||
parser.write(output)
|
||||
return [filename], [output.getvalue()]
|
||||
|
||||
## Upgrades an instance container from version 2.5 to 2.6.
|
||||
#
|
||||
# \param serialised The serialised form of a quality profile.
|
||||
# \param filename The name of the file to upgrade.
|
||||
def upgradeInstanceContainer(self, serialised, filename):
|
||||
parser = configparser.ConfigParser(interpolation = None)
|
||||
parser.read_string(serialised)
|
||||
|
||||
#Remove settings from the [values] section.
|
||||
if parser.has_section("values"):
|
||||
for removed_setting in (_removed_settings & parser["values"].keys()): #Both in keys that need to be removed and in keys present in the file.
|
||||
del parser["values"][removed_setting]
|
||||
for replaced_setting in (_split_settings.keys() & parser["values"].keys()):
|
||||
for replacement in _split_settings[replaced_setting]:
|
||||
parser["values"][replacement] = parser["values"][replaced_setting] #Copy to replacement before removing the original!
|
||||
del replaced_setting
|
||||
|
||||
#Change the version number in the file.
|
||||
if parser.has_section("general"):
|
||||
parser["general"]["version"] = "3"
|
||||
|
||||
#Re-serialise the file.
|
||||
output = io.StringIO()
|
||||
parser.write(output)
|
||||
return [filename], [output.getvalue()]
|
44
plugins/VersionUpgrade/VersionUpgrade25to26/__init__.py
Normal file
44
plugins/VersionUpgrade/VersionUpgrade25to26/__init__.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
# Copyright (c) 2017 Ultimaker B.V.
|
||||
# Cura is released under the terms of the AGPLv3 or higher.
|
||||
|
||||
from . import VersionUpgrade24to25
|
||||
|
||||
from UM.i18n import i18nCatalog
|
||||
catalog = i18nCatalog("cura")
|
||||
|
||||
upgrade = VersionUpgrade25to26.VersionUpgrade25to26()
|
||||
|
||||
def getMetaData():
|
||||
return {
|
||||
"plugin": {
|
||||
"name": catalog.i18nc("@label", "Version Upgrade 2.5 to 2.6"),
|
||||
"author": "Ultimaker",
|
||||
"version": "1.0",
|
||||
"description": catalog.i18nc("@info:whatsthis", "Upgrades configurations from Cura 2.5 to Cura 2.6."),
|
||||
"api": 3
|
||||
},
|
||||
"version_upgrade": {
|
||||
# From To Upgrade function
|
||||
("preferences", 4): ("preferences", 5, upgrade.upgradePreferences),
|
||||
("quality", 2): ("quality", 3, upgrade.upgradeInstanceContainer),
|
||||
("variant", 2): ("variant", 3, upgrade.upgradeInstanceContainer), #We can re-use upgradeContainerStack since there is nothing specific to quality, variant or user profiles being changed.
|
||||
("user", 2): ("user", 3, upgrade.upgradeInstanceContainer)
|
||||
},
|
||||
"sources": {
|
||||
"quality": {
|
||||
"get_version": upgrade.getCfgVersion,
|
||||
"location": {"./quality"}
|
||||
},
|
||||
"preferences": {
|
||||
"get_version": upgrade.getCfgVersion,
|
||||
"location": {"."}
|
||||
},
|
||||
"user": {
|
||||
"get_version": upgrade.getCfgVersion,
|
||||
"location": {"./user"}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def register(app):
|
||||
return { "version_upgrade": upgrade }
|
|
@ -0,0 +1,190 @@
|
|||
# Copyright (c) 2017 Ultimaker B.V.
|
||||
# Cura is released under the terms of the AGPLv3 or higher.
|
||||
|
||||
import configparser #To check whether the appropriate exceptions are raised.
|
||||
import pytest #To register tests with.
|
||||
|
||||
import VersionUpgrade24to25 #The module we're testing.
|
||||
|
||||
## Creates an instance of the upgrader to test with.
|
||||
@pytest.fixture
|
||||
def upgrader():
|
||||
return VersionUpgrade24to25.VersionUpgrade24to25()
|
||||
|
||||
test_cfg_version_good_data = [
|
||||
{
|
||||
"test_name": "Simple",
|
||||
"file_data": """[general]
|
||||
version = 1
|
||||
""",
|
||||
"version": 1
|
||||
},
|
||||
{
|
||||
"test_name": "Other Data Around",
|
||||
"file_data": """[nonsense]
|
||||
life = good
|
||||
|
||||
[general]
|
||||
version = 3
|
||||
|
||||
[values]
|
||||
layer_height = 0.12
|
||||
infill_sparse_density = 42
|
||||
""",
|
||||
"version": 3
|
||||
},
|
||||
{
|
||||
"test_name": "Negative Version", #Why not?
|
||||
"file_data": """[general]
|
||||
version = -20
|
||||
""",
|
||||
"version": -20
|
||||
}
|
||||
]
|
||||
|
||||
## Tests the technique that gets the version number from CFG files.
|
||||
#
|
||||
# \param data The parametrised data to test with. It contains a test name
|
||||
# to debug with, the serialised contents of a CFG file and the correct
|
||||
# version number in that CFG file.
|
||||
# \param upgrader The instance of the upgrade class to test.
|
||||
@pytest.mark.parametrize("data", test_cfg_version_good_data)
|
||||
def test_cfgVersionGood(data, upgrader):
|
||||
version = upgrader.getCfgVersion(data["file_data"])
|
||||
assert version == data["version"]
|
||||
|
||||
test_cfg_version_bad_data = [
|
||||
{
|
||||
"test_name": "Empty",
|
||||
"file_data": "",
|
||||
"exception": configparser.Error #Explicitly not specified further which specific error we're getting, because that depends on the implementation of configparser.
|
||||
},
|
||||
{
|
||||
"test_name": "No General",
|
||||
"file_data": """[values]
|
||||
layer_height = 0.1337
|
||||
""",
|
||||
"exception": configparser.Error
|
||||
},
|
||||
{
|
||||
"test_name": "No Version",
|
||||
"file_data": """[general]
|
||||
true = false
|
||||
""",
|
||||
"exception": configparser.Error
|
||||
},
|
||||
{
|
||||
"test_name": "Not a Number",
|
||||
"file_data": """[general]
|
||||
version = not-a-text-version-number
|
||||
""",
|
||||
"exception": ValueError
|
||||
}
|
||||
]
|
||||
|
||||
## Tests whether getting a version number from bad CFG files gives an
|
||||
# exception.
|
||||
#
|
||||
# \param data The parametrised data to test with. It contains a test name
|
||||
# to debug with, the serialised contents of a CFG file and the class of
|
||||
# exception it needs to throw.
|
||||
# \param upgrader The instance of the upgrader to test.
|
||||
@pytest.mark.parametrize("data", test_cfg_version_bad_data)
|
||||
def test_cfgVersionBad(data, upgrader):
|
||||
with pytest.raises(data["exception"]):
|
||||
upgrader.getCfgVersion(data["file_data"])
|
||||
|
||||
test_upgrade_preferences_removed_settings_data = [
|
||||
{
|
||||
"test_name": "Removed Setting",
|
||||
"file_data": """[general]
|
||||
visible_settings = baby;you;know;how;I;like;to;start_layers_at_same_position
|
||||
""",
|
||||
},
|
||||
{
|
||||
"test_name": "No Removed Setting",
|
||||
"file_data": """[general]
|
||||
visible_settings = baby;you;now;how;I;like;to;eat;chocolate;muffins
|
||||
"""
|
||||
},
|
||||
{
|
||||
"test_name": "No Visible Settings Key",
|
||||
"file_data": """[general]
|
||||
cura = cool
|
||||
"""
|
||||
},
|
||||
{
|
||||
"test_name": "No General Category",
|
||||
"file_data": """[foos]
|
||||
foo = bar
|
||||
"""
|
||||
}
|
||||
]
|
||||
|
||||
## Tests whether the settings that should be removed are removed for the 2.5
|
||||
# version of preferences.
|
||||
@pytest.mark.parametrize("data", test_upgrade_preferences_removed_settings_data)
|
||||
def test_upgradePreferencesRemovedSettings(data, upgrader):
|
||||
#Get the settings from the original file.
|
||||
original_parser = configparser.ConfigParser(interpolation = None)
|
||||
original_parser.read_string(data["file_data"])
|
||||
settings = set()
|
||||
if original_parser.has_section("general") and "visible_settings" in original_parser["general"]:
|
||||
settings = set(original_parser["general"]["visible_settings"].split(";"))
|
||||
|
||||
#Perform the upgrade.
|
||||
_, upgraded_preferences = upgrader.upgradePreferences(data["file_data"], "<string>")
|
||||
upgraded_preferences = upgraded_preferences[0]
|
||||
|
||||
#Find whether the removed setting is removed from the file now.
|
||||
settings -= VersionUpgrade24to25._removed_settings
|
||||
parser = configparser.ConfigParser(interpolation = None)
|
||||
parser.read_string(upgraded_preferences)
|
||||
assert (parser.has_section("general") and "visible_settings" in parser["general"]) == (len(settings) > 0) #If there are settings, there must also be a preference.
|
||||
if settings:
|
||||
assert settings == set(parser["general"]["visible_settings"].split(";"))
|
||||
|
||||
test_upgrade_instance_container_removed_settings_data = [
|
||||
{
|
||||
"test_name": "Removed Setting",
|
||||
"file_data": """[values]
|
||||
layer_height = 0.1337
|
||||
start_layers_at_same_position = True
|
||||
"""
|
||||
},
|
||||
{
|
||||
"test_name": "No Removed Setting",
|
||||
"file_data": """[values]
|
||||
oceans_number = 11
|
||||
"""
|
||||
},
|
||||
{
|
||||
"test_name": "No Values Category",
|
||||
"file_data": """[general]
|
||||
type = instance_container
|
||||
"""
|
||||
}
|
||||
]
|
||||
|
||||
## Tests whether the settings that should be removed are removed for the 2.5
|
||||
# version of instance containers.
|
||||
@pytest.mark.parametrize("data", test_upgrade_instance_container_removed_settings_data)
|
||||
def test_upgradeInstanceContainerRemovedSettings(data, upgrader):
|
||||
#Get the settings from the original file.
|
||||
original_parser = configparser.ConfigParser(interpolation = None)
|
||||
original_parser.read_string(data["file_data"])
|
||||
settings = set()
|
||||
if original_parser.has_section("values"):
|
||||
settings = set(original_parser["values"])
|
||||
|
||||
#Perform the upgrade.
|
||||
_, upgraded_container = upgrader.upgradeInstanceContainer(data["file_data"], "<string>")
|
||||
upgraded_container = upgraded_container[0]
|
||||
|
||||
#Find whether the forbidden setting is still in the container.
|
||||
settings -= VersionUpgrade24to25._removed_settings
|
||||
parser = configparser.ConfigParser(interpolation = None)
|
||||
parser.read_string(upgraded_container)
|
||||
assert parser.has_section("values") == (len(settings) > 0) #If there are settings, there must also be the values category.
|
||||
if settings:
|
||||
assert settings == set(parser["values"])
|
Loading…
Add table
Add a link
Reference in a new issue