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
This commit is contained in:
parent
523967580a
commit
628e1a2fb0
|
@ -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):
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue