def test_basic(self): with mock.patch.object(Configuration, '_get_file_contents') as mock_get_contents: mock_get_contents.return_value = '{"a": "first file"}' conf = Configuration('/tmp/file1.json') mock_get_contents.return_value = 'a: "second file"' conf.load_source('/tmp/yaml.yaml') self.assertEqual(conf.get('a'), 'second file')
class Watchtower(): DEFAULT_STORAGE = 'watchtower.storage.memory.MemoryStorage' def __init__(self): self.settings = Configuration('config.yaml', Configuration.Arg('config')) print(' == Watchtower == ') print('\tStorage:', self.settings.get('storage.type')) self._load_storage( self.settings.get('storage.type', self.DEFAULT_STORAGE), self.settings.get('storage.options', {}), ) self.alerts = self._parse_alerts(self.settings.get('alerts', {})) print('\tAlerts:') for alert_name, alert in self.alerts.items(): print('\t\t{}: {}'.format( alert.__class__.__name__, alert.name )) self.checks = self._parse_checks(self.settings.get('checks', [])) print('\tChecks:') for check in self.checks: print('\t\t{}: {}'.format( check.__class__.__name__, check.name )) def _parse_checks(self, checks_config): checks = [] for check_info in checks_config: name = check_info['name'] title = check_info.get('title', None) # Parse Alert alerts = [] alert_names = check_info["alerts"] for alert_name in alert_names: alert = self.alerts.get(alert_name, None) if alert is None: continue else: alerts.append(alert) # Make Alert of Type type_path = check_info['type'] type_class = import_class(type_path) options = check_info.get('options', {}) checks.append(type_class(name, title, options, alerts)) return checks def _parse_alerts(self, alerts_config): alerts = {} for name, alert_info in alerts_config.items(): type_path = alert_info['type'] type_class = import_class(type_path) options = alert_info.get('options', {}) issue_options = alert_info.get('issues', {}) alerts[name] = type_class(name, options, issue_options) return alerts def _load_storage(self, path, settings): StorageClass = import_class(path) self.storage = StorageClass(**settings) return self.storage