""" Replacement for shelve, using json. This is currently needed to correctly support db between Python 2 and 3. """ __all__ = ["JsonStore"] import io import sys from json import load, dump, dumps from os.path import exists IS_PY3 = sys.version_info[0] >= 3 class JsonStore(object): def __init__(self, filename): super(JsonStore, self).__init__() self.filename = filename self.data = {} if exists(filename): try: with io.open(filename, encoding='utf-8') as fd: self.data = load(fd) except ValueError: print("Unable to read the state.db, content will be replaced.") def __getitem__(self, key): return self.data[key] def __setitem__(self, key, value): self.data[key] = value self.sync() def __delitem__(self, key): del self.data[key] self.sync() def __contains__(self, item): return item in self.data def get(self, item, default=None): return self.data.get(item, default) def keys(self): return self.data.keys() def sync(self): # http://stackoverflow.com/questions/12309269/write-json-data-to-file-in-python/14870531#14870531 if IS_PY3: with open(self.filename, 'w') as fd: dump(self.data, fd, ensure_ascii=False) else: with io.open(self.filename, 'w', encoding='utf-8') as fd: fd.write(unicode(dumps(self.data, ensure_ascii=False)))