Example #1
0
 def _save_m3u(self, playlist, encoding=sys.getfilesystemencoding()):
     if playlist.name:
         name = self._sanitize_m3u_name(playlist.name, encoding)
         uri = translator.path_to_playlist_uri(
             name.encode(encoding) + b'.m3u')
         path = translator.playlist_uri_to_path(uri, self._playlists_dir)
     elif playlist.uri:
         uri = playlist.uri
         path = translator.playlist_uri_to_path(uri, self._playlists_dir)
         name, _ = os.path.splitext(os.path.basename(path).decode(encoding))
     else:
         raise ValueError('M3U playlist needs name or URI')
     translator.save_m3u(path, playlist.tracks, 'latin1')
     # assert playlist name matches file name/uri
     return playlist.replace(uri=uri, name=name)
Example #2
0
 def _save_m3u(self, playlist, encoding=sys.getfilesystemencoding()):
     if playlist.name:
         name = self._sanitize_m3u_name(playlist.name, encoding)
         uri = translator.path_to_playlist_uri(
             name.encode(encoding) + b'.m3u')
         path = translator.playlist_uri_to_path(uri, self._playlists_dir)
     elif playlist.uri:
         uri = playlist.uri
         path = translator.playlist_uri_to_path(uri, self._playlists_dir)
         name, _ = os.path.splitext(os.path.basename(path).decode(encoding))
     else:
         raise ValueError('M3U playlist needs name or URI')
     translator.save_m3u(path, playlist.tracks, 'latin1')
     # assert playlist name matches file name/uri
     return playlist.replace(uri=uri, name=name)
Example #3
0
    def test_save_playlist_with_new_uri(self):
        uri = 'm3u:test.m3u'

        with self.assertRaises(AssertionError):
            self.core.playlists.save(Playlist(uri=uri))

        path = playlist_uri_to_path(uri, self.playlists_dir)
        self.assertFalse(os.path.exists(path))
Example #4
0
    def test_save_playlist_with_new_uri(self):
        uri = 'm3u:test.m3u'

        with self.assertRaises(AssertionError):
            self.core.playlists.save(Playlist(uri=uri))

        path = playlist_uri_to_path(uri, self.playlists_dir)
        self.assertFalse(os.path.exists(path))
Example #5
0
    def test_utf8_playlist_contents_is_replaced_and_written_to_disk(self):
        track = Track(uri=generate_song(1), name='Test\u07b4', length=60000)
        playlist = self.core.playlists.create('test')
        playlist = self.core.playlists.save(playlist.copy(tracks=[track]))
        path = playlist_uri_to_path(playlist.uri, self.playlists_dir)

        with open(path, 'rb') as f:
            m3u = f.read().splitlines()
        self.assertEqual([b'#EXTM3U', b'#EXTINF:60,Test?', track.uri], m3u)
Example #6
0
    def test_saved_playlist_is_persisted(self):
        uri1 = 'm3u:test1.m3u'
        uri2 = 'm3u:test2.m3u'

        path1 = playlist_uri_to_path(uri1, self.playlists_dir)
        path2 = playlist_uri_to_path(uri2, self.playlists_dir)

        playlist = self.core.playlists.create('test1')
        self.assertEqual('test1', playlist.name)
        self.assertEqual(uri1, playlist.uri)
        self.assertTrue(os.path.exists(path1))
        self.assertFalse(os.path.exists(path2))

        playlist = self.core.playlists.save(playlist.copy(name='test2'))
        self.assertEqual('test2', playlist.name)
        self.assertEqual(uri2, playlist.uri)
        self.assertFalse(os.path.exists(path1))
        self.assertTrue(os.path.exists(path2))
Example #7
0
    def test_created_playlist_is_persisted(self):
        uri = 'm3u:test.m3u'
        path = playlist_uri_to_path(uri, self.playlists_dir)
        self.assertFalse(os.path.exists(path))

        playlist = self.core.playlists.create('test')
        self.assertEqual('test', playlist.name)
        self.assertEqual(uri, playlist.uri)
        self.assertTrue(os.path.exists(path))
