Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu option to open preferences directory and update to util functions to pathlib #1843

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion sleap/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__)
Expand Down Expand Up @@ -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 preference directory...",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Open preference directory...",
"Open Preferences Directory...",

self.openPrefs,
)

fileMenu.addSeparator()
add_menu_item(fileMenu, "close", "Quit", self.close)

Expand Down Expand Up @@ -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()
Expand Down
30 changes: 10 additions & 20 deletions sleap/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<version>/ directory to store user version of the config file.
desired_path.parent.mkdir(parents=True, exist_ok=True)

# Make sure there's a ~/.sleap/<version>/ 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}."
)
Expand Down