~alisonken1/openlp/pjlink2-n

« back to all changes in this revision

Viewing changes to openlp/core/common/settings.py

  • Committer: Tim Bentley
  • Author(s): Raoul Snyman
  • Date: 2017-11-16 18:52:40 UTC
  • mfrom: (2782.3.9 settings-upgrade)
  • Revision ID: tim.bentley@gmail.com-20171116185240-icqqpzfy690eo1tp
Add multi-to-one settings upgrading, and merge all the post-2.4 settings changes into __setting_upgrade_2__

Add this to your merge proposal:
--------------------------------------------------------------------------------
lp:~raoul-snyman/openlp/settings-upgrade (revision 2791)
https://ci.openlp.io/job/Branch-01-Pull/2295/                          [SUCCESS]
https://ci.openlp.io/job/Branch-02-Functional-Tests/2196/              [SUCCESS]
https://ci.openlp.io/job/Branch-03-Interface-Tests/2074...

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
# Fix for bug #1014422.
42
42
X11_BYPASS_DEFAULT = True
43
 
if is_linux():
 
43
if is_linux():                                                                              # pragma: no cover
44
44
    # Default to False on Gnome.
45
45
    X11_BYPASS_DEFAULT = bool(not os.environ.get('GNOME_DESKTOP_SESSION_ID'))
46
46
    # Default to False on Xfce.
206
206
        'projector/source dialog type': 0  # Source select dialog box type
207
207
    }
208
208
    __file_path__ = ''
 
209
    # Settings upgrades prior to 3.0
209
210
    __setting_upgrade_1__ = [
210
 
        # Changed during 2.2.x development.
211
211
        ('songs/search as type', 'advanced/search as type', []),
212
212
        ('media/players', 'media/players_temp', [(media_players_conv, None)]),  # Convert phonon to system
213
213
        ('media/players_temp', 'media/players', []),  # Move temp setting from above to correct setting
 
214
    ]
 
215
    # Settings upgrades for 3.0 (aka 2.6)
 
216
    __setting_upgrade_2__ = [
214
217
        ('advanced/default color', 'core/logo background color', []),  # Default image renamed + moved to general > 2.4.
215
218
        ('advanced/default image', 'core/logo file', []),  # Default image renamed + moved to general after 2.4.
216
219
        ('remotes/https enabled', '', []),
231
234
        # Last search type was renamed to last used search type in 2.6 since Bible search value type changed in 2.6.
232
235
        ('songs/last search type', 'songs/last used search type', []),
233
236
        ('bibles/last search type', '', []),
234
 
        ('custom/last search type', 'custom/last used search type', [])]
235
 
 
236
 
    __setting_upgrade_2__ = [
 
237
        ('custom/last search type', 'custom/last used search type', []),
237
238
        # The following changes are being made for the conversion to using Path objects made in 2.6 development
238
239
        ('advanced/data path', 'advanced/data path', [(str_to_path, None)]),
239
240
        ('crashreport/last directory', 'crashreport/last directory', [(str_to_path, None)]),
467
468
        for version in range(current_version, __version__):
468
469
            version += 1
469
470
            upgrade_list = getattr(self, '__setting_upgrade_{version}__'.format(version=version))
470
 
            for old_key, new_key, rules in upgrade_list:
 
471
            for old_keys, new_key, rules in upgrade_list:
471
472
                # Once removed we don't have to do this again. - Can be removed once fully switched to the versioning
472
473
                # system.
473
 
                if not self.contains(old_key):
 
474
                if not isinstance(old_keys, (tuple, list)):
 
475
                    old_keys = [old_keys]
 
476
                if any([not self.contains(old_key) for old_key in old_keys]):
 
477
                    log.warning('One of {} does not exist, skipping upgrade'.format(old_keys))
474
478
                    continue
475
479
                if new_key:
476
480
                    # Get the value of the old_key.
477
 
                    old_value = super(Settings, self).value(old_key)
 
481
                    old_values = [super(Settings, self).value(old_key) for old_key in old_keys]
478
482
                    # When we want to convert the value, we have to figure out the default value (because we cannot get
479
483
                    # the default value from the central settings dict.
480
484
                    if rules:
481
 
                        default_value = rules[0][1]
482
 
                        old_value = self._convert_value(old_value, default_value)
 
485
                        default_values = rules[0][1]
 
486
                        if not isinstance(default_values, (list, tuple)):
 
487
                            default_values = [default_values]
 
488
                        old_values = [self._convert_value(old_value, default_value)
 
489
                                      for old_value, default_value in zip(old_values, default_values)]
483
490
                    # Iterate over our rules and check what the old_value should be "converted" to.
484
 
                    for new, old in rules:
 
491
                    new_value = None
 
492
                    for new_rule, old_rule in rules:
485
493
                        # If the value matches with the condition (rule), then use the provided value. This is used to
486
494
                        # convert values. E. g. an old value 1 results in True, and 0 in False.
487
 
                        if callable(new):
488
 
                            old_value = new(old_value)
489
 
                        elif old == old_value:
490
 
                            old_value = new
 
495
                        if callable(new_rule):
 
496
                            new_value = new_rule(*old_values)
 
497
                        elif old_rule in old_values:
 
498
                            new_value = new_rule
491
499
                            break
492
 
                    self.setValue(new_key, old_value)
493
 
                if new_key != old_key:
494
 
                    self.remove(old_key)
495
 
        self.setValue('settings/version', version)
 
500
                    self.setValue(new_key, new_value)
 
501
                [self.remove(old_key) for old_key in old_keys if old_key != new_key]
 
502
            self.setValue('settings/version', version)
496
503
 
497
504
    def value(self, key):
498
505
        """