def test_new_notifier_without_alert_and_fallback(self): configuration = Configuration('Foo') data = { 'state': ['some', 'state'], 'inform': ['some', 'inform'], 'confirm': ['some', 'confirm'], } with self.assertRaises(ValueError): new_notifier(configuration, 'command', data)
def test_new_notifier_without_alert_and_fallback(self): with NamedTemporaryFile(mode='a+t') as state_file: with NamedTemporaryFile(mode='a+t') as inform_file: with NamedTemporaryFile(mode='a+t') as confirm_file: data = { 'state': state_file.name, 'inform': inform_file.name, 'confirm': confirm_file.name, } configuration = Mock(Configuration) with self.assertRaises(ValueError): new_notifier(configuration, 'file', data)
def test_new_notifier_with_fallback_only(self): configuration = Configuration('Foo') data = { 'fallback': ['some', 'fallback'], } notifier = new_notifier(configuration, 'command', data) self.assertIsInstance(notifier, CommandNotifier)
def test_new_notifier_with_fallback_only(self): with NamedTemporaryFile(mode='a+t') as fallback_file: configuration = Configuration('Foo') data = { 'fallback': fallback_file.name, } notifier = new_notifier(configuration, 'file', data) self.assertIsInstance(notifier, FileNotifier)
def test_new_notifier_without_fallback(self): configuration = Configuration('Foo') data = { 'state': ['some', 'state'], 'inform': ['some', 'inform'], 'confirm': ['some', 'confirm'], 'alert': ['some', 'alert'], } notifier = new_notifier(configuration, 'command', data) self.assertIsInstance(notifier, CommandNotifier)
def test_new_notifier_without_fallback(self): with NamedTemporaryFile(mode='a+t') as state_file: with NamedTemporaryFile(mode='a+t') as inform_file: with NamedTemporaryFile(mode='a+t') as confirm_file: with NamedTemporaryFile(mode='a+t') as alert_file: configuration = Configuration('Foo') data = { 'state': state_file.name, 'inform': inform_file.name, 'confirm': confirm_file.name, 'alert': alert_file.name, } notifier = new_notifier(configuration, 'file', data) self.assertIsInstance(notifier, FileNotifier)
def test_new_notifier(self): configuration = Mock(Configuration) notifier = new_notifier(configuration, 'stdio') self.assertIsInstance(notifier, StdioNotifier)
def test_new_notifier_of_unknown_type(self): with self.assertRaises(ValueError): configuration = Mock(Configuration) new_notifier(configuration, 'NonExistentType')
def from_configuration_data(configuration_file_path, data, verbose=None, interactive=None): """Parse configuration from raw, built-in types such as dictionaries, lists, and scalars. :param configuration_file_path: str :param data: dict :param verbose: Optional[bool] :param interactive: Optional[bool] :return: cls :raise: ValueError """ name = data['name'] if 'name' in data else configuration_file_path working_directory = os.path.dirname(configuration_file_path) if verbose is None and 'verbose' in data: if not isinstance(data['verbose'], bool): raise ValueError('`verbose` must be a boolean.') verbose = data['verbose'] if interactive is None: interactive = True if 'interactive' in data: if not isinstance(data['interactive'], bool): raise ValueError('`interactive` must be a boolean.') interactive = data['interactive'] configuration = Configuration(name, working_directory, verbose, interactive) if 'logging' in data: logging_config.dictConfig(data['logging']) notifier = GroupedNotifiers() if 'notifications' in data: for notifier_data in data['notifications']: if 'type' not in notifier_data: raise ValueError('`notifiers[][type]` is required.') notifier_configuration = notifier_data[ 'configuration'] if 'configuration' in notifier_data else None notifier.notifiers.append( new_notifier(configuration, notifier_data['type'], notifier_configuration)) if not configuration.verbose: notifier = QuietNotifier(notifier) configuration.notifier = notifier if 'source' not in data: raise ValueError('`source` is required.') if 'type' not in data['source']: raise ValueError('`source[type]` is required.') source_configuration = data['source'][ 'configuration'] if 'configuration' in data['source'] else None configuration.source = new_source(configuration, data['source']['type'], source_configuration) if 'target' not in data: raise ValueError('`target` is required.') if 'type' not in data['target']: raise ValueError('`target[type]` is required.') target_configuration = data['target'][ 'configuration'] if 'configuration' in data['target'] else None configuration.target = new_target(configuration, data['target']['type'], target_configuration) return configuration