diff --git a/sleap/gui/app.py b/sleap/gui/app.py index 736d7207f..e872ce9a6 100644 --- a/sleap/gui/app.py +++ b/sleap/gui/app.py @@ -53,6 +53,8 @@ from logging import getLogger from pathlib import Path from typing import Callable, List, Optional, Tuple +import sys +import subprocess from qtpy import QtCore, QtGui from qtpy.QtCore import QEvent, Qt @@ -84,7 +86,7 @@ from sleap.io.video import available_video_exts from sleap.prefs import prefs from sleap.skeleton import Skeleton -from sleap.util import parse_uri_path +from sleap.util import parse_uri_path, get_config_file logger = getLogger(__name__) @@ -515,6 +517,13 @@ def add_submenu_choices(menu, title, options, key): fileMenu, "reset prefs", "Reset preferences to defaults...", self.resetPrefs ) + add_menu_item( + fileMenu, + "open preference directory", + "Open Preferences Directory...", + self.openPrefs, + ) + fileMenu.addSeparator() add_menu_item(fileMenu, "close", "Quit", self.close) @@ -1330,6 +1339,20 @@ def resetPrefs(self): ) msg.exec_() + def openPrefs(self): + """Open preference file directory""" + pref_path = get_config_file("preferences.yaml") + # Make sure the pref_path is a directory rather than a file + if pref_path.is_file(): + pref_path = pref_path.parent + # Open the file explorer at the folder containing the preferences.yaml file + if sys.platform == "win32": + subprocess.Popen(["explorer", str(pref_path)]) + elif sys.platform == "darwin": + subprocess.Popen(["open", str(pref_path)]) + else: + subprocess.Popen(["xdg-open", str(pref_path)]) + def _update_track_menu(self): """Updates track menu options.""" self.track_menu.clear() diff --git a/sleap/util.py b/sleap/util.py index 5edbf164b..eef762ff4 100644 --- a/sleap/util.py +++ b/sleap/util.py @@ -270,30 +270,20 @@ def get_config_file( The full path to the specified config file. """ - desired_path = None # Handle case where get_defaults, but cannot find package_path + desired_path = Path.home() / f".sleap/{sleap_version.__version__}/{shortname}" - if not get_defaults: - desired_path = os.path.expanduser( - f"~/.sleap/{sleap_version.__version__}/{shortname}" - ) + # Make sure there's a ~/.sleap// directory to store user version of the config file. + desired_path.parent.mkdir(parents=True, exist_ok=True) - # Make sure there's a ~/.sleap// directory to store user version of the - # config file. - try: - os.makedirs(os.path.expanduser(f"~/.sleap/{sleap_version.__version__}")) - except FileExistsError: - pass - - # If we don't care whether the file exists, just return the path - if ignore_file_not_found: - return desired_path - - # If we do care whether the file exists, check the package version of the - # config file if we can't find the user version. + # If we don't care whether the file exists, just return the path + if ignore_file_not_found: + return desired_path - if get_defaults or not os.path.exists(desired_path): + # If we do care whether the file exists, check the package version of the config file if we can't find the user version. + if get_defaults or not desired_path.exists(): package_path = get_package_file(f"config/{shortname}") - if not os.path.exists(package_path): + package_path = Path(package_path) + if not package_path.exists(): raise FileNotFoundError( f"Cannot locate {shortname} config file at {desired_path} or {package_path}." )