From 628e1a2fb0d82a5a3418e38c686bdc2399ce6ffc Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 5 Aug 2015 13:46:09 +0300 Subject: [PATCH] Make ReportingHandler a proper base class Having a method that raises NotImplementedError is usually not ideal, since the implementations of the class can't delegate control through super to the parent or to other classes found in the MRO. Instead, we mark a priori ReportingHandler as a base class, leaving the implementation of `publish_event` empty. Change-Id: Id5c442b6998743b1caffbad627847ee5e88f2982 --- cloudinit/reporting/handlers.py | 10 +++++++++- cloudinit/tests/test_reporting.py | 9 +++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cloudinit/reporting/handlers.py b/cloudinit/reporting/handlers.py index be323f53..24734b4e 100644 --- a/cloudinit/reporting/handlers.py +++ b/cloudinit/reporting/handlers.py @@ -1,14 +1,22 @@ import abc import logging +import six + from cloudinit.registry import DictRegistry +@six.add_metaclass(abc.ABCMeta) class ReportingHandler(object): + """Base class for report handlers. + + Implement :meth:`~publish_event` for controlling what + the handler does with an event. + """ @abc.abstractmethod def publish_event(self, event): - raise NotImplementedError + """Publish an event to the ``INFO`` log level.""" class LogHandler(ReportingHandler): diff --git a/cloudinit/tests/test_reporting.py b/cloudinit/tests/test_reporting.py index 9c5d1c0b..66958811 100644 --- a/cloudinit/tests/test_reporting.py +++ b/cloudinit/tests/test_reporting.py @@ -92,11 +92,12 @@ class TestReportingEvent(TestCase): self.assertEqual(expected_string_representation, event.as_string()) -class TestReportingHandler(TestCase): +class TestBaseReportingHandler(TestCase): - def test_no_default_publish_event_implementation(self): - self.assertRaises(NotImplementedError, - handlers.ReportingHandler().publish_event, None) + def test_base_reporting_handler_is_abstract(self): + exc = self.assertRaises(TypeError, handlers.ReportingHandler) + self.assertIn("publish_event", str(exc)) + self.assertIn("abstract", str(exc)) class TestLogHandler(TestCase):