Skip to content

Instantly share code, notes, and snippets.

@Azeirah
Created October 16, 2016 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Azeirah/67a378a9b4826347c257c8b8772dad9c to your computer and use it in GitHub Desktop.
Save Azeirah/67a378a9b4826347c257c8b8772dad9c to your computer and use it in GitHub Desktop.
Confy, dictionary backed by a json file, use it for your configuration files.
"""Confy, a dictionary backed by a json file, serves as a replacement to ini/config files. Use like any other dict, call .save() to save.
Initialize it with a filename, the backing file will be created if it didn't previously exist,
otherwise, the contents of the file will be loaded into the dict.
save using confy.save(), loading happens automatically
"""
import json
import collections
import os.path
class Confy(collections.MutableMapping):
def __init__(self, filename, *args, **kwargs):
self.store = dict()
self.filename = filename
self.update(dict(*args, **kwargs)) # use the free update to set keys
if os.path.isfile(self.filename):
# if storage exists, load it into this dict
with open(self.filename) as f:
self.store = json.load(f)
else:
# if storage doesn't exist, create it
with open(self.filename, 'w') as f:
f.write("{}") # write an empty dictionary to the file
def __getitem__(self, key):
return self.store[self.__keytransform__(key)]
def __setitem__(self, key, value):
self.store[self.__keytransform__(key)] = value
def __delitem__(self, key):
del self.store[self.__keytransform__(key)]
def __iter__(self):
return iter(self.store)
def __len__(self):
return len(self.store)
def __keytransform__(self, key):
return key
def save(self):
with open(self.filename, 'w') as f:
json.dump(self.store, f, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment