From 89f113e1904a7b4c6126810e49ffe237551081ea Mon Sep 17 00:00:00 2001 From: Athanasius Date: Wed, 1 Jul 2020 15:33:27 +0100 Subject: [PATCH] Use Preferences Save Serial Number instead of flag * new class `PrefsVersion` in prefs.py. A singleton `prefsSaved` (note case) is created. * When new preferences are added and require defaults on first run the code should use: `if prefsVersion.shouldSetDefaults(, []):` to check if defaults should be set in preferences. So if prior release was '3.4.6.0' and you've added a new preference with defaults you should call this with '3.4.6.0' as the first argument. The is really only for historical purposes, as a fallback in case no 'PrefsVersion' has yet been set in the user's Registry/settings file. * Any code that adds such a new preference **MUST** make changes to the `versions` dictionary in the PrefsVersion class. 1. Add the predicted next version to the dictionary, with number one higher than 'current' 2. Set 'current' equal to that new value. Obviously if other post-last-release code has already done this then you don't need to. Failure to update the versions dictionary in this manner will lead to an Exception being raised, and the code the preferences are for failing (i.e. EDDN means no EDDN tab on Settings). Closes #407 --- plugins/eddn.py | 4 ++- prefs.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/plugins/eddn.py b/plugins/eddn.py index 3ded1ea0..95667e71 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -14,6 +14,8 @@ import tkinter as tk from ttkHyperlinkLabel import HyperlinkLabel import myNotebook as nb +from prefs import prefsVersion + if sys.platform != 'win32': from fcntl import lockf, LOCK_EX, LOCK_NB @@ -347,7 +349,7 @@ def plugin_prefs(parent, cmdr, is_beta): BUTTONX = 12 # indent Checkbuttons and Radiobuttons PADY = 2 # close spacing - if not config.getint('PrefsDidSave'): + if prefsVersion.shouldSetDefaults('0.0.0.0', not bool(config.getint('output'))): output = (config.OUT_MKT_EDDN | config.OUT_SYS_EDDN) # default settings else: output = config.getint('output') diff --git a/prefs.py b/prefs.py index f3f7d0c8..5a1ed4c2 100644 --- a/prefs.py +++ b/prefs.py @@ -10,7 +10,7 @@ from tkinter import colorchooser as tkColorChooser from ttkHyperlinkLabel import HyperlinkLabel import myNotebook as nb -from config import applongname, config +from config import applongname, config, appversion from hotkey import hotkeymgr from l10n import Translations from monitor import monitor @@ -18,6 +18,70 @@ from theme import theme import plug +########################################################################### +# Versioned preferences, so we know whether to set an 'on' default on +# 'new' preferences, or not. +########################################################################### + + +class PrefsVersion(object): + versions = { + '0.0.0.0': 1, + '1.0.0.0': 2, + '3.4.6.0': 3, + '3.5.1.0': 4, + # Only add new versions that add new Preferences + 'current': 4, # Should always match the last specific version, but only increment after you've added the new version. Guess at it if anticipating a new version. + } + + def __init__(self): + return + + def stringToSerial(self, versionStr: str) -> int: + """ + Convert a version string into a preferences version serial number. + + If the version string isn't known returns the 'current' (latest) serial number. + + :param versionStr: + :return int: + """ + if versionStr in self.versions: + return self.versions[versionStr] + + return self.versions['current'] + + ########################################################################### + # Should defaults be set, given the settings were added after 'addedAfter' ? + # + # config.get('PrefsVersion') is the version preferences we last saved for + ########################################################################### + def shouldSetDefaults(self, addedAfter: str, oldTest : bool=True) -> bool: + pv = config.getint('PrefsVersion') + # If no PrefsVersion yet exists then return oldTest + if not pv: + return oldTest + + # Convert addedAfter to a version serial number + if addedAfter not in self.versions: + # Assume it was added at the start + aa = 1 + else: + aa = self.versions[addedAfter] + # Sanity check, if something was added after then current should be greater + if aa >= self.versions['current']: + raise Exception('ERROR: Call to prefs.py:PrefsVersion.shouldSetDefaults() with "addedAfter" >= current latest in "versions" table. You probably need to increase "current" serial number.') + + # If this preference was added after the saved PrefsVersion we should set defaults + if aa >= pv: + return True + + return False + ########################################################################### + +prefsVersion = PrefsVersion() +########################################################################### + if platform == 'darwin': import objc from Foundation import NSFileManager @@ -103,7 +167,7 @@ class PreferencesDialog(tk.Toplevel): outframe = nb.Frame(notebook) outframe.columnconfigure(0, weight=1) - if not config.getint('PrefsDidSave'): + if prefsVersion.shouldSetDefaults('0.0.0.0', not bool(config.getint('output'))): output = config.OUT_SHIP # default settings else: output = config.getint('output') @@ -502,7 +566,7 @@ class PreferencesDialog(tk.Toplevel): def apply(self): - config.set('PrefsDidSave', 1) + config.set('PrefsVersion', prefsVersion.stringToSerial(appversion)) config.set('output', (self.out_td.get() and config.OUT_MKT_TD) + (self.out_csv.get() and config.OUT_MKT_CSV) +