Skip to content

Instantly share code, notes, and snippets.

@dalethestirling
Last active August 29, 2015 14:06
Show Gist options
  • Save dalethestirling/6bb1761f93ba438e3549 to your computer and use it in GitHub Desktop.
Save dalethestirling/6bb1761f93ba438e3549 to your computer and use it in GitHub Desktop.
Simple config object Python

Here is a simple config object that I though would be worth breaking out so that it can be used for other things.

This is currently implemented in rdiff-backup-web, using a JSON file as the imput.

import json
import os
class ConfigException(Exception):
"""Config general Exception"""
pass
class Config(dict):
def __init__(self, config_file='../config/config.json'):
# Store absolute path to config param
self.path = os.path.abspath(config_file)
# Store raw JSON file
if os.path.isfile(config_file):
self.raw = json.loads(config_file)
else:
raise ConfigException('Config File not Found')
if self._validate_config(self.raw):
# Add config options to dict interface
self.update(self.raw)
# Add config objects as attr
for attr in self.raw:
setattr(self, attr, self.raw[attr])
else:
raise ConfigException('Config Not Correct')
def _validate_config(self):
# Extend to validate file and return booleen pass/fail
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment