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

PICARD-2879: macOS: Extend all paths in filebrowser with /Volumes/ #2440

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 1 addition & 4 deletions picard/const/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2007, 2014, 2016 Lukáš Lalinský
# Copyright (C) 2014, 2019-2022 Philipp Wolfer
# Copyright (C) 2014, 2019-2022, 2024 Philipp Wolfer
# Copyright (C) 2014-2016, 2018-2021, 2024 Laurent Monin
# Copyright (C) 2015 Ohm Patel
# Copyright (C) 2016 Rahul Raturi
Expand Down Expand Up @@ -77,9 +77,6 @@


DEFAULT_CURRENT_BROWSER_PATH = QStandardPaths.writableLocation(QStandardPaths.StandardLocation.HomeLocation)
if IS_MACOS:
from picard.util.macos import extend_root_volume_path
DEFAULT_CURRENT_BROWSER_PATH = extend_root_volume_path(DEFAULT_CURRENT_BROWSER_PATH)

# Default query limit
DEFAULT_QUERY_LIMIT = 50
Expand Down
10 changes: 9 additions & 1 deletion picard/ui/filebrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# Copyright (C) 2006-2008 Lukáš Lalinský
# Copyright (C) 2008 Hendrik van Antwerpen
# Copyright (C) 2008-2009, 2019-2022 Philipp Wolfer
# Copyright (C) 2008-2009, 2019-2022, 2024 Philipp Wolfer
# Copyright (C) 2011 Andrew Barnert
# Copyright (C) 2012-2013 Michael Wiencek
# Copyright (C) 2013 Wieland Hoffmann
Expand Down Expand Up @@ -42,6 +42,10 @@
from picard.formats import supported_formats
from picard.i18n import gettext as _
from picard.util import find_existing_path
from picard.util.macos import (
extend_root_volume_path,
strip_root_volume_path,
)


class FileBrowser(QtWidgets.QTreeView):
Expand Down Expand Up @@ -176,6 +180,8 @@ def _restore_state(self):
path = config.persist['current_browser_path']
scrolltype = QtWidgets.QAbstractItemView.ScrollHint.PositionAtCenter
if path:
if IS_MACOS:
path = extend_root_volume_path(path)
index = self.model().index(find_existing_path(path))
self.setCurrentIndex(index)
self.expand(index)
Expand All @@ -185,6 +191,8 @@ def _get_destination_from_path(self, path):
destination = os.path.normpath(path)
if not os.path.isdir(destination):
destination = os.path.dirname(destination)
if IS_MACOS:
destination = strip_root_volume_path(destination)
return destination

def load_file_for_item(self, index):
Expand Down
11 changes: 11 additions & 0 deletions picard/util/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ def extend_root_volume_path(path):
path = path[1:]
path = os.path.join(root_volume, path)
return path


def strip_root_volume_path(path):
if not path.startswith("/Volumes/"):
return path
root_volume = _find_root_volume()
if root_volume:
norm_path = os.path.normpath(path)
if norm_path.startswith(root_volume):
path = os.path.join('/', norm_path[len(root_volume):])
return path
26 changes: 25 additions & 1 deletion test/test_util_macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
from test.picardtestcase import PicardTestCase

from picard.const.sys import IS_MACOS
from picard.util.macos import extend_root_volume_path
from picard.util.macos import (
extend_root_volume_path,
strip_root_volume_path,
)


def fake_os_scandir(path):
Expand Down Expand Up @@ -58,3 +61,24 @@ def test_path_donot_starts_with_volumes_failed(self):
result = extend_root_volume_path(path)
self.assertEqual(result, path)
self.assertTrue(mock.called)


@unittest.skipUnless(IS_MACOS, "macOS test")
class UtilMacosStripRootVolumeTest(PicardTestCase):

def test_path_starts_not_with_volumes(self):
path = '/Users/sandra'
result = strip_root_volume_path(path)
self.assertEqual(result, path)

@patch('picard.util.macos._find_root_volume', lambda: '/Volumes/root_volume')
def test_path_starts_not_with_root_volume(self):
path = '/Volumes/other_volume/foo/bar'
result = strip_root_volume_path(path)
self.assertEqual(result, path)

@patch('picard.util.macos._find_root_volume', lambda: '/Volumes/root_volume')
def test_path_starts_with_root_volume(self):
path = '/Volumes/root_volume/foo/bar'
result = strip_root_volume_path(path)
self.assertEqual(result, '/foo/bar')
Loading