From 335c4d9fd8ca4f4bf1642da72b3025e37727d05e Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Mon, 8 Apr 2013 20:42:33 +0200 Subject: [PATCH 1/6] main/path: Inline constants used once --- mopidy/__main__.py | 7 ++++--- mopidy/utils/path.py | 5 ----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/mopidy/__main__.py b/mopidy/__main__.py index 94e245d0..ea7bea32 100644 --- a/mopidy/__main__.py +++ b/mopidy/__main__.py @@ -309,9 +309,10 @@ def validate_config(raw_config, schemas, extensions=None): def create_file_structures(): - path.get_or_create_folder(path.DATA_PATH) - path.get_or_create_folder(path.CONFIG_PATH) - path.get_or_create_file(path.CONFIG_FILE) + path.get_or_create_folder(path.expand_path('$XDG_DATA_DIR/mopidy')) + path.get_or_create_folder(path.expand_path('$XDG_CONFIG_DIR/mopidy')) + path.get_or_create_file( + path.expand_path('$XDG_CONFIG_DIR/mopidy/mopidy.conf')) def setup_audio(config): diff --git a/mopidy/utils/path.py b/mopidy/utils/path.py index 88b8be3e..bcd610da 100644 --- a/mopidy/utils/path.py +++ b/mopidy/utils/path.py @@ -25,11 +25,6 @@ XDG_DIRS = { 'XDG_DATA_DIR': XDG_DATA_DIR, 'XDG_MUSIC_DIR': XDG_MUSIC_DIR, } -DATA_PATH = os.path.join(unicode(XDG_DATA_DIR), 'mopidy') -CONFIG_PATH = os.path.join(unicode(XDG_CONFIG_DIR), 'mopidy') -CONFIG_FILE = os.path.join(unicode(CONFIG_PATH), 'mopidy.conf') -SETTINGS_PATH = os.path.join(unicode(XDG_CONFIG_DIR), 'mopidy') -SETTINGS_FILE = os.path.join(unicode(SETTINGS_PATH), 'settings.py') def get_or_create_folder(folder): From 6e5cdb85b0369480b9638dab65ae3f4d77e66dbc Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Mon, 8 Apr 2013 20:44:27 +0200 Subject: [PATCH 2/6] spotify: Rename 'cache_path' to 'cache_dir' --- mopidy/backends/spotify/__init__.py | 4 ++-- mopidy/backends/spotify/session_manager.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mopidy/backends/spotify/__init__.py b/mopidy/backends/spotify/__init__.py index 8aa0f4c0..704471b1 100644 --- a/mopidy/backends/spotify/__init__.py +++ b/mopidy/backends/spotify/__init__.py @@ -25,7 +25,7 @@ bitrate = 160 timeout = 10 # Path to the Spotify data cache. Cannot be shared with other Spotify apps -cache_path = $XDG_CACHE_DIR/mopidy/spotify +cache_dir = $XDG_CACHE_DIR/mopidy/spotify """ __doc__ = """A backend for playing music from Spotify @@ -75,7 +75,7 @@ class Extension(ext.Extension): schema['password'] = config.String(secret=True) schema['bitrate'] = config.Integer(choices=(96, 160, 320)) schema['timeout'] = config.Integer(minimum=0) - schema['cache_path'] = config.Path() + schema['cache_dir'] = config.Path() return schema def validate_environment(self): diff --git a/mopidy/backends/spotify/session_manager.py b/mopidy/backends/spotify/session_manager.py index 22ad4632..a830138f 100644 --- a/mopidy/backends/spotify/session_manager.py +++ b/mopidy/backends/spotify/session_manager.py @@ -30,8 +30,8 @@ class SpotifySessionManager(process.BaseThread, PyspotifySessionManager): def __init__(self, config, audio, backend_ref): - self.cache_location = config['spotify']['cache_path'] - self.settings_location = config['spotify']['cache_path'] + self.cache_location = config['spotify']['cache_dir'] + self.settings_location = config['spotify']['cache_dir'] PyspotifySessionManager.__init__( self, config['spotify']['username'], config['spotify']['password'], From 3339b79c1e269d2706427bbda8153deab56d4a28 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Mon, 8 Apr 2013 20:55:27 +0200 Subject: [PATCH 3/6] local: Rename 'music_path' to 'music_dir', 'playlist_path' to 'playlists_dir' --- mopidy/backends/local/__init__.py | 14 +++++++------- mopidy/backends/local/library.py | 7 +++---- mopidy/backends/local/playlists.py | 22 +++++++++++----------- mopidy/scanner.py | 6 +++--- tests/backends/local/events_test.py | 4 ++-- tests/backends/local/library_test.py | 4 ++-- tests/backends/local/playback_test.py | 4 ++-- tests/backends/local/playlists_test.py | 24 ++++++++++++------------ tests/backends/local/tracklist_test.py | 4 ++-- 9 files changed, 44 insertions(+), 45 deletions(-) diff --git a/mopidy/backends/local/__init__.py b/mopidy/backends/local/__init__.py index 54a1c7a4..abd3eab6 100644 --- a/mopidy/backends/local/__init__.py +++ b/mopidy/backends/local/__init__.py @@ -11,13 +11,13 @@ default_config = """ # If the local extension should be enabled or not enabled = true -# Path to folder with local music -music_path = $XDG_MUSIC_DIR +# Path to directory with local media files +media_dir = $XDG_MUSIC_DIR -# Path to playlist folder with m3u files for local music -playlist_path = $XDG_DATA_DIR/mopidy/playlists +# Path to playlists directory with m3u files for local media +playlists_dir = $XDG_DATA_DIR/mopidy/playlists -# Path to tag cache for local music +# Path to tag cache for local media tag_cache_file = $XDG_DATA_DIR/mopidy/tag_cache """ @@ -55,8 +55,8 @@ class Extension(ext.Extension): def get_config_schema(self): schema = config.ExtensionConfigSchema() - schema['music_path'] = config.Path() - schema['playlist_path'] = config.Path() + schema['media_dir'] = config.Path() + schema['playlists_dir'] = config.Path() schema['tag_cache_file'] = config.Path() return schema diff --git a/mopidy/backends/local/library.py b/mopidy/backends/local/library.py index 2b1c93f7..a76ce594 100644 --- a/mopidy/backends/local/library.py +++ b/mopidy/backends/local/library.py @@ -14,17 +14,16 @@ class LocalLibraryProvider(base.BaseLibraryProvider): def __init__(self, *args, **kwargs): super(LocalLibraryProvider, self).__init__(*args, **kwargs) self._uri_mapping = {} - self._music_path = self.backend.config['local']['music_path'] - self._playlist_path = self.backend.config['local']['playlist_path'] + self._media_dir = self.backend.config['local']['media_dir'] self._tag_cache_file = self.backend.config['local']['tag_cache_file'] self.refresh() def refresh(self, uri=None): - tracks = parse_mpd_tag_cache(self._tag_cache_file, self._music_path) + tracks = parse_mpd_tag_cache(self._tag_cache_file, self._media_dir) logger.info( 'Loading tracks from %s using %s', - self._music_path, self._tag_cache_file) + self._media_dir, self._tag_cache_file) for track in tracks: self._uri_mapping[track.uri] = track diff --git a/mopidy/backends/local/playlists.py b/mopidy/backends/local/playlists.py index 063d044d..3b9e1d73 100644 --- a/mopidy/backends/local/playlists.py +++ b/mopidy/backends/local/playlists.py @@ -18,8 +18,8 @@ logger = logging.getLogger('mopidy.backends.local') class LocalPlaylistsProvider(base.BasePlaylistsProvider): def __init__(self, *args, **kwargs): super(LocalPlaylistsProvider, self).__init__(*args, **kwargs) - self._music_path = self.backend.config['local']['music_path'] - self._playlist_path = self.backend.config['local']['playlist_path'] + self._media_dir = self.backend.config['local']['media_dir'] + self._playlists_dir = self.backend.config['local']['playlists_dir'] self.refresh() def create(self, name): @@ -42,16 +42,16 @@ class LocalPlaylistsProvider(base.BasePlaylistsProvider): return playlist def refresh(self): - logger.info('Loading playlists from %s', self._playlist_path) + logger.info('Loading playlists from %s', self._playlists_dir) playlists = [] - for m3u in glob.glob(os.path.join(self._playlist_path, '*.m3u')): + for m3u in glob.glob(os.path.join(self._playlists_dir, '*.m3u')): uri = path.path_to_uri(m3u) name = os.path.splitext(os.path.basename(m3u))[0] tracks = [] - for track_uri in parse_m3u(m3u, self._music_path): + for track_uri in parse_m3u(m3u, self._media_dir): try: # TODO We must use core.library.lookup() to support tracks # from other backends @@ -86,13 +86,13 @@ class LocalPlaylistsProvider(base.BasePlaylistsProvider): def _get_m3u_path(self, name): name = formatting.slugify(name) - file_path = os.path.join(self._playlist_path, name + '.m3u') - path.check_file_path_is_inside_base_dir(file_path, self._playlist_path) + file_path = os.path.join(self._playlists_dir, name + '.m3u') + path.check_file_path_is_inside_base_dir(file_path, self._playlists_dir) return file_path def _save_m3u(self, playlist): file_path = path.uri_to_path(playlist.uri) - path.check_file_path_is_inside_base_dir(file_path, self._playlist_path) + path.check_file_path_is_inside_base_dir(file_path, self._playlists_dir) with open(file_path, 'w') as file_handle: for track in playlist.tracks: if track.uri.startswith('file://'): @@ -103,18 +103,18 @@ class LocalPlaylistsProvider(base.BasePlaylistsProvider): def _delete_m3u(self, uri): file_path = path.uri_to_path(uri) - path.check_file_path_is_inside_base_dir(file_path, self._playlist_path) + path.check_file_path_is_inside_base_dir(file_path, self._playlists_dir) if os.path.exists(file_path): os.remove(file_path) def _rename_m3u(self, playlist): src_file_path = path.uri_to_path(playlist.uri) path.check_file_path_is_inside_base_dir( - src_file_path, self._playlist_path) + src_file_path, self._playlists_dir) dst_file_path = self._get_m3u_path(playlist.name) path.check_file_path_is_inside_base_dir( - dst_file_path, self._playlist_path) + dst_file_path, self._playlists_dir) shutil.move(src_file_path, dst_file_path) diff --git a/mopidy/scanner.py b/mopidy/scanner.py index 364828f4..0f87a4d1 100644 --- a/mopidy/scanner.py +++ b/mopidy/scanner.py @@ -57,9 +57,9 @@ def main(): logging.warning('Failed %s: %s', uri, error) logging.debug('Debug info for %s: %s', uri, debug) - logging.info('Scanning %s', config['local']['music_path']) + logging.info('Scanning %s', config['local']['media_dir']) - scanner = Scanner(config['local']['music_path'], store, debug) + scanner = Scanner(config['local']['media_dir'], store, debug) try: scanner.start() except KeyboardInterrupt: @@ -68,7 +68,7 @@ def main(): logging.info('Done scanning; writing tag cache...') for row in mpd_translator.tracks_to_tag_cache_format( - tracks, config['mpd']['music_path']): + tracks, config['mpd']['media_dir']): if len(row) == 1: print ('%s' % row).encode('utf-8') else: diff --git a/tests/backends/local/events_test.py b/tests/backends/local/events_test.py index 83d77a2f..e09bf4b9 100644 --- a/tests/backends/local/events_test.py +++ b/tests/backends/local/events_test.py @@ -10,8 +10,8 @@ class LocalBackendEventsTest(events.BackendEventsTest, unittest.TestCase): backend_class = actor.LocalBackend config = { 'local': { - 'music_path': path_to_data_dir(''), - 'playlist_path': '', + 'media_dir': path_to_data_dir(''), + 'playlists_dir': '', 'tag_cache_file': path_to_data_dir('empty_tag_cache'), } } diff --git a/tests/backends/local/library_test.py b/tests/backends/local/library_test.py index e582c788..74635b3e 100644 --- a/tests/backends/local/library_test.py +++ b/tests/backends/local/library_test.py @@ -10,8 +10,8 @@ class LocalLibraryControllerTest(LibraryControllerTest, unittest.TestCase): backend_class = actor.LocalBackend config = { 'local': { - 'music_path': path_to_data_dir(''), - 'playlist_path': '', + 'media_dir': path_to_data_dir(''), + 'playlists_dir': '', 'tag_cache_file': path_to_data_dir('library_tag_cache'), } } diff --git a/tests/backends/local/playback_test.py b/tests/backends/local/playback_test.py index 4c304590..16592539 100644 --- a/tests/backends/local/playback_test.py +++ b/tests/backends/local/playback_test.py @@ -14,8 +14,8 @@ class LocalPlaybackControllerTest(PlaybackControllerTest, unittest.TestCase): backend_class = actor.LocalBackend config = { 'local': { - 'music_path': path_to_data_dir(''), - 'playlist_path': '', + 'media_dir': path_to_data_dir(''), + 'playlists_dir': '', 'tag_cache_file': path_to_data_dir('empty_tag_cache'), } } diff --git a/tests/backends/local/playlists_test.py b/tests/backends/local/playlists_test.py index 8528adf4..b35b504f 100644 --- a/tests/backends/local/playlists_test.py +++ b/tests/backends/local/playlists_test.py @@ -20,32 +20,32 @@ class LocalPlaylistsControllerTest( backend_class = actor.LocalBackend config = { 'local': { - 'music_path': path_to_data_dir(''), + 'media_dir': path_to_data_dir(''), 'tag_cache_file': path_to_data_dir('library_tag_cache'), } } def setUp(self): - self.config['local']['playlist_path'] = tempfile.mkdtemp() - self.playlist_path = self.config['local']['playlist_path'] + self.config['local']['playlists_dir'] = tempfile.mkdtemp() + self.playlists_dir = self.config['local']['playlists_dir'] super(LocalPlaylistsControllerTest, self).setUp() def tearDown(self): super(LocalPlaylistsControllerTest, self).tearDown() - if os.path.exists(self.playlist_path): - shutil.rmtree(self.playlist_path) + if os.path.exists(self.playlists_dir): + shutil.rmtree(self.playlists_dir) def test_created_playlist_is_persisted(self): - path = os.path.join(self.playlist_path, 'test.m3u') + path = os.path.join(self.playlists_dir, 'test.m3u') self.assertFalse(os.path.exists(path)) self.core.playlists.create('test') self.assertTrue(os.path.exists(path)) def test_create_slugifies_playlist_name(self): - path = os.path.join(self.playlist_path, 'test-foo-bar.m3u') + path = os.path.join(self.playlists_dir, 'test-foo-bar.m3u') self.assertFalse(os.path.exists(path)) playlist = self.core.playlists.create('test FOO baR') @@ -53,7 +53,7 @@ class LocalPlaylistsControllerTest( self.assertTrue(os.path.exists(path)) def test_create_slugifies_names_which_tries_to_change_directory(self): - path = os.path.join(self.playlist_path, 'test-foo-bar.m3u') + path = os.path.join(self.playlists_dir, 'test-foo-bar.m3u') self.assertFalse(os.path.exists(path)) playlist = self.core.playlists.create('../../test FOO baR') @@ -61,8 +61,8 @@ class LocalPlaylistsControllerTest( self.assertTrue(os.path.exists(path)) def test_saved_playlist_is_persisted(self): - path1 = os.path.join(self.playlist_path, 'test1.m3u') - path2 = os.path.join(self.playlist_path, 'test2-foo-bar.m3u') + path1 = os.path.join(self.playlists_dir, 'test1.m3u') + path2 = os.path.join(self.playlists_dir, 'test2-foo-bar.m3u') playlist = self.core.playlists.create('test1') @@ -77,7 +77,7 @@ class LocalPlaylistsControllerTest( self.assertTrue(os.path.exists(path2)) def test_deleted_playlist_is_removed(self): - path = os.path.join(self.playlist_path, 'test.m3u') + path = os.path.join(self.playlists_dir, 'test.m3u') self.assertFalse(os.path.exists(path)) playlist = self.core.playlists.create('test') @@ -100,7 +100,7 @@ class LocalPlaylistsControllerTest( self.assertEqual(track_path, contents.strip()) def test_playlists_are_loaded_at_startup(self): - playlist_path = os.path.join(self.playlist_path, 'test.m3u') + playlist_path = os.path.join(self.playlists_dir, 'test.m3u') track = Track(uri=path_to_uri(path_to_data_dir('uri2'))) playlist = self.core.playlists.create('test') diff --git a/tests/backends/local/tracklist_test.py b/tests/backends/local/tracklist_test.py index 3fc8a0be..2d8e87b6 100644 --- a/tests/backends/local/tracklist_test.py +++ b/tests/backends/local/tracklist_test.py @@ -12,8 +12,8 @@ class LocalTracklistControllerTest(TracklistControllerTest, unittest.TestCase): backend_class = actor.LocalBackend config = { 'local': { - 'music_path': path_to_data_dir(''), - 'playlist_path': '', + 'media_dir': path_to_data_dir(''), + 'playlists_dir': '', 'tag_cache_file': path_to_data_dir('empty_tag_cache'), } } From f9ed1ba4d3045aec5292fda2a0703ef4a34b07fc Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Mon, 8 Apr 2013 20:58:34 +0200 Subject: [PATCH 4/6] mpd: Rename 'music_path' to 'music_dir' --- mopidy/frontends/mpd/translator.py | 20 ++++++------- tests/frontends/mpd/translator_test.py | 40 +++++++++++++------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/mopidy/frontends/mpd/translator.py b/mopidy/frontends/mpd/translator.py index d820b0e0..21be37cc 100644 --- a/mopidy/frontends/mpd/translator.py +++ b/mopidy/frontends/mpd/translator.py @@ -215,14 +215,14 @@ def query_from_mpd_search_format(mpd_query): return query -def tracks_to_tag_cache_format(tracks, music_path): +def tracks_to_tag_cache_format(tracks, media_dir): """ Format list of tracks for output to MPD tag cache :param tracks: the tracks :type tracks: list of :class:`mopidy.models.Track` - :param music_path: the path to the music dir - :type music_path: string + :param media_dir: the path to the music dir + :type media_dir: string :rtype: list of lists of two-tuples """ result = [ @@ -232,13 +232,13 @@ def tracks_to_tag_cache_format(tracks, music_path): ('info_end',) ] tracks.sort(key=lambda t: t.uri) - folders, files = tracks_to_directory_tree(tracks, music_path) - _add_to_tag_cache(result, folders, files, music_path) + folders, files = tracks_to_directory_tree(tracks, media_dir) + _add_to_tag_cache(result, folders, files, media_dir) return result -def _add_to_tag_cache(result, folders, files, music_path): - base_path = music_path.encode('utf-8') +def _add_to_tag_cache(result, folders, files, media_dir): + base_path = media_dir.encode('utf-8') for path, (entry_folders, entry_files) in folders.items(): try: @@ -249,7 +249,7 @@ def _add_to_tag_cache(result, folders, files, music_path): result.append(('directory', text_path)) result.append(('mtime', get_mtime(os.path.join(base_path, path)))) result.append(('begin', name)) - _add_to_tag_cache(result, entry_folders, entry_files, music_path) + _add_to_tag_cache(result, entry_folders, entry_files, media_dir) result.append(('end', name)) result.append(('songList begin',)) @@ -275,7 +275,7 @@ def _add_to_tag_cache(result, folders, files, music_path): result.append(('songList end',)) -def tracks_to_directory_tree(tracks, music_path): +def tracks_to_directory_tree(tracks, media_dir): directories = ({}, []) for track in tracks: @@ -284,7 +284,7 @@ def tracks_to_directory_tree(tracks, music_path): absolute_track_dir_path = os.path.dirname(uri_to_path(track.uri)) relative_track_dir_path = re.sub( - '^' + re.escape(music_path), b'', absolute_track_dir_path) + '^' + re.escape(media_dir), b'', absolute_track_dir_path) for part in split_path(relative_track_dir_path): path = os.path.join(path, part) diff --git a/tests/frontends/mpd/translator_test.py b/tests/frontends/mpd/translator_test.py index 828acf1a..2f646faf 100644 --- a/tests/frontends/mpd/translator_test.py +++ b/tests/frontends/mpd/translator_test.py @@ -23,7 +23,7 @@ class TrackMpdFormatTest(unittest.TestCase): ) def setUp(self): - self.music_path = '/dir/subdir' + self.media_dir = '/dir/subdir' mtime.set_fake_time(1234567) def tearDown(self): @@ -135,14 +135,14 @@ class QueryFromMpdListFormatTest(unittest.TestCase): class TracksToTagCacheFormatTest(unittest.TestCase): def setUp(self): - self.music_path = '/dir/subdir' + self.media_dir = '/dir/subdir' mtime.set_fake_time(1234567) def tearDown(self): mtime.undo_fake() def translate(self, track): - base_path = self.music_path.encode('utf-8') + base_path = self.media_dir.encode('utf-8') result = dict(translator.track_to_mpd_format(track)) result['file'] = uri_to_path(result['file'])[len(base_path) + 1:] result['key'] = os.path.basename(result['file']) @@ -174,11 +174,11 @@ class TracksToTagCacheFormatTest(unittest.TestCase): self.fail("Couldn't find end %s in result" % directory) def test_empty_tag_cache_has_header(self): - result = translator.tracks_to_tag_cache_format([], self.music_path) + result = translator.tracks_to_tag_cache_format([], self.media_dir) result = self.consume_headers(result) def test_empty_tag_cache_has_song_list(self): - result = translator.tracks_to_tag_cache_format([], self.music_path) + result = translator.tracks_to_tag_cache_format([], self.media_dir) result = self.consume_headers(result) song_list, result = self.consume_song_list(result) @@ -187,12 +187,12 @@ class TracksToTagCacheFormatTest(unittest.TestCase): def test_tag_cache_has_header(self): track = Track(uri='file:///dir/subdir/song.mp3') - result = translator.tracks_to_tag_cache_format([track], self.music_path) + result = translator.tracks_to_tag_cache_format([track], self.media_dir) result = self.consume_headers(result) def test_tag_cache_has_song_list(self): track = Track(uri='file:///dir/subdir/song.mp3') - result = translator.tracks_to_tag_cache_format([track], self.music_path) + result = translator.tracks_to_tag_cache_format([track], self.media_dir) result = self.consume_headers(result) song_list, result = self.consume_song_list(result) @@ -202,7 +202,7 @@ class TracksToTagCacheFormatTest(unittest.TestCase): def test_tag_cache_has_formated_track(self): track = Track(uri='file:///dir/subdir/song.mp3') formated = self.translate(track) - result = translator.tracks_to_tag_cache_format([track], self.music_path) + result = translator.tracks_to_tag_cache_format([track], self.media_dir) result = self.consume_headers(result) song_list, result = self.consume_song_list(result) @@ -213,7 +213,7 @@ class TracksToTagCacheFormatTest(unittest.TestCase): def test_tag_cache_has_formated_track_with_key_and_mtime(self): track = Track(uri='file:///dir/subdir/song.mp3') formated = self.translate(track) - result = translator.tracks_to_tag_cache_format([track], self.music_path) + result = translator.tracks_to_tag_cache_format([track], self.media_dir) result = self.consume_headers(result) song_list, result = self.consume_song_list(result) @@ -224,7 +224,7 @@ class TracksToTagCacheFormatTest(unittest.TestCase): def test_tag_cache_suports_directories(self): track = Track(uri='file:///dir/subdir/folder/song.mp3') formated = self.translate(track) - result = translator.tracks_to_tag_cache_format([track], self.music_path) + result = translator.tracks_to_tag_cache_format([track], self.media_dir) result = self.consume_headers(result) folder, result = self.consume_directory(result) @@ -238,7 +238,7 @@ class TracksToTagCacheFormatTest(unittest.TestCase): def test_tag_cache_diretory_header_is_right(self): track = Track(uri='file:///dir/subdir/folder/sub/song.mp3') - result = translator.tracks_to_tag_cache_format([track], self.music_path) + result = translator.tracks_to_tag_cache_format([track], self.media_dir) result = self.consume_headers(result) folder, result = self.consume_directory(result) @@ -250,7 +250,7 @@ class TracksToTagCacheFormatTest(unittest.TestCase): def test_tag_cache_suports_sub_directories(self): track = Track(uri='file:///dir/subdir/folder/sub/song.mp3') formated = self.translate(track) - result = translator.tracks_to_tag_cache_format([track], self.music_path) + result = translator.tracks_to_tag_cache_format([track], self.media_dir) result = self.consume_headers(result) @@ -278,7 +278,7 @@ class TracksToTagCacheFormatTest(unittest.TestCase): formated.extend(self.translate(tracks[0])) formated.extend(self.translate(tracks[1])) - result = translator.tracks_to_tag_cache_format(tracks, self.music_path) + result = translator.tracks_to_tag_cache_format(tracks, self.media_dir) result = self.consume_headers(result) song_list, result = self.consume_song_list(result) @@ -296,7 +296,7 @@ class TracksToTagCacheFormatTest(unittest.TestCase): formated.append(self.translate(tracks[0])) formated.append(self.translate(tracks[1])) - result = translator.tracks_to_tag_cache_format(tracks, self.music_path) + result = translator.tracks_to_tag_cache_format(tracks, self.media_dir) result = self.consume_headers(result) folder, result = self.consume_directory(result) @@ -312,10 +312,10 @@ class TracksToTagCacheFormatTest(unittest.TestCase): class TracksToDirectoryTreeTest(unittest.TestCase): def setUp(self): - self.music_path = '/root' + self.media_dir = '/root' def test_no_tracks_gives_emtpy_tree(self): - tree = translator.tracks_to_directory_tree([], self.music_path) + tree = translator.tracks_to_directory_tree([], self.media_dir) self.assertEqual(tree, ({}, [])) def test_top_level_files(self): @@ -324,18 +324,18 @@ class TracksToDirectoryTreeTest(unittest.TestCase): Track(uri='file:///root/file2.mp3'), Track(uri='file:///root/file3.mp3'), ] - tree = translator.tracks_to_directory_tree(tracks, self.music_path) + tree = translator.tracks_to_directory_tree(tracks, self.media_dir) self.assertEqual(tree, ({}, tracks)) def test_single_file_in_subdir(self): tracks = [Track(uri='file:///root/dir/file1.mp3')] - tree = translator.tracks_to_directory_tree(tracks, self.music_path) + tree = translator.tracks_to_directory_tree(tracks, self.media_dir) expected = ({'dir': ({}, tracks)}, []) self.assertEqual(tree, expected) def test_single_file_in_sub_subdir(self): tracks = [Track(uri='file:///root/dir1/dir2/file1.mp3')] - tree = translator.tracks_to_directory_tree(tracks, self.music_path) + tree = translator.tracks_to_directory_tree(tracks, self.media_dir) expected = ({'dir1': ({'dir1/dir2': ({}, tracks)}, [])}, []) self.assertEqual(tree, expected) @@ -347,7 +347,7 @@ class TracksToDirectoryTreeTest(unittest.TestCase): Track(uri='file:///root/dir2/file4.mp3'), Track(uri='file:///root/dir2/sub/file5.mp3'), ] - tree = translator.tracks_to_directory_tree(tracks, self.music_path) + tree = translator.tracks_to_directory_tree(tracks, self.media_dir) expected = ( { 'dir1': ({}, [tracks[1], tracks[2]]), From 41d7ae9a2c093a7e0f70be97868f73f4662c1324 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Mon, 8 Apr 2013 21:13:59 +0200 Subject: [PATCH 5/6] Replace 'folder' with 'dir' --- mopidy/__main__.py | 4 +- mopidy/backends/local/translator.py | 4 +- mopidy/frontends/mpd/translator.py | 10 ++-- mopidy/scanner.py | 4 +- mopidy/utils/path.py | 18 +++---- tests/backends/local/playlists_test.py | 2 +- tests/backends/local/translator_test.py | 2 +- tests/frontends/mpd/translator_test.py | 24 ++++----- tests/scanner_test.py | 2 +- tests/utils/path_test.py | 66 ++++++++++++------------- 10 files changed, 68 insertions(+), 68 deletions(-) diff --git a/mopidy/__main__.py b/mopidy/__main__.py index ea7bea32..4e81856a 100644 --- a/mopidy/__main__.py +++ b/mopidy/__main__.py @@ -309,8 +309,8 @@ def validate_config(raw_config, schemas, extensions=None): def create_file_structures(): - path.get_or_create_folder(path.expand_path('$XDG_DATA_DIR/mopidy')) - path.get_or_create_folder(path.expand_path('$XDG_CONFIG_DIR/mopidy')) + path.get_or_create_dir(path.expand_path('$XDG_DATA_DIR/mopidy')) + path.get_or_create_dir(path.expand_path('$XDG_CONFIG_DIR/mopidy')) path.get_or_create_file( path.expand_path('$XDG_CONFIG_DIR/mopidy/mopidy.conf')) diff --git a/mopidy/backends/local/translator.py b/mopidy/backends/local/translator.py index a36be927..683ad6b4 100644 --- a/mopidy/backends/local/translator.py +++ b/mopidy/backends/local/translator.py @@ -10,7 +10,7 @@ from mopidy.utils.path import path_to_uri logger = logging.getLogger('mopidy.backends.local') -def parse_m3u(file_path, music_folder): +def parse_m3u(file_path, media_dir): r""" Convert M3U file list of uris @@ -49,7 +49,7 @@ def parse_m3u(file_path, music_folder): if line.startswith('file://'): uris.append(line) else: - path = path_to_uri(music_folder, line) + path = path_to_uri(media_dir, line) uris.append(path) return uris diff --git a/mopidy/frontends/mpd/translator.py b/mopidy/frontends/mpd/translator.py index 21be37cc..7cf5b0c6 100644 --- a/mopidy/frontends/mpd/translator.py +++ b/mopidy/frontends/mpd/translator.py @@ -232,15 +232,15 @@ def tracks_to_tag_cache_format(tracks, media_dir): ('info_end',) ] tracks.sort(key=lambda t: t.uri) - folders, files = tracks_to_directory_tree(tracks, media_dir) - _add_to_tag_cache(result, folders, files, media_dir) + dirs, files = tracks_to_directory_tree(tracks, media_dir) + _add_to_tag_cache(result, dirs, files, media_dir) return result -def _add_to_tag_cache(result, folders, files, media_dir): +def _add_to_tag_cache(result, dirs, files, media_dir): base_path = media_dir.encode('utf-8') - for path, (entry_folders, entry_files) in folders.items(): + for path, (entry_dirs, entry_files) in dirs.items(): try: text_path = path.decode('utf-8') except UnicodeDecodeError: @@ -249,7 +249,7 @@ def _add_to_tag_cache(result, folders, files, media_dir): result.append(('directory', text_path)) result.append(('mtime', get_mtime(os.path.join(base_path, path)))) result.append(('begin', name)) - _add_to_tag_cache(result, entry_folders, entry_files, media_dir) + _add_to_tag_cache(result, entry_dirs, entry_files, media_dir) result.append(('end', name)) result.append(('songList begin',)) diff --git a/mopidy/scanner.py b/mopidy/scanner.py index 0f87a4d1..0c78839b 100644 --- a/mopidy/scanner.py +++ b/mopidy/scanner.py @@ -142,9 +142,9 @@ def translator(data): class Scanner(object): - def __init__(self, folder, data_callback, error_callback=None): + def __init__(self, base_dir, data_callback, error_callback=None): self.data = {} - self.files = path.find_files(folder) + self.files = path.find_files(base_dir) self.data_callback = data_callback self.error_callback = error_callback self.loop = gobject.MainLoop() diff --git a/mopidy/utils/path.py b/mopidy/utils/path.py index bcd610da..eb0cbbb0 100644 --- a/mopidy/utils/path.py +++ b/mopidy/utils/path.py @@ -27,16 +27,16 @@ XDG_DIRS = { } -def get_or_create_folder(folder): - folder = os.path.expanduser(folder) - if os.path.isfile(folder): +def get_or_create_dir(dir_path): + dir_path = os.path.expanduser(dir_path) + if os.path.isfile(dir_path): raise OSError( 'A file with the same name as the desired dir, ' - '"%s", already exists.' % folder) - elif not os.path.isdir(folder): - logger.info('Creating dir %s', folder) - os.makedirs(folder, 0755) - return folder + '"%s", already exists.' % dir_path) + elif not os.path.isdir(dir_path): + logger.info('Creating dir %s', dir_path) + os.makedirs(dir_path, 0755) + return dir_path def get_or_create_file(filename): @@ -121,7 +121,7 @@ def find_files(path): for dirpath, dirnames, filenames in os.walk(path, followlinks=True): for dirname in dirnames: if dirname.startswith(b'.'): - # Skip hidden folders by modifying dirnames inplace + # Skip hidden dirs by modifying dirnames inplace dirnames.remove(dirname) for filename in filenames: diff --git a/tests/backends/local/playlists_test.py b/tests/backends/local/playlists_test.py index b35b504f..acaac941 100644 --- a/tests/backends/local/playlists_test.py +++ b/tests/backends/local/playlists_test.py @@ -123,5 +123,5 @@ class LocalPlaylistsControllerTest( pass @unittest.SkipTest - def test_playlist_folder_is_createad(self): + def test_playlist_dir_is_created(self): pass diff --git a/tests/backends/local/translator_test.py b/tests/backends/local/translator_test.py index 61a86672..67907ff1 100644 --- a/tests/backends/local/translator_test.py +++ b/tests/backends/local/translator_test.py @@ -35,7 +35,7 @@ class M3UToUriTest(unittest.TestCase): uris = parse_m3u(path_to_data_dir('comment.m3u'), data_dir) self.assertEqual([song1_uri], uris) - def test_file_is_relative_to_correct_folder(self): + def test_file_is_relative_to_correct_dir(self): with tempfile.NamedTemporaryFile(delete=False) as tmp: tmp.write('song1.mp3') try: diff --git a/tests/frontends/mpd/translator_test.py b/tests/frontends/mpd/translator_test.py index 2f646faf..e4755d01 100644 --- a/tests/frontends/mpd/translator_test.py +++ b/tests/frontends/mpd/translator_test.py @@ -221,18 +221,18 @@ class TracksToTagCacheFormatTest(unittest.TestCase): self.assertEqual(formated, song_list) self.assertEqual(len(result), 0) - def test_tag_cache_suports_directories(self): + def test_tag_cache_supports_directories(self): track = Track(uri='file:///dir/subdir/folder/song.mp3') formated = self.translate(track) result = translator.tracks_to_tag_cache_format([track], self.media_dir) result = self.consume_headers(result) - folder, result = self.consume_directory(result) + dir_data, result = self.consume_directory(result) song_list, result = self.consume_song_list(result) self.assertEqual(len(song_list), 0) self.assertEqual(len(result), 0) - song_list, result = self.consume_song_list(folder) + song_list, result = self.consume_song_list(dir_data) self.assertEqual(len(result), 0) self.assertEqual(formated, song_list) @@ -241,11 +241,11 @@ class TracksToTagCacheFormatTest(unittest.TestCase): result = translator.tracks_to_tag_cache_format([track], self.media_dir) result = self.consume_headers(result) - folder, result = self.consume_directory(result) + dir_data, result = self.consume_directory(result) - self.assertEqual(('directory', 'folder/sub'), folder[0]) - self.assertEqual(('mtime', mtime('.')), folder[1]) - self.assertEqual(('begin', 'sub'), folder[2]) + self.assertEqual(('directory', 'folder/sub'), dir_data[0]) + self.assertEqual(('mtime', mtime('.')), dir_data[1]) + self.assertEqual(('begin', 'sub'), dir_data[2]) def test_tag_cache_suports_sub_directories(self): track = Track(uri='file:///dir/subdir/folder/sub/song.mp3') @@ -254,17 +254,17 @@ class TracksToTagCacheFormatTest(unittest.TestCase): result = self.consume_headers(result) - folder, result = self.consume_directory(result) + dir_data, result = self.consume_directory(result) song_list, result = self.consume_song_list(result) self.assertEqual(len(song_list), 0) self.assertEqual(len(result), 0) - folder, result = self.consume_directory(folder) + dir_data, result = self.consume_directory(dir_data) song_list, result = self.consume_song_list(result) self.assertEqual(len(result), 0) self.assertEqual(len(song_list), 0) - song_list, result = self.consume_song_list(folder) + song_list, result = self.consume_song_list(dir_data) self.assertEqual(len(result), 0) self.assertEqual(formated, song_list) @@ -299,8 +299,8 @@ class TracksToTagCacheFormatTest(unittest.TestCase): result = translator.tracks_to_tag_cache_format(tracks, self.media_dir) result = self.consume_headers(result) - folder, result = self.consume_directory(result) - song_list, song_result = self.consume_song_list(folder) + dir_data, result = self.consume_directory(result) + song_list, song_result = self.consume_song_list(dir_data) self.assertEqual(formated[1], song_list) self.assertEqual(len(song_result), 0) diff --git a/tests/scanner_test.py b/tests/scanner_test.py index 617f2537..edcc2242 100644 --- a/tests/scanner_test.py +++ b/tests/scanner_test.py @@ -196,7 +196,7 @@ class ScannerTest(unittest.TestCase): self.check('scanner/simple/song1.mp3', 'title', 'trackname') self.check('scanner/simple/song1.ogg', 'title', 'trackname') - def test_nonexistant_folder_does_not_fail(self): + def test_nonexistant_dir_does_not_fail(self): self.scan('scanner/does-not-exist') self.assert_(not self.errors) diff --git a/tests/utils/path_test.py b/tests/utils/path_test.py index 461f0809..a73cb8e4 100644 --- a/tests/utils/path_test.py +++ b/tests/utils/path_test.py @@ -13,7 +13,7 @@ from mopidy.utils import path from tests import unittest, path_to_data_dir -class GetOrCreateFolderTest(unittest.TestCase): +class GetOrCreateDirTest(unittest.TestCase): def setUp(self): self.parent = tempfile.mkdtemp() @@ -21,40 +21,40 @@ class GetOrCreateFolderTest(unittest.TestCase): if os.path.isdir(self.parent): shutil.rmtree(self.parent) - def test_creating_folder(self): - folder = os.path.join(self.parent, 'test') - self.assert_(not os.path.exists(folder)) - self.assert_(not os.path.isdir(folder)) - created = path.get_or_create_folder(folder) - self.assert_(os.path.exists(folder)) - self.assert_(os.path.isdir(folder)) - self.assertEqual(created, folder) + def test_creating_dir(self): + dir_path = os.path.join(self.parent, 'test') + self.assert_(not os.path.exists(dir_path)) + self.assert_(not os.path.isdir(dir_path)) + created = path.get_or_create_dir(dir_path) + self.assert_(os.path.exists(dir_path)) + self.assert_(os.path.isdir(dir_path)) + self.assertEqual(created, dir_path) - def test_creating_nested_folders(self): - level2_folder = os.path.join(self.parent, 'test') - level3_folder = os.path.join(self.parent, 'test', 'test') - self.assert_(not os.path.exists(level2_folder)) - self.assert_(not os.path.isdir(level2_folder)) - self.assert_(not os.path.exists(level3_folder)) - self.assert_(not os.path.isdir(level3_folder)) - created = path.get_or_create_folder(level3_folder) - self.assert_(os.path.exists(level2_folder)) - self.assert_(os.path.isdir(level2_folder)) - self.assert_(os.path.exists(level3_folder)) - self.assert_(os.path.isdir(level3_folder)) - self.assertEqual(created, level3_folder) + def test_creating_nested_dirs(self): + level2_dir = os.path.join(self.parent, 'test') + level3_dir = os.path.join(self.parent, 'test', 'test') + self.assert_(not os.path.exists(level2_dir)) + self.assert_(not os.path.isdir(level2_dir)) + self.assert_(not os.path.exists(level3_dir)) + self.assert_(not os.path.isdir(level3_dir)) + created = path.get_or_create_dir(level3_dir) + self.assert_(os.path.exists(level2_dir)) + self.assert_(os.path.isdir(level2_dir)) + self.assert_(os.path.exists(level3_dir)) + self.assert_(os.path.isdir(level3_dir)) + self.assertEqual(created, level3_dir) - def test_creating_existing_folder(self): - created = path.get_or_create_folder(self.parent) + def test_creating_existing_dir(self): + created = path.get_or_create_dir(self.parent) self.assert_(os.path.exists(self.parent)) self.assert_(os.path.isdir(self.parent)) self.assertEqual(created, self.parent) - def test_create_folder_with_name_of_existing_file_throws_oserror(self): + def test_create_dir_with_name_of_existing_file_throws_oserror(self): conflicting_file = os.path.join(self.parent, 'test') open(conflicting_file, 'w').close() - folder = os.path.join(self.parent, 'test') - self.assertRaises(OSError, path.get_or_create_folder, folder) + dir_path = os.path.join(self.parent, 'test') + self.assertRaises(OSError, path.get_or_create_dir, dir_path) class PathToFileURITest(unittest.TestCase): @@ -66,7 +66,7 @@ class PathToFileURITest(unittest.TestCase): result = path.path_to_uri('/etc/fstab') self.assertEqual(result, 'file:///etc/fstab') - def test_folder_and_path(self): + def test_dir_and_path(self): if sys.platform == 'win32': result = path.path_to_uri('C:/WINDOWS/', 'clock.avi') self.assertEqual(result, 'file:///C://WINDOWS/clock.avi') @@ -145,10 +145,10 @@ class SplitPathTest(unittest.TestCase): def test_empty_path(self): self.assertEqual([], path.split_path('')) - def test_single_folder(self): + def test_single_dir(self): self.assertEqual(['foo'], path.split_path('foo')) - def test_folders(self): + def test_dirs(self): self.assertEqual(['foo', 'bar', 'baz'], path.split_path('foo/bar/baz')) def test_initial_slash_is_ignored(self): @@ -190,10 +190,10 @@ class FindFilesTest(unittest.TestCase): def find(self, value): return list(path.find_files(path_to_data_dir(value))) - def test_basic_folder(self): + def test_basic_dir(self): self.assert_(self.find('')) - def test_nonexistant_folder(self): + def test_nonexistant_dir(self): self.assertEqual(self.find('does-not-exist'), []) def test_file(self): @@ -207,7 +207,7 @@ class FindFilesTest(unittest.TestCase): self.assert_( is_bytes(name), '%s is not bytes object' % repr(name)) - def test_ignores_hidden_folders(self): + def test_ignores_hidden_dirs(self): self.assertEqual(self.find('.hidden'), []) def test_ignores_hidden_files(self): From ef57c905262822afa518d7faa6aba7ab73901da6 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Tue, 9 Apr 2013 00:09:33 +0200 Subject: [PATCH 6/6] path: Use our expand_path instead of os.path.expanduser --- mopidy/__main__.py | 7 +++---- mopidy/utils/path.py | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/mopidy/__main__.py b/mopidy/__main__.py index 4e81856a..6a691cce 100644 --- a/mopidy/__main__.py +++ b/mopidy/__main__.py @@ -309,10 +309,9 @@ def validate_config(raw_config, schemas, extensions=None): def create_file_structures(): - path.get_or_create_dir(path.expand_path('$XDG_DATA_DIR/mopidy')) - path.get_or_create_dir(path.expand_path('$XDG_CONFIG_DIR/mopidy')) - path.get_or_create_file( - path.expand_path('$XDG_CONFIG_DIR/mopidy/mopidy.conf')) + path.get_or_create_dir('$XDG_DATA_DIR/mopidy') + path.get_or_create_dir('$XDG_CONFIG_DIR/mopidy') + path.get_or_create_file('$XDG_CONFIG_DIR/mopidy/mopidy.conf') def setup_audio(config): diff --git a/mopidy/utils/path.py b/mopidy/utils/path.py index eb0cbbb0..699b361f 100644 --- a/mopidy/utils/path.py +++ b/mopidy/utils/path.py @@ -28,7 +28,7 @@ XDG_DIRS = { def get_or_create_dir(dir_path): - dir_path = os.path.expanduser(dir_path) + dir_path = expand_path(dir_path) if os.path.isfile(dir_path): raise OSError( 'A file with the same name as the desired dir, ' @@ -40,7 +40,7 @@ def get_or_create_dir(dir_path): def get_or_create_file(filename): - filename = os.path.expanduser(filename) + filename = expand_path(filename) if not os.path.isfile(filename): logger.info('Creating file %s', filename) open(filename, 'w')