Skip to content

Commit

Permalink
Merge pull request #372 from jodal/feature/add-extension-superclass
Browse files Browse the repository at this point in the history
Add extension superclass and exception
  • Loading branch information
adamcik committed Apr 1, 2013
2 parents b095f14 + 64b0cc7 commit bd90ed6
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/extensiondev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions mopidy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ class SettingsError(MopidyException):

class OptionalDependencyError(MopidyException):
pass


class ExtensionError(MopidyException):
pass
27 changes: 27 additions & 0 deletions mopidy/ext.py
Original file line number Diff line number Diff line change
@@ -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 changes: 25 additions & 0 deletions tests/exceptions_test.py
Original file line number Diff line number Diff line change
@@ -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 changes: 34 additions & 0 deletions tests/ext_test.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit bd90ed6

Please sign in to comment.