curds: Move Root's self.track function into __init__.py
And keep it as a global variable. This reduces the amount of work in the root node, and makes things a little easier to follow Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
52c68eb3e2
commit
1088bb16ce
|
@ -4,35 +4,57 @@ from . import playlist
|
|||
from . import root
|
||||
from .. import data
|
||||
from .. import notify
|
||||
from .. import trak
|
||||
import threading
|
||||
|
||||
Library = library.LibraryPlaylist
|
||||
Lock = threading.Lock()
|
||||
Node = node.PlaylistNode
|
||||
Root = None
|
||||
Lock = threading.Lock()
|
||||
Track = None
|
||||
|
||||
|
||||
def current():
|
||||
return Root.current[0]
|
||||
with Lock:
|
||||
return Root.current[0]
|
||||
|
||||
def lookup(name):
|
||||
return Root.lookup(name)
|
||||
with Lock:
|
||||
return Root.lookup(name)
|
||||
|
||||
def next():
|
||||
global Track
|
||||
with Lock:
|
||||
Track = Root.next()
|
||||
return Track
|
||||
|
||||
def previous():
|
||||
global Track
|
||||
with Lock:
|
||||
Track = Root.previous()
|
||||
return Track
|
||||
|
||||
def save():
|
||||
with Lock:
|
||||
with data.DataFile("playlists.pickle", data.WRITE) as f:
|
||||
f.pickle([ Root ])
|
||||
path = Track.path if Track != None else None
|
||||
f.pickle([ Root, path ])
|
||||
|
||||
def load():
|
||||
global Root
|
||||
global Track
|
||||
with data.DataFile("playlists.pickle", data.READ) as f:
|
||||
if f.exists():
|
||||
[ Root ] = f.unpickle()
|
||||
[ Root, path ] = f.unpickle()
|
||||
Track = trak.lookup(path)
|
||||
if Root == None:
|
||||
Root = root.PlaylistRoot()
|
||||
Root = root.PlaylistRoot()
|
||||
Track = None
|
||||
init()
|
||||
|
||||
def reset():
|
||||
global Track
|
||||
Track = None
|
||||
Root.reset()
|
||||
library.reset()
|
||||
init()
|
||||
|
|
|
@ -28,19 +28,10 @@ class PlaylistRoot(node.PlaylistNode):
|
|||
self.append_child(library.LibraryNode())
|
||||
|
||||
self.current = [ self.lookup("Collection") ]
|
||||
self.track = None
|
||||
|
||||
def __getstate__(self):
|
||||
state = node.PlaylistNode.__getstate__(self)
|
||||
if self.track != None:
|
||||
state["track"] = self.track.path
|
||||
return state
|
||||
|
||||
def __setstate__(self, state):
|
||||
node.PlaylistNode.__setstate__(self, state)
|
||||
self.current = state["current"]
|
||||
self.track = state["track"]
|
||||
self.track = trak.lookup(state["track"]) if self.track else None
|
||||
|
||||
def current_changed(self, old):
|
||||
if old != self.current[0]:
|
||||
|
@ -58,15 +49,15 @@ class PlaylistRoot(node.PlaylistNode):
|
|||
with self.node_lock:
|
||||
old = self.current[0]
|
||||
|
||||
self.track = self.current[0].next()
|
||||
while self.track == None and len(self.current) > 1:
|
||||
track = self.current[0].next()
|
||||
while track == None and len(self.current) > 1:
|
||||
self.current.pop(0)
|
||||
self.track = self.current[0].next()
|
||||
track = self.current[0].next()
|
||||
|
||||
self.lookup("Previous").add(self.track)
|
||||
self.lookup("Previous").add(track)
|
||||
self.current_changed(old)
|
||||
notify.notify("next-track", self.track)
|
||||
return self.track
|
||||
notify.notify("next-track", track)
|
||||
return track
|
||||
|
||||
def peek(self, n):
|
||||
with self.node_lock:
|
||||
|
@ -84,15 +75,13 @@ class PlaylistRoot(node.PlaylistNode):
|
|||
return res
|
||||
|
||||
def previous(self):
|
||||
self.track = self.lookup("Previous").next()
|
||||
return self.track
|
||||
return self.lookup("Previous").next()
|
||||
|
||||
def reset(self):
|
||||
with self.node_lock:
|
||||
for plist in self.children:
|
||||
plist.reset()
|
||||
self.current = [ self.lookup("Collection") ]
|
||||
self.track = None
|
||||
|
||||
def select(self, plist):
|
||||
with self.node_lock:
|
||||
|
|
|
@ -77,7 +77,6 @@ class TestPlaylistRoot(unittest.TestCase):
|
|||
|
||||
self.assertEqual(self.playman.name, "root")
|
||||
self.assertEqual(self.playman.current, [ self.playman.lookup("Collection") ])
|
||||
self.assertEqual(self.playman.track, None)
|
||||
|
||||
def test_root_on_scan(self):
|
||||
plist = self.playman.lookup("Libraries").lookup(test_library)
|
||||
|
@ -91,14 +90,6 @@ class TestPlaylistRoot(unittest.TestCase):
|
|||
self.playman.reset()
|
||||
self.assertEqual(len(self.playman.lookup("Collection")), 0)
|
||||
|
||||
def test_root_getstate(self):
|
||||
self.playman.lookup("Libraries").lookup(test_album)
|
||||
library.join()
|
||||
|
||||
track = self.playman.next()
|
||||
state = self.playman.__getstate__()
|
||||
self.assertEqual(state["current"], [ self.playman.lookup("Collection") ])
|
||||
|
||||
def test_root_setstate(self):
|
||||
self.playman.lookup("Libraries").lookup(test_album)
|
||||
library.join()
|
||||
|
@ -113,11 +104,9 @@ class TestPlaylistRoot(unittest.TestCase):
|
|||
self.assertEqual(self.playman.name, "Test")
|
||||
self.assertEqual(self.playman.icon, "test")
|
||||
self.assertEqual(self.playman.current, [ 1, 2, 3 ])
|
||||
self.assertEqual(self.playman.track, track)
|
||||
|
||||
def test_root_next_prev(self):
|
||||
self.assertIsNone(self.playman.next())
|
||||
self.assertIsNone(self.playman.track)
|
||||
|
||||
clist = self.playman.lookup("Collection")
|
||||
prev = self.playman.lookup("Previous")
|
||||
|
@ -125,7 +114,6 @@ class TestPlaylistRoot(unittest.TestCase):
|
|||
library.join()
|
||||
|
||||
track1 = self.playman.next()
|
||||
self.assertEqual(self.playman.track, track1)
|
||||
self.assertEqual(clist.current, 0)
|
||||
self.assertEqual(track1, clist[0])
|
||||
self.assertEqual(self.cb_track, track1)
|
||||
|
@ -134,7 +122,6 @@ class TestPlaylistRoot(unittest.TestCase):
|
|||
self.assertEqual(prev[0], track1)
|
||||
|
||||
track2 = self.playman.next()
|
||||
self.assertEqual(self.playman.track, track2)
|
||||
self.assertEqual(clist.current, 1)
|
||||
self.assertEqual(track2, clist[1])
|
||||
self.assertEqual(self.cb_track, track2)
|
||||
|
@ -148,7 +135,6 @@ class TestPlaylistRoot(unittest.TestCase):
|
|||
|
||||
self.playman.reset()
|
||||
self.assertEqual(len(self.playman.lookup("Previous")), 0)
|
||||
self.assertIsNone(self.playman.track)
|
||||
|
||||
def test_root_peek(self):
|
||||
self.assertEqual(self.playman.peek(3), [ ])
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
# Copyright 2019 (c) Anna Schumaker.
|
||||
|
||||
class Tag:
|
||||
def extract(info, key, default):
|
||||
pass
|
|
@ -0,0 +1,7 @@
|
|||
# Copyright 2019 (c) Anna Schumaker.
|
||||
from . import tag
|
||||
import unittest
|
||||
|
||||
class TestTag(unittest.TestCase):
|
||||
def test_tag_extract(self):
|
||||
t = tag.Tag()
|
|
@ -2,11 +2,16 @@
|
|||
from . import data
|
||||
from . import notify
|
||||
from . import playlist
|
||||
from . import trak
|
||||
import os
|
||||
import unittest
|
||||
|
||||
test_album = os.path.abspath("./trier/Test Album")
|
||||
|
||||
class TestPlaylist(unittest.TestCase):
|
||||
def setUp(self):
|
||||
data.DataFile("playlists.pickle", data.WRITE).remove()
|
||||
trak.reset()
|
||||
playlist.reset()
|
||||
self.orig = playlist.Root
|
||||
|
||||
|
@ -38,23 +43,67 @@ class TestPlaylist(unittest.TestCase):
|
|||
playlist.reset()
|
||||
self.assertEqual(playlist.UpNext, playlist.lookup("Up Next"))
|
||||
|
||||
def test_next_prev(self):
|
||||
self.assertIsNone(playlist.next())
|
||||
self.assertIsNone(playlist.previous())
|
||||
self.assertIsNone(playlist.Track)
|
||||
|
||||
clist = playlist.lookup("Collection")
|
||||
prev = playlist.lookup("Previous")
|
||||
plist = playlist.lookup("Libraries").lookup(test_album)
|
||||
playlist.library.join()
|
||||
|
||||
track1 = playlist.next()
|
||||
self.assertEqual(playlist.Track, track1)
|
||||
self.assertEqual(clist.current, 0)
|
||||
self.assertEqual(track1, clist[0])
|
||||
|
||||
self.assertEqual(len(prev), 1)
|
||||
self.assertEqual(prev[0], track1)
|
||||
|
||||
track2 = playlist.next()
|
||||
self.assertEqual(playlist.Track, track2)
|
||||
self.assertEqual(clist.current, 1)
|
||||
self.assertEqual(track2, clist[1])
|
||||
|
||||
self.assertEqual(len(prev), 2)
|
||||
self.assertEqual(prev[0], track2)
|
||||
self.assertEqual(prev[1], track1)
|
||||
|
||||
self.assertEqual(playlist.previous(), track1)
|
||||
self.assertEqual(playlist.Track, track1)
|
||||
|
||||
playlist.reset()
|
||||
self.assertEqual(len(prev), 0)
|
||||
self.assertIsNone(playlist.Track)
|
||||
|
||||
def test_save_playlists(self):
|
||||
dfile = data.DataFile("playlists.pickle", data.READ)
|
||||
playlist.Root = None
|
||||
track = trak.lookup(os.path.join(test_album, "01 - Test Track.ogg"))
|
||||
|
||||
playlist.Root = None
|
||||
playlist.Track = 1234
|
||||
|
||||
self.assertFalse(dfile.exists())
|
||||
playlist.load()
|
||||
self.assertIsInstance(playlist.Root, playlist.root.PlaylistRoot)
|
||||
self.assertIsNone(playlist.Track)
|
||||
self.assertEqual(playlist.Starred, playlist.lookup("Playlists").lookup("Starred"))
|
||||
self.assertEqual(playlist.UpNext, playlist.lookup("Up Next"))
|
||||
root = playlist.Root
|
||||
playlist.Track = track
|
||||
|
||||
self.assertFalse(dfile.exists())
|
||||
playlist.Starred.changed()
|
||||
notify.notify_idle()
|
||||
self.assertTrue(dfile.exists())
|
||||
|
||||
playlist.Root = None
|
||||
playlist.Track = None
|
||||
|
||||
playlist.load()
|
||||
self.assertIsNotNone(playlist.Root)
|
||||
self.assertNotEqual(playlist.Root, root)
|
||||
self.assertEqual(playlist.Track, track)
|
||||
self.assertEqual(playlist.Starred, playlist.lookup("Playlists").lookup("Starred"))
|
||||
self.assertEqual(playlist.UpNext, playlist.lookup("Up Next"))
|
||||
|
|
|
@ -67,11 +67,11 @@ class TestAudioWidgets(unittest.TestCase):
|
|||
def test_next_previous_buttons(self):
|
||||
widgets.Next.clicked()
|
||||
self.assertEqual(playbin.get_state(), Gst.State.NULL)
|
||||
self.assertEqual(curds.playlist.Root.track, None)
|
||||
self.assertEqual(curds.playlist.Track, None)
|
||||
|
||||
widgets.Previous.clicked()
|
||||
self.assertEqual(playbin.get_state(), Gst.State.NULL)
|
||||
self.assertEqual(curds.playlist.Root.track, None)
|
||||
self.assertEqual(curds.playlist.Track, None)
|
||||
|
||||
curds.playlist.lookup("Libraries").lookup(test_album)
|
||||
curds.playlist.library.join()
|
||||
|
@ -79,25 +79,25 @@ class TestAudioWidgets(unittest.TestCase):
|
|||
|
||||
widgets.Next.clicked()
|
||||
self.assertEqual(playbin.get_state(), Gst.State.PLAYING)
|
||||
self.assertNotEqual(curds.playlist.Root.track, None)
|
||||
track = curds.playlist.Root.track
|
||||
self.assertNotEqual(curds.playlist.Track, None)
|
||||
track = curds.playlist.Track
|
||||
|
||||
widgets.Next.clicked()
|
||||
gtk.main_loop(delay=0.1)
|
||||
self.assertEqual(playbin.get_state(), Gst.State.PLAYING)
|
||||
self.assertNotEqual(curds.playlist.Root.track, track)
|
||||
track2 = curds.playlist.Root.track
|
||||
self.assertNotEqual(curds.playlist.Track, track)
|
||||
track2 = curds.playlist.Track
|
||||
|
||||
widgets.Previous.clicked()
|
||||
self.assertEqual(playbin.get_state(), Gst.State.PLAYING)
|
||||
self.assertNotEqual(curds.playlist.Root.track, track2)
|
||||
self.assertNotEqual(curds.playlist.Track, track2)
|
||||
|
||||
track3 = curds.playlist.Root.next()
|
||||
track3 = curds.playlist.next()
|
||||
widgets.NEXT_TRACK = track3
|
||||
widgets.Next.clicked()
|
||||
gtk.main_loop(delay=0.2)
|
||||
self.assertEqual(playbin.get_state(), Gst.State.PLAYING)
|
||||
self.assertEqual(curds.playlist.Root.track, track3)
|
||||
self.assertEqual(curds.playlist.Track, track3)
|
||||
|
||||
def test_seek_scale(self):
|
||||
playbin.set_uri(test_track, Gst.State.PAUSED)
|
||||
|
@ -190,23 +190,23 @@ class TestAudioWidgets(unittest.TestCase):
|
|||
curds.playlist.lookup("Libraries").lookup(test_album)
|
||||
curds.playlist.library.join()
|
||||
|
||||
track = curds.playlist.Root.next()
|
||||
track = curds.playlist.next()
|
||||
widgets.on_message(playbin.Bus, Gst.Message.new_eos())
|
||||
self.assertEqual(playbin.get_state(), Gst.State.PLAYING)
|
||||
self.assertNotEqual(curds.playlist.Root.track, track)
|
||||
self.assertNotEqual(curds.playlist.Track, track)
|
||||
|
||||
track = curds.playlist.Root.track
|
||||
track = curds.playlist.Track
|
||||
autopause.Count = 0
|
||||
widgets.on_message(playbin.Bus, Gst.Message.new_eos())
|
||||
self.assertEqual(autopause.Count, -1)
|
||||
self.assertEqual(playbin.get_state(), Gst.State.PAUSED)
|
||||
self.assertNotEqual(curds.playlist.Root.track, track)
|
||||
self.assertNotEqual(curds.playlist.Track, track)
|
||||
|
||||
def test_about_to_finish(self):
|
||||
curds.playlist.lookup("Libraries").lookup(test_album)
|
||||
curds.playlist.library.join()
|
||||
|
||||
track = curds.playlist.Root.next()
|
||||
track = curds.playlist.next()
|
||||
playbin.set_uri(track.path, Gst.State.PLAYING)
|
||||
|
||||
widgets.on_about_to_finish(playbin.Playbin)
|
||||
|
|
|
@ -12,7 +12,7 @@ SEEK_STEP = 10
|
|||
def on_next(button):
|
||||
track = NEXT_TRACK
|
||||
if track == None:
|
||||
track = curds.playlist.Root.next()
|
||||
track = curds.playlist.next()
|
||||
if track != None:
|
||||
playbin.set_uri(track.path, Gst.State.PLAYING)
|
||||
|
||||
|
@ -21,7 +21,7 @@ Next.connect("clicked", on_next)
|
|||
Next.connect("can-activate-accel", gtk.can_activate_accel)
|
||||
|
||||
def on_previous(button):
|
||||
track = curds.playlist.Root.previous()
|
||||
track = curds.playlist.previous()
|
||||
if track != None:
|
||||
playbin.set_uri(track.path, Gst.State.PLAYING)
|
||||
|
||||
|
@ -71,7 +71,7 @@ Title = gtk.Builder.get_object("title")
|
|||
Artist = gtk.Builder.get_object("artist")
|
||||
|
||||
def on_eos(message):
|
||||
track = curds.playlist.Root.next()
|
||||
track = curds.playlist.next()
|
||||
if track != None:
|
||||
state = autopause.decrement()
|
||||
playbin.set_uri(track.path, state)
|
||||
|
@ -119,7 +119,7 @@ playbin.Bus.connect("message", on_message)
|
|||
def on_about_to_finish(pbin):
|
||||
global NEXT_TRACK
|
||||
if not autopause.about_to_pause():
|
||||
NEXT_TRACK = curds.playlist.Root.next()
|
||||
NEXT_TRACK = curds.playlist.next()
|
||||
if NEXT_TRACK:
|
||||
playbin.set_uri(NEXT_TRACK.path)
|
||||
autopause.decrement()
|
||||
|
|
Loading…
Reference in New Issue