lib: Give TagStores functions for saving and restoring

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-06-21 14:11:12 -04:00
parent 0b7d505688
commit def33c625a
2 changed files with 24 additions and 0 deletions

View File

@ -10,6 +10,10 @@ class TagStore:
self.Added = publisher.Publisher()
self.Removed = publisher.Publisher()
def __getstate__(self):
with self.lock:
return { "store" : self.store }
def __get_tag__(self, name):
with self.lock:
if (t := self.store.get(name)) != None:
@ -19,6 +23,12 @@ class TagStore:
self.Added.publish(t)
return t
def __setstate__(self, state):
self.store = state["store"]
self.lock = threading.Lock()
self.Added = publisher.Publisher()
self.Removed = publisher.Publisher()
def add(self, name, track=None):
t = self.__get_tag__(name.strip())
if track:

View File

@ -67,3 +67,17 @@ class TestTagStore(unittest.TestCase):
self.assertNotIn(tag.name, store.store.keys())
self.assertEqual(store.Added.subscribers, set())
self.assertEqual(store.Removed.subscribers, set())
def test_tag_store_state(self):
store = tagstore.TagStore()
tag = store.add("test", 1)
state = store.__getstate__()
self.assertEqual(set(state.keys()), set([ "store" ]))
store.__dict__.clear()
store.__setstate__(state)
self.assertEqual(store.store, { "test" : tag })
self.assertIsInstance(store.lock, type(threading.Lock()))
self.assertIsInstance(store.Added, publisher.Publisher)
self.assertIsInstance(store.Removed, publisher.Publisher)