2020-01-25 14:20:25 +01:00
|
|
|
"""Store interface
|
|
|
|
|
|
|
|
Allows arbitrary classes to offer a simple get/set
|
|
|
|
store interface by deriving from the Store class in
|
|
|
|
this module
|
|
|
|
"""
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-01-25 14:20:25 +01:00
|
|
|
class Store(object):
|
|
|
|
"""Interface for storing and retrieving simple values"""
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-01-25 14:20:25 +01:00
|
|
|
def __init__(self):
|
2020-02-08 13:45:52 +01:00
|
|
|
super(Store, self).__init__()
|
2020-01-25 14:20:25 +01:00
|
|
|
self._data = {}
|
|
|
|
|
|
|
|
def set(self, key, value):
|
2020-05-05 20:37:00 +02:00
|
|
|
"""Sets key to value, overwriting any existing data for that key
|
|
|
|
|
|
|
|
:param key: the name of the parameter to set
|
|
|
|
:param value: the value to be set
|
|
|
|
"""
|
2020-05-03 11:15:52 +02:00
|
|
|
self._data[key] = {"value": value, "used": False}
|
2020-01-25 14:20:25 +01:00
|
|
|
|
|
|
|
def unused_keys(self):
|
2020-05-05 20:37:00 +02:00
|
|
|
"""Returns a list of unused keys
|
|
|
|
|
|
|
|
:return: a list of keys that are set, but never used
|
|
|
|
:rtype: list of strings
|
|
|
|
"""
|
2020-05-03 11:15:52 +02:00
|
|
|
return [key for key, value in self._data.items() if value["used"] == False]
|
2020-01-25 14:20:25 +01:00
|
|
|
|
|
|
|
def get(self, key, default=None):
|
2020-05-05 20:37:00 +02:00
|
|
|
"""Returns the current value for the specified key, or a default value,
|
|
|
|
if the key is not set
|
|
|
|
|
|
|
|
:param key: the name of the parameter to retrieve
|
|
|
|
:param default: the default value to return, defaults to None
|
|
|
|
"""
|
2020-01-25 14:20:25 +01:00
|
|
|
if key in self._data:
|
2020-05-03 11:15:52 +02:00
|
|
|
self._data[key]["used"] = True
|
|
|
|
return self._data.get(key, {"value": default})["value"]
|
|
|
|
|
2020-01-25 14:20:25 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|