From 8dddeac673b3eae9178c9fd9f3e1c1d740ec2e38 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Sat, 1 Feb 2025 13:19:43 +0100 Subject: [PATCH] Adder Playlist.path property to Playlist so we dont always have to get the path by the title. --- wobuzz/player/playlist.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/wobuzz/player/playlist.py b/wobuzz/player/playlist.py index 1160128..26bad40 100644 --- a/wobuzz/player/playlist.py +++ b/wobuzz/player/playlist.py @@ -20,6 +20,10 @@ class Playlist: self.current_track: Track | None = None self.view = None + self.path = os.path.expanduser( + f"{app.settings.library_path}/playlists/{self.title.replace(" ", "_")}.wbz.m3u" + ) + def clear(self): self.sorting: list[Qt.SortOrder] | None = None self.tracks = [] @@ -119,36 +123,29 @@ class Playlist: for track in self.tracks: wbz_data += f"{track.path}\n" - wbz = open( - os.path.expanduser( - f"{self.app.settings.library_path}/playlists/{self.title.replace(" ", "_")}.wbz.m3u" - ), - "w" - ) + wbz = open(self.path, "w") wbz.write(wbz_data) wbz.close() def rename(self, title: str): # remove from unique names so a new playlist can have the old name and delete old playlist. - path = f"{self.app.settings.library_path}/playlists/{self.title.replace(" ", "_")}.wbz.m3u" - path = os.path.expanduser(path) - - if os.path.exists(path): - os.remove(os.path.expanduser(path)) + if os.path.exists(self.path): + os.remove(self.path) old_title = self.title self.title = self.app.utils.unique_name(title, ignore=old_title) + self.path = os.path.expanduser( + f"{self.app.settings.library_path}/playlists/{self.title.replace(" ", "_")}.wbz.m3u" + ) + if not old_title == self.title: # remove only when the playlist actually has a different name self.app.utils.unique_names.remove(old_title) def delete(self): - path = f"{self.app.settings.library_path}/playlists/{self.title.replace(" ", "_")}.wbz.m3u" - path = os.path.expanduser(path) - - if os.path.exists(path): - os.remove(os.path.expanduser(path)) + if os.path.exists(self.path): + os.remove(self.path) self.app.utils.unique_names.remove(self.title) self.app.library.playlists.remove(self)