Merge pull request #372 from jodal/feature/add-extension-superclass

Add extension superclass and exception
This commit is contained in:
Thomas Adamcik 2013-04-01 02:43:47 -07:00
commit bd90ed63e8
5 changed files with 94 additions and 4 deletions

View File

@ -265,13 +265,13 @@ meaningful defaults blank, like ``username`` and ``password``.
# You will typically only implement one of the next three methods
# in a single extension.
def get_frontend_class(self):
def get_frontend_classes(self):
from .frontend import SoundspotFrontend
return SoundspotFrontend
return [SoundspotFrontend]
def get_backend_class(self):
def get_backend_classes(self):
from .backend import SoundspotBackend
return SoundspotBackend
return [SoundspotBackend]
def register_gstreamer_elements(self):
from .mixer import SoundspotMixer

View File

@ -22,3 +22,7 @@ class SettingsError(MopidyException):
class OptionalDependencyError(MopidyException):
pass
class ExtensionError(MopidyException):
pass

27
mopidy/ext.py Normal file
View File

@ -0,0 +1,27 @@
from __future__ import unicode_literals
class Extension(object):
name = None
version = None
def get_default_config(self):
raise NotImplementedError(
'Add at least a config section with "enabled = true"')
def validate_config(self, config):
raise NotImplementedError(
'You must explicitly pass config validation if not needed')
def validate_environment(self):
pass
def get_frontend_classes(self):
return []
def get_backend_classes(self):
return []
def register_gstreamer_elements(self):
pass

25
tests/exceptions_test.py Normal file
View File

@ -0,0 +1,25 @@
from __future__ import unicode_literals
from mopidy import exceptions
from tests import unittest
class ExceptionsTest(unittest.TestCase):
def test_exception_can_include_message_string(self):
exc = exceptions.MopidyException('foo')
self.assertEqual(exc.message, 'foo')
self.assertEqual(str(exc), 'foo')
def test_settings_error_is_a_mopidy_exception(self):
self.assert_(issubclass(
exceptions.SettingsError, exceptions.MopidyException))
def test_optional_dependency_error_is_a_mopidy_exception(self):
self.assert_(issubclass(
exceptions.OptionalDependencyError, exceptions.MopidyException))
def test_extension_error_is_a_mopidy_exception(self):
self.assert_(issubclass(
exceptions.ExtensionError, exceptions.MopidyException))

34
tests/ext_test.py Normal file
View File

@ -0,0 +1,34 @@
from __future__ import unicode_literals
from mopidy.ext import Extension
from tests import unittest
class ExtensionTest(unittest.TestCase):
def setUp(self):
self.ext = Extension()
def test_name_is_none(self):
self.assertIsNone(self.ext.name)
def test_version_is_none(self):
self.assertIsNone(self.ext.version)
def test_get_default_config_raises_not_implemented(self):
self.assertRaises(NotImplementedError, self.ext.get_default_config)
def test_validate_config_raises_not_implemented(self):
self.assertRaises(NotImplementedError, self.ext.validate_config, None)
def test_validate_environment_does_nothing_by_default(self):
self.assertIsNone(self.ext.validate_environment())
def test_get_frontend_classes_returns_an_empty_list(self):
self.assertListEqual(self.ext.get_frontend_classes(), [])
def test_get_backend_classes_returns_an_empty_list(self):
self.assertListEqual(self.ext.get_backend_classes(), [])
def test_register_gstreamer_elements_does_nothing_by_default(self):
self.assertIsNone(self.ext.register_gstreamer_elements())