Skip to content

Commit

Permalink
cleaned up the usage of globals
Browse files Browse the repository at this point in the history
refactored names to make more sense
  • Loading branch information
ImLucasBrown committed Nov 23, 2024
1 parent c580cbe commit 6818fc7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
4 changes: 2 additions & 2 deletions nxt_editor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def launch_editor(paths=None, start_rpc=False):
app = existing
else:
app = _new_qapp()
from nxt_editor.dialogs import CopyPrefsDialogue
CopyPrefsDialogue.show_message() # Show message if upgrade possible
from nxt_editor.dialogs import UpgradePrefsDialogue
UpgradePrefsDialogue.confirm_upgrade_if_possible()
instance = show_new_editor(paths, start_rpc)
app.setActiveWindow(instance)
if not existing:
Expand Down
37 changes: 18 additions & 19 deletions nxt_editor/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,30 +411,29 @@ def show_message(cls, *args, **kwargs):
return False


class CopyPrefsDialogue(NxtConfirmDialog):
title_text = (f'Copy version {user_dir.UPGRADE_PREFS_FROM_VERSION} '
f'Preferences?')
button_text = {
NxtConfirmDialog.Ok: f'Copy v'
f'{user_dir.UPGRADE_PREFS_FROM_VERSION} prefs',
NxtConfirmDialog.Cancel: 'Use default preferences'
}
info = ('Would you like to copy preferences from an older version of '
'NXT?\nSome things like the window layout may not be preserved.')

def __int__(self):
super(CopyPrefsDialogue, self).__init__(text=self.title_text,
info=self.info,
button_text=self.button_text)
class UpgradePrefsDialogue(NxtConfirmDialog):
def __int__(self, title_text, info, button_text):
super(UpgradePrefsDialogue, self).__init__(text=title_text,
info=info,
button_text=button_text)

@classmethod
def show_message(cls):
def confirm_upgrade_if_possible(cls):

if not user_dir.UPGRADABLE_PREFS:
return
do_upgrade = super().show_message(text=cls.title_text, info=cls.info,
button_text=cls.button_text)
from_version = user_dir.UPGRADE_PREFS_FROM_VERSION
title_text = f'Copy version {from_version} Preferences?'
button_text = {
NxtConfirmDialog.Ok: f'Copy v{from_version} prefs',
NxtConfirmDialog.Cancel: 'Use default preferences'
}
i = ('Would you like to copy preferences from an older version of NXT?'
'\nSome things like the window layout may not be preserved.')
do_upgrade = super().show_message(text=title_text, info=i,
button_text=button_text)
if do_upgrade:
user_dir.upgrade_prefs()
user_dir.upgrade_prefs(user_dir.UPGRADABLE_PREFS)


class UnsavedLayersDialogue(QtWidgets.QDialog):
Expand Down
27 changes: 15 additions & 12 deletions nxt_editor/user_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
HOTKEYS_PREF = os.path.join(PREF_DIR, 'hotkeys.json')
MAX_RECENT_FILES = 10
JSON_PREFS = [USER_PREFS_PATH, BREAKPOINT_FILE, SKIPPOINT_FILE, HOTKEYS_PREF]
UPGRADABLE_PREFS = []
UPGRADE_PREFS_FROM_VERSION = -1
broken_files = {}


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


def check_for_upgradable_prefs():
def get_upgradable_prefs():
"""
Identify preference files that can be safely upgraded
between major editor versions. Only existing preference files
from the nearest older version are copied; missing files are skipped
without warnings.
without warnings. Returns a list of prefs that can upgrade and the
versio number they're coming from.
:returns: (list, int)
"""
global UPGRADABLE_PREFS
global UPGRADE_PREFS_FROM_VERSION
upgradable_prefs = []
upgrade_prefs_from_version = -1
for pref_file in JSON_PREFS:
if os.path.isfile(pref_file):
break
Expand All @@ -72,20 +72,22 @@ def check_for_upgradable_prefs():
if os.path.isfile(old_pref_file):
# In the future if we change the structure of the json
# prefs we'll need a way to convert them or skip
UPGRADABLE_PREFS.append(old_pref_file)
if UPGRADABLE_PREFS:
UPGRADE_PREFS_FROM_VERSION = dir_num
upgradable_prefs.append(old_pref_file)
if upgradable_prefs:
upgrade_prefs_from_version = dir_num
break
dir_num -= 1
return upgradable_prefs, upgrade_prefs_from_version


def upgrade_prefs():
def upgrade_prefs(prefs_to_upgrade):
"""
Copies old 'upgradeable' prefs to current pref dir, will eat and
exception raised by shutil.copy. In the future this function may do more
than simply copy.
:param prefs_to_upgrade: List of pref filepaths to upgrade
"""
for pref_file in UPGRADABLE_PREFS:
for pref_file in prefs_to_upgrade:
try:
shutil.copy(pref_file, PREF_DIR)
except Exception as e:
Expand All @@ -94,7 +96,8 @@ def upgrade_prefs():


ensure_pref_dir_exists()
check_for_upgradable_prefs()
# Must check these before we setup the defaults at the bottom of this file
UPGRADABLE_PREFS, UPGRADE_PREFS_FROM_VERSION = get_upgradable_prefs()


class USER_PREF():
Expand Down

0 comments on commit 6818fc7

Please sign in to comment.