Adder Playlist.path property to Playlist so we dont always have to get the path by the title.

This commit is contained in:
The Wobbler 2025-02-01 13:19:43 +01:00
parent 429ec8e683
commit 8dddeac673

View file

@ -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)