Skip to content

Instantly share code, notes, and snippets.

@Perlence
Created September 3, 2013 07:37
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 Perlence/6420753 to your computer and use it in GitHub Desktop.
Save Perlence/6420753 to your computer and use it in GitHub Desktop.
Acquire access to JSON configs via ConfigParser interface
import json
import ConfigParser
class JSONConfig(ConfigParser.RawConfigParser):
def write(self, fp):
output = self._dict()
if self._defaults:
for (key, value) in self._defaults.items():
output[key] = value
for section in self._sections:
output[section] = {}
for (key, value) in self._sections[section].items():
if key == "__name__":
continue
if (value is not None) or (self._optcre == self.OPTCRE):
output[section][key] = value
json.dump(output, fp, indent=4)
def _read(self, fp, fpname):
obj = json.load(fp, object_hook=self._dict)
for key, value in obj.items():
if not isinstance(value, self._dict):
self._defaults[key] = value
else:
self._sections[key] = value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment