Skip to content

Commit

Permalink
Merge pull request #281 from nxt-dev/features/copy_old_prefs
Browse files Browse the repository at this point in the history
Copy old prefs flow
  • Loading branch information
ImLucasBrown authored Nov 24, 2024
2 parents c27cc13 + 6818fc7 commit 77f7598
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 8 deletions.
6 changes: 4 additions & 2 deletions nxt_editor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _new_qapp():
return app


def launch_editor(paths=None, start_rpc=True):
def launch_editor(paths=None, start_rpc=False):
"""Launch an instance of the editor. Will attach to existing QApp if found,
otherwise will create and open one.
"""
Expand All @@ -93,14 +93,16 @@ def launch_editor(paths=None, start_rpc=True):
app = existing
else:
app = _new_qapp()
from nxt_editor.dialogs import UpgradePrefsDialogue
UpgradePrefsDialogue.confirm_upgrade_if_possible()
instance = show_new_editor(paths, start_rpc)
app.setActiveWindow(instance)
if not existing:
app.exec_()
return instance


def show_new_editor(paths=None, start_rpc=True):
def show_new_editor(paths=None, start_rpc=False):
path = None
if paths and isinstance(paths, list):
path = paths[0]
Expand Down
6 changes: 4 additions & 2 deletions nxt_editor/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class FONTS(object):
DEFAULT_SIZE = 10


_pref_dir_name = str(EDITOR_VERSION.MAJOR)
PREF_DIR = os.path.join(USER_DIR, 'prefs', _pref_dir_name)
PREF_DIR_INT = EDITOR_VERSION.MAJOR
PREF_DIR_NAME = 'prefs'
_pref_dir_num = str(PREF_DIR_INT)
PREF_DIR = os.path.join(USER_DIR, PREF_DIR_NAME, _pref_dir_num)

NXT_WEBSITE = 'https://nxt-dev.github.io/'
26 changes: 26 additions & 0 deletions nxt_editor/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ def __init__(self, text='Title', info='Confirm something!',
self.setIcon(icon)
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.setStandardButtons(self.Ok | self.Cancel)
self.setWindowTitle(text)
if button_text:
self.setButtonText(self.Ok, button_text.get(self.Ok, 'Ok'))
self.setButtonText(self.Cancel, button_text.get(self.Cancel,
Expand All @@ -410,6 +411,31 @@ def show_message(cls, *args, **kwargs):
return False


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 confirm_upgrade_if_possible(cls):

if not user_dir.UPGRADABLE_PREFS:
return
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.UPGRADABLE_PREFS)


class UnsavedLayersDialogue(QtWidgets.QDialog):
@classmethod
def save_before_exit(cls, stage_models, main_window):
Expand Down
2 changes: 1 addition & 1 deletion nxt_editor/integration/maya/plug-ins/nxt_maya.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def doIt(self, args):
if __NXT_INSTANCE__:
__NXT_INSTANCE__.close()
return
nxt_win = nxt_editor.main_window.MainWindow()
nxt_win = nxt_editor.show_new_editor()
if 'win32' in sys.platform:
# gives nxt it's own entry on taskbar
nxt_win.setWindowFlags(QtCore.Qt.Window)
Expand Down
2 changes: 1 addition & 1 deletion nxt_editor/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MainWindow(QtWidgets.QMainWindow):
new_log_signal = QtCore.Signal(logging.LogRecord)
font_size_changed = QtCore.Signal(int)

def __init__(self, filepath=None, parent=None, start_rpc=True):
def __init__(self, filepath=None, parent=None, start_rpc=False):
"""Create NXT window.
:param parent: parent to attach this UI to.
Expand Down
53 changes: 51 additions & 2 deletions nxt_editor/user_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Internal
from nxt.constants import USER_DIR
from nxt_editor.constants import PREF_DIR
from nxt_editor.constants import PREF_DIR, PREF_DIR_INT, PREF_DIR_NAME
import nxt_editor

logger = logging.getLogger(nxt_editor.LOGGER_NAME)
Expand All @@ -32,7 +32,7 @@
SKIPPOINT_FILE = os.path.join(PREF_DIR, 'skippoints')
HOTKEYS_PREF = os.path.join(PREF_DIR, 'hotkeys.json')
MAX_RECENT_FILES = 10

JSON_PREFS = [USER_PREFS_PATH, BREAKPOINT_FILE, SKIPPOINT_FILE, HOTKEYS_PREF]
broken_files = {}


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


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. Returns a list of prefs that can upgrade and the
versio number they're coming from.
:returns: (list, int)
"""
upgradable_prefs = []
upgrade_prefs_from_version = -1
for pref_file in JSON_PREFS:
if os.path.isfile(pref_file):
break
else: # Didn't find any json prefs in current version prefs
dir_num = PREF_DIR_INT - 1
while dir_num > -1:
old_pref_dir = os.path.join(USER_DIR, PREF_DIR_NAME, str(dir_num))
for pref_file in JSON_PREFS:
file_name = os.path.basename(pref_file)
old_pref_file = os.path.join(old_pref_dir, file_name)
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
break
dir_num -= 1
return upgradable_prefs, upgrade_prefs_from_version


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 prefs_to_upgrade:
try:
shutil.copy(pref_file, PREF_DIR)
except Exception as e:
logger.error(e)
logger.error(f'Failed to copy old pref file: {pref_file}')


ensure_pref_dir_exists()
# 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 77f7598

Please sign in to comment.