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.
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 |