Make translateSettingName use dictionary look-up

This solution is a bit neater in code. It makes the function perform a single purpose, since it no longer translates a list of setting names but just one. Also it now neatly puts the translations in a separate, easy-to-modify dict. Only disadvantage is when simple key look-up is not sufficient, such as when renaming lots of settings at once, where substring matching would make the code a bit shorter. But we shouldn't do such a rename anyway.

Contributes to issue CURA-844.
This commit is contained in:
Ghostkeeper 2016-07-03 23:31:10 +02:00
parent f07598a228
commit 88b36ad3d7
2 changed files with 14 additions and 13 deletions

View file

@ -53,7 +53,8 @@ class Preferences:
visible_settings = self._config.get("machines", "setting_visibility") visible_settings = self._config.get("machines", "setting_visibility")
visible_settings = visible_settings.split(",") visible_settings = visible_settings.split(",")
import VersionUpgrade21to22 #Import here to prevent a circular dependency. import VersionUpgrade21to22 #Import here to prevent a circular dependency.
VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateSettingNames(visible_settings) visible_settings = [VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateSettingName(setting_name)
for setting_name in visible_settings]
visible_settings = ",".join(visible_settings) visible_settings = ",".join(visible_settings)
self._config.set("machines", "setting_visibility", value = visible_settings) self._config.set("machines", "setting_visibility", value = visible_settings)

View file

@ -16,6 +16,10 @@ _profile_translation = {
"CPE": "generic_cpe" "CPE": "generic_cpe"
} }
_setting_name_translation = {
"speed_support_lines": "speed_support_infill"
}
## Converts configuration from Cura 2.1's file formats to Cura 2.2's. ## Converts configuration from Cura 2.1's file formats to Cura 2.2's.
# #
# It converts the machine instances and profiles. # It converts the machine instances and profiles.
@ -85,7 +89,7 @@ class VersionUpgrade21to22(VersionUpgrade):
def translateProfile(profile): def translateProfile(profile):
if profile in _profile_translation: if profile in _profile_translation:
return _profile_translation[profile] return _profile_translation[profile]
return profile return profile #Doesn't need to be translated.
## Updates settings for the change from Cura 2.1 to 2.2. ## Updates settings for the change from Cura 2.1 to 2.2.
# #
@ -105,16 +109,12 @@ class VersionUpgrade21to22(VersionUpgrade):
settings[key] = "off" if (value == "False") else "all" settings[key] = "off" if (value == "False") else "all"
return settings return settings
## Translates setting names for the change from Cura 2.1 to 2.2. ## Translates a setting name for the change from Cura 2.1 to 2.2.
# #
# The setting names are changed in-place in the provided list. This changes # \param setting The name of a setting in Cura 2.1.
# the input parameter. # \return The name of the corresponding setting in Cura 2.2.
#
# \param settings A list of setting names to update.
# \return The same list.
@staticmethod @staticmethod
def translateSettingNames(settings): def translateSettingName(setting):
for i in range(0, len(settings)): if setting in _setting_name_translation:
if settings[i] == "speed_support_lines": return _setting_name_translation[setting]
settings[i] = "speed_support_infill" return setting #Doesn't need to be translated.
return settings