Created
September 3, 2013 07:37
-
-
Save Perlence/6420753 to your computer and use it in GitHub Desktop.
Acquire access to JSON configs via ConfigParser interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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