[core] Add "Store" interface

Add an interface that allows arbitrary objects to store/retrieve
arbitrary key/value pairs. This will be used for different purposes in
the future:

* Config class(es) can store user-defined parameters for modules
* Widgets can store state
* ???

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-09 07:41:07 +01:00
parent a7e756e015
commit c8a51b416f
2 changed files with 42 additions and 0 deletions

18
bumblebee/store.py Normal file
View file

@ -0,0 +1,18 @@
"""Store interface
Allows arbitrary classes to offer a simple get/set
store interface by deriving from the Store class in
this module
"""
class Store(object):
def __init__(self):
self._data = {}
def set(self, key, value):
self._data[key] = value
def get(self, key, default=None):
return self._data.get(key, default)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

24
tests/test_store.py Normal file
View file

@ -0,0 +1,24 @@
# pylint: disable=C0103,C0111,W0703
import unittest
from bumblebee.store import Store
class TestStore(unittest.TestCase):
def setUp(self):
self.store = Store()
self.anyKey = "some-key"
self.anyValue = "some-value"
self.unsetKey = "invalid-key"
def test_set_value(self):
self.store.set(self.anyKey, self.anyValue)
self.assertEquals(self.store.get(self.anyKey), self.anyValue)
def test_get_invalid_value(self):
result = self.store.get(self.unsetKey)
self.assertEquals(result, None)
def test_get_invalid_with_default_value(self):
result = self.store.get(self.unsetKey, self.anyValue)
self.assertEquals(result, self.anyValue)