Example #8
0
    def test_saved_playlist_is_persisted(self):
        uri1 = 'm3u:test1.m3u'
        uri2 = 'm3u:test2.m3u'

        path1 = playlist_uri_to_path(uri1, self.playlists_dir)
        path2 = playlist_uri_to_path(uri2, self.playlists_dir)

        playlist = self.core.playlists.create('test1')
        self.assertEqual('test1', playlist.name)
        self.assertEqual(uri1, playlist.uri)
        self.assertTrue(os.path.exists(path1))
        self.assertFalse(os.path.exists(path2))

        playlist = self.core.playlists.save(playlist.copy(name='test2'))
        self.assertEqual('test2', playlist.name)
        self.assertEqual(uri2, playlist.uri)
        self.assertFalse(os.path.exists(path1))
        self.assertTrue(os.path.exists(path2))
Example #9
0
    def test_created_playlist_is_persisted(self):
        uri = 'm3u:test.m3u'
        path = playlist_uri_to_path(uri, self.playlists_dir)
        self.assertFalse(os.path.exists(path))

        playlist = self.core.playlists.create('test')
        self.assertEqual('test', playlist.name)
        self.assertEqual(uri, playlist.uri)
        self.assertTrue(os.path.exists(path))
Example #10
0
    def test_utf8_playlist_contents_is_replaced_and_written_to_disk(self):
        track = Track(uri=generate_song(1), name='Test\u07b4', length=60000)
        playlist = self.core.playlists.create('test')
        playlist = self.core.playlists.save(playlist.copy(tracks=[track]))
        path = playlist_uri_to_path(playlist.uri, self.playlists_dir)

        with open(path, 'rb') as f:
            m3u = f.read().splitlines()
        self.assertEqual([b'#EXTM3U', b'#EXTINF:60,Test?', track.uri], m3u)
Example #11
0
    def test_playlist_contents_is_written_to_disk(self):
        track = Track(uri=generate_song(1))
        playlist = self.core.playlists.create('test')
        playlist = self.core.playlists.save(playlist.copy(tracks=[track]))
        path = playlist_uri_to_path(playlist.uri, self.playlists_dir)

        with open(path) as f:
            contents = f.read()

        self.assertEqual(track.uri, contents.strip())
Example #12
0
 def delete(self, uri):
     if uri in self._playlists:
         path = translator.playlist_uri_to_path(uri, self._playlists_dir)
         if os.path.exists(path):
             os.remove(path)
         else:
             logger.warn('Trying to delete missing playlist file %s', path)
         del self._playlists[uri]
     else:
         logger.warn('Trying to delete unknown playlist %s', uri)
Example #13
0
 def delete(self, uri):
     if uri in self._playlists:
         path = translator.playlist_uri_to_path(uri, self._playlists_dir)
         if os.path.exists(path):
             os.remove(path)
         else:
             logger.warn('Trying to delete missing playlist file %s', path)
         del self._playlists[uri]
     else:
         logger.warn('Trying to delete unknown playlist %s', uri)
Example #14
0
    def test_extended_playlist_contents_is_written_to_disk(self):
        track = Track(uri=generate_song(1), name='Test', length=60000)
        playlist = self.core.playlists.create('test')
        playlist = self.core.playlists.save(playlist.copy(tracks=[track]))
        path = playlist_uri_to_path(playlist.uri, self.playlists_dir)

        with open(path) as f:
            contents = f.read().splitlines()

        self.assertEqual(contents, ['#EXTM3U', '#EXTINF:60,Test', track.uri])
Example #15
0
    def test_playlist_contents_is_written_to_disk(self):
        track = Track(uri=generate_song(1))
        playlist = self.core.playlists.create('test')
        playlist = self.core.playlists.save(playlist.copy(tracks=[track]))
        path = playlist_uri_to_path(playlist.uri, self.playlists_dir)

        with open(path) as f:
            contents = f.read()

        self.assertEqual(track.uri, contents.strip())
Example #16
0
    def test_load_playlist_with_nonfilesystem_encoding_of_filename(self):
        uri = 'm3u:%s.m3u' % urllib.quote('øæå'.encode('latin-1'))
        path = playlist_uri_to_path(uri, self.playlists_dir)
        with open(path, 'wb+') as f:
            f.write(b'#EXTM3U\n')

        self.core.playlists.refresh()

        self.assertEqual(len(self.core.playlists.as_list()), 1)
        result = self.core.playlists.lookup(uri)
        self.assertEqual('\ufffd\ufffd\ufffd', result.name)
