Skip to content

Commit 6818fc7

Browse files
committed
cleaned up the usage of globals
refactored names to make more sense
1 parent c580cbe commit 6818fc7

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

nxt_editor/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def launch_editor(paths=None, start_rpc=False):
9393
app = existing
9494
else:
9595
app = _new_qapp()
96-
from nxt_editor.dialogs import CopyPrefsDialogue
97-
CopyPrefsDialogue.show_message() # Show message if upgrade possible
96+
from nxt_editor.dialogs import UpgradePrefsDialogue
97+
UpgradePrefsDialogue.confirm_upgrade_if_possible()
9898
instance = show_new_editor(paths, start_rpc)
9999
app.setActiveWindow(instance)
100100
if not existing:

nxt_editor/dialogs.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -411,30 +411,29 @@ def show_message(cls, *args, **kwargs):
411411
return False
412412

413413

414-
class CopyPrefsDialogue(NxtConfirmDialog):
415-
title_text = (f'Copy version {user_dir.UPGRADE_PREFS_FROM_VERSION} '
416-
f'Preferences?')
417-
button_text = {
418-
NxtConfirmDialog.Ok: f'Copy v'
419-
f'{user_dir.UPGRADE_PREFS_FROM_VERSION} prefs',
420-
NxtConfirmDialog.Cancel: 'Use default preferences'
421-
}
422-
info = ('Would you like to copy preferences from an older version of '
423-
'NXT?\nSome things like the window layout may not be preserved.')
424-
425-
def __int__(self):
426-
super(CopyPrefsDialogue, self).__init__(text=self.title_text,
427-
info=self.info,
428-
button_text=self.button_text)
414+
class UpgradePrefsDialogue(NxtConfirmDialog):
415+
def __int__(self, title_text, info, button_text):
416+
super(UpgradePrefsDialogue, self).__init__(text=title_text,
417+
info=info,
418+
button_text=button_text)
429419

430420
@classmethod
431-
def show_message(cls):
421+
def confirm_upgrade_if_possible(cls):
422+
432423
if not user_dir.UPGRADABLE_PREFS:
433424
return
434-
do_upgrade = super().show_message(text=cls.title_text, info=cls.info,
435-
button_text=cls.button_text)
425+
from_version = user_dir.UPGRADE_PREFS_FROM_VERSION
426+
title_text = f'Copy version {from_version} Preferences?'
427+
button_text = {
428+
NxtConfirmDialog.Ok: f'Copy v{from_version} prefs',
429+
NxtConfirmDialog.Cancel: 'Use default preferences'
430+
}
431+
i = ('Would you like to copy preferences from an older version of NXT?'
432+
'\nSome things like the window layout may not be preserved.')
433+
do_upgrade = super().show_message(text=title_text, info=i,
434+
button_text=button_text)
436435
if do_upgrade:
437-
user_dir.upgrade_prefs()
436+
user_dir.upgrade_prefs(user_dir.UPGRADABLE_PREFS)
438437

439438

440439
class UnsavedLayersDialogue(QtWidgets.QDialog):

nxt_editor/user_dir.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
HOTKEYS_PREF = os.path.join(PREF_DIR, 'hotkeys.json')
3434
MAX_RECENT_FILES = 10
3535
JSON_PREFS = [USER_PREFS_PATH, BREAKPOINT_FILE, SKIPPOINT_FILE, HOTKEYS_PREF]
36-
UPGRADABLE_PREFS = []
37-
UPGRADE_PREFS_FROM_VERSION = -1
3836
broken_files = {}
3937

4038

@@ -50,15 +48,17 @@ def ensure_pref_dir_exists():
5048
raise Exception('Failed to generate user dir {}' + USER_DIR)
5149

5250

53-
def check_for_upgradable_prefs():
51+
def get_upgradable_prefs():
5452
"""
5553
Identify preference files that can be safely upgraded
5654
between major editor versions. Only existing preference files
5755
from the nearest older version are copied; missing files are skipped
58-
without warnings.
56+
without warnings. Returns a list of prefs that can upgrade and the
57+
versio number they're coming from.
58+
:returns: (list, int)
5959
"""
60-
global UPGRADABLE_PREFS
61-
global UPGRADE_PREFS_FROM_VERSION
60+
upgradable_prefs = []
61+
upgrade_prefs_from_version = -1
6262
for pref_file in JSON_PREFS:
6363
if os.path.isfile(pref_file):
6464
break
@@ -72,20 +72,22 @@ def check_for_upgradable_prefs():
7272
if os.path.isfile(old_pref_file):
7373
# In the future if we change the structure of the json
7474
# prefs we'll need a way to convert them or skip
75-
UPGRADABLE_PREFS.append(old_pref_file)
76-
if UPGRADABLE_PREFS:
77-
UPGRADE_PREFS_FROM_VERSION = dir_num
75+
upgradable_prefs.append(old_pref_file)
76+
if upgradable_prefs:
77+
upgrade_prefs_from_version = dir_num
7878
break
7979
dir_num -= 1
80+
return upgradable_prefs, upgrade_prefs_from_version
8081

8182

82-
def upgrade_prefs():
83+
def upgrade_prefs(prefs_to_upgrade):
8384
"""
8485
Copies old 'upgradeable' prefs to current pref dir, will eat and
8586
exception raised by shutil.copy. In the future this function may do more
8687
than simply copy.
88+
:param prefs_to_upgrade: List of pref filepaths to upgrade
8789
"""
88-
for pref_file in UPGRADABLE_PREFS:
90+
for pref_file in prefs_to_upgrade:
8991
try:
9092
shutil.copy(pref_file, PREF_DIR)
9193
except Exception as e:
@@ -94,7 +96,8 @@ def upgrade_prefs():
9496

9597

9698
ensure_pref_dir_exists()
97-
check_for_upgradable_prefs()
99+
# Must check these before we setup the defaults at the bottom of this file
100+
UPGRADABLE_PREFS, UPGRADE_PREFS_FROM_VERSION = get_upgradable_prefs()
98101

99102

100103
class USER_PREF():

0 commit comments

Comments
 (0)