From 63c2ff52e94256367087a24a0ed6ad812a682f92 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 29 Jun 2020 14:28:20 +0100 Subject: [PATCH 1/3] Fix an Output/EDDN option combination causing defaults to get re-set Change Output and EDDN options to only get set to defaults if key 'PrefsDidSave' is not present and true. This gets a value of 0x1 saved any time preferences are applied. The issue was that if you had sufficient options set such that the saved 'output' value was 0x0 then that would evaluate to false and cause the defaults to get set. Fixes #407 --- plugins/eddn.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/eddn.py b/plugins/eddn.py index c235ec74..3ded1ea0 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -347,7 +347,10 @@ def plugin_prefs(parent, cmdr, is_beta): BUTTONX = 12 # indent Checkbuttons and Radiobuttons PADY = 2 # close spacing - output = config.getint('output') or (config.OUT_MKT_EDDN | config.OUT_SYS_EDDN) # default settings + if not config.getint('PrefsDidSave'): + output = (config.OUT_MKT_EDDN | config.OUT_SYS_EDDN) # default settings + else: + output = config.getint('output') eddnframe = nb.Frame(parent) @@ -371,7 +374,7 @@ def prefsvarchanged(event=None): def prefs_changed(cmdr, is_beta): config.set('output', - (config.getint('output') & (config.OUT_MKT_TD | config.OUT_MKT_CSV | config.OUT_SHIP |config. OUT_MKT_MANUAL)) + + (config.getint('output') & (config.OUT_MKT_TD | config.OUT_MKT_CSV | config.OUT_SHIP |config.OUT_MKT_MANUAL)) + (this.eddn_station.get() and config.OUT_MKT_EDDN) + (this.eddn_system.get() and config.OUT_SYS_EDDN) + (this.eddn_delay.get() and config.OUT_SYS_DELAY)) From ecdd65e0acac1def38fce915d810db940280232e Mon Sep 17 00:00:00 2001 From: Athanasius Date: Tue, 30 Jun 2020 15:22:50 +0100 Subject: [PATCH 2/3] prefs.py change for that output settings fix Addresses #407 --- prefs.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/prefs.py b/prefs.py index 329869c6..f3f7d0c8 100644 --- a/prefs.py +++ b/prefs.py @@ -103,7 +103,10 @@ class PreferencesDialog(tk.Toplevel): outframe = nb.Frame(notebook) outframe.columnconfigure(0, weight=1) - output = config.getint('output') or config.OUT_SHIP # default settings + if not config.getint('PrefsDidSave'): + output = config.OUT_SHIP # default settings + else: + output = config.getint('output') self.out_label = nb.Label(outframe, text=_('Please choose what data to save')) self.out_label.grid(columnspan=2, padx=PADX, sticky=tk.W) @@ -499,6 +502,7 @@ class PreferencesDialog(tk.Toplevel): def apply(self): + config.set('PrefsDidSave', 1) config.set('output', (self.out_td.get() and config.OUT_MKT_TD) + (self.out_csv.get() and config.OUT_MKT_CSV) + From 89f113e1904a7b4c6126810e49ffe237551081ea Mon Sep 17 00:00:00 2001 From: Athanasius Date: Wed, 1 Jul 2020 15:33:27 +0100 Subject: [PATCH 3/3] 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) +