Example #17
0
    def test_delete_playlist_without_file(self):
        playlist = self.core.playlists.create('test')
        self.assertEqual(playlist, self.core.playlists.lookup(playlist.uri))

        path = playlist_uri_to_path(playlist.uri, self.playlists_dir)
        self.assertTrue(os.path.exists(path))

        os.remove(path)
        self.assertFalse(os.path.exists(path))

        self.core.playlists.delete(playlist.uri)
        self.assertIsNone(self.core.playlists.lookup(playlist.uri))
Example #18
0
    def test_delete_playlist_without_file(self):
        playlist = self.core.playlists.create('test')
        self.assertEqual(playlist, self.core.playlists.lookup(playlist.uri))

        path = playlist_uri_to_path(playlist.uri, self.playlists_dir)
        self.assertTrue(os.path.exists(path))

        os.remove(path)
        self.assertFalse(os.path.exists(path))

        self.core.playlists.delete(playlist.uri)
        self.assertIsNone(self.core.playlists.lookup(playlist.uri))
Example #19
0
    def test_load_playlist_with_nonfilesystem_encoding_of_filename(self):
        uri = 'm3u:%s.m3u' % urllib.quote('øæå'.encode('latin-1'))
        path = playlist_uri_to_path(uri, self.playlists_dir)
        with open(path, 'wb+') as f:
            f.write(b'#EXTM3U\n')

        self.core.playlists.refresh()

        self.assertEqual(len(self.core.playlists.as_list()), 1)
        result = self.core.playlists.as_list()
        if platform.system() == 'Darwin':
            self.assertEqual('%F8%E6%E5', result[0].name)
        else:
            self.assertEqual('\ufffd\ufffd\ufffd', result[0].name)
Example #20
0
    def _save_m3u(self, playlist, encoding=sys.getfilesystemencoding()):
        if playlist.name:
            name = self._sanitize_m3u_name(playlist.name, encoding)
            uri = translator.path_to_playlist_uri(
                name.encode(encoding) + b'.m3u')
            path = translator.playlist_uri_to_path(uri, self._playlists_dir)
        elif playlist.uri:
            uri = playlist.uri
            path = translator.playlist_uri_to_path(uri, self._playlists_dir)
            name, _ = os.path.splitext(os.path.basename(path).decode(encoding))
        else:
            raise ValueError('M3U playlist needs name or URI')
        extended = any(track.name for track in playlist.tracks)

        with open(path, 'w') as file_handle:
            if extended:
                file_handle.write('#EXTM3U\n')
            for track in playlist.tracks:
                if extended and track.name:
                    self._write_m3u_extinf(file_handle, track)
                file_handle.write(track.uri + '\n')

        # assert playlist name matches file name/uri
        return playlist.copy(uri=uri, name=name)
Example #21
0
    def _save_m3u(self, playlist, encoding=sys.getfilesystemencoding()):
        if playlist.name:
            name = self._sanitize_m3u_name(playlist.name, encoding)
            uri = translator.path_to_playlist_uri(
                name.encode(encoding) + b'.m3u')
            path = translator.playlist_uri_to_path(uri, self._playlists_dir)
        elif playlist.uri:
            uri = playlist.uri
            path = translator.playlist_uri_to_path(uri, self._playlists_dir)
            name, _ = os.path.splitext(os.path.basename(path).decode(encoding))
        else:
            raise ValueError('M3U playlist needs name or URI')
        extended = any(track.name for track in playlist.tracks)

        with open(path, 'w') as file_handle:
            if extended:
                file_handle.write('#EXTM3U\n')
            for track in playlist.tracks:
                if extended and track.name:
                    self._write_m3u_extinf(file_handle, track)
                file_handle.write(track.uri + '\n')

        # assert playlist name matches file name/uri
        return playlist.copy(uri=uri, name=name)
Example #22
0
 def test_create_sanitizes_playlist_name(self):
     playlist = self.core.playlists.create('  ../../test FOO baR ')
     self.assertEqual('..|..|test FOO baR', playlist.name)
     path = playlist_uri_to_path(playlist.uri, self.playlists_dir)
     self.assertEqual(self.playlists_dir, os.path.dirname(path))
     self.assertTrue(os.path.exists(path))
Example #23
0
 def test_create_sanitizes_playlist_name(self):
     playlist = self.core.playlists.create('  ../../test FOO baR ')
     self.assertEqual('..|..|test FOO baR', playlist.name)
     path = playlist_uri_to_path(playlist.uri, self.playlists_dir)
     self.assertEqual(self.playlists_dir, os.path.dirname(path))
     self.assertTrue(os.path.exists(path))