iotile.core.utilities.kvstore_json

Module Contents

class JSONKVStore(name, folder=None, respect_venv=False)

A Key Value store based on flat json files with atomic write semantics

This is intended as a drop in replacement for SQLiteKVStore. Note that the implementation is not meant to be efficient in the sense of caching the file in memory. Instead it has read-through and write-through semantics where the file is reloaded every time a request is made.

Args:

name (string): The name of the file to use as a persistent store for this KVStore folder (string): Optional folder to store the file. If None, the system default

settings directory is used
respect_venv (bool): Make folder relative to the current virtual environment if there
is one.
DefaultFolder
_load_file(self)

Load all entries from json backing file

_save_file(self, data)

Attempt to atomically save file by saving and then moving into position

The goal is to make it difficult for a crash to corrupt our data file since the move operation can be made atomic if needed on mission critical filesystems.

get(self, key)

Get a value by its key

Args:
key (string): The key used to store this value
Returns:
value (string): The value associated to the key
Raises:
KeyError: if the key was not found
get_all(self)

Return a list of all (key, value) tuples in the kv store

Returns:
list(string, string): A list of key, value pairs
remove(self, key)

Remove a key from the data store

Args:
key (string): The key to remove
Raises:
KeyError: if the key was not found
try_get(self, key)

Try to get a value by its key, returning None if not found

Args:
key (string): The key used to store this value
Returns:
value (string): The value associated to the key or None
set(self, key, value)

Set the value of a key

Args:
key (string): The key used to store this value value (string): The value to store
clear(self)

Clear all values from this kv store