lib: Give Tags functions for saving and restoring state

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-06-21 13:38:22 -04:00
parent c342d906e3
commit 5cdf01ddea
2 changed files with 24 additions and 0 deletions

View File

@ -6,12 +6,21 @@ class Tag:
self.tracks = [ ]
self.widgets = None
def __getstate__(self):
return { "name" : self.name,
"tracks" : self.tracks }
def __len__(self):
return len(self.tracks)
def __lt__(self, rhs):
return self.name < rhs.name
def __setstate__(self, state):
self.name = state["name"]
self.tracks = state["tracks"]
self.widgets = None
def add_track(self, track):
self.tracks.append(track)

View File

@ -23,6 +23,21 @@ class TestTag(unittest.TestCase):
self.assertFalse(b < a)
self.assertFalse(a < a)
def test_tag_state(self):
t = tag.Tag("test")
t.tracks = [ 1, 2, 3, 4, 5 ]
state = t.__getstate__()
self.assertEqual(set(state.keys()), set([ "name", "tracks" ]))
self.assertEqual(state["name"], "test")
self.assertEqual(state["tracks"], [ 1, 2, 3, 4, 5 ])
t.__dict__.clear()
t.__setstate__(state)
self.assertEqual(t.name, "test")
self.assertEqual(t.tracks, [ 1, 2, 3, 4, 5 ])
self.assertEqual(t.widgets, None)
def test_tag_add_track(self):
t = tag.Tag("test")
t.add_track(1)