From 88b36ad3d7b837dc63590831cddecccd7cf07b21 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Sun, 3 Jul 2016 23:31:10 +0200 Subject: [PATCH] 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. --- .../VersionUpgrade21to22/Preferences.py | 3 ++- .../VersionUpgrade21to22.py | 24 +++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/plugins/VersionUpgrade/VersionUpgrade21to22/Preferences.py b/plugins/VersionUpgrade/VersionUpgrade21to22/Preferences.py index 18391880e0..37cb3ccb9f 100644 --- a/plugins/VersionUpgrade/VersionUpgrade21to22/Preferences.py +++ b/plugins/VersionUpgrade/VersionUpgrade21to22/Preferences.py @@ -53,7 +53,8 @@ class Preferences: visible_settings = self._config.get("machines", "setting_visibility") visible_settings = visible_settings.split(",") 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) self._config.set("machines", "setting_visibility", value = visible_settings) diff --git a/plugins/VersionUpgrade/VersionUpgrade21to22/VersionUpgrade21to22.py b/plugins/VersionUpgrade/VersionUpgrade21to22/VersionUpgrade21to22.py index 641931c5a4..e1632e0d85 100644 --- a/plugins/VersionUpgrade/VersionUpgrade21to22/VersionUpgrade21to22.py +++ b/plugins/VersionUpgrade/VersionUpgrade21to22/VersionUpgrade21to22.py @@ -16,6 +16,10 @@ _profile_translation = { "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. # # It converts the machine instances and profiles. @@ -85,7 +89,7 @@ class VersionUpgrade21to22(VersionUpgrade): def translateProfile(profile): if profile in _profile_translation: 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. # @@ -105,16 +109,12 @@ class VersionUpgrade21to22(VersionUpgrade): settings[key] = "off" if (value == "False") else "all" 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 - # the input parameter. - # - # \param settings A list of setting names to update. - # \return The same list. + # \param setting The name of a setting in Cura 2.1. + # \return The name of the corresponding setting in Cura 2.2. @staticmethod - def translateSettingNames(settings): - for i in range(0, len(settings)): - if settings[i] == "speed_support_lines": - settings[i] = "speed_support_infill" - return settings \ No newline at end of file + def translateSettingName(setting): + if setting in _setting_name_translation: + return _setting_name_translation[setting] + return setting #Doesn't need to be translated. \ No newline at end of file