import logging

from pykka.actor import ThreadingActor
from pykka.registry import ActorRegistry

from mopidy import settings
from mopidy.backends.base import (Backend, CurrentPlaylistController,
    LibraryController, PlaybackController, StoredPlaylistsController)
from mopidy.gstreamer import GStreamer

logger = logging.getLogger('mopidy.backends.spotify')

ENCODING = 'utf-8'
BITRATES = {96: 2, 160: 0, 320: 1}

class SpotifyBackend(ThreadingActor, Backend):
    """
    A backend for playing music from the `Spotify <http://www.spotify.com/>`_
    music streaming service. The backend uses the official `libspotify
    <http://developer.spotify.com/en/libspotify/overview/>`_ library and the
    `pyspotify <http://github.com/winjer/pyspotify/>`_ Python bindings for
    libspotify.

    .. note::

        This product uses SPOTIFY(R) CORE but is not endorsed, certified or
        otherwise approved in any way by Spotify. Spotify is the registered
        trade mark of the Spotify Group.

    **Issues:**
    https://github.com/mopidy/mopidy/issues?labels=backend-spotify

    **Dependencies:**

    - libspotify == 0.0.8 (libspotify8 package from apt.mopidy.com)
    - pyspotify == 1.2 (python-spotify package from apt.mopidy.com)

    **Settings:**

    - :attr:`mopidy.settings.SPOTIFY_CACHE_PATH`
    - :attr:`mopidy.settings.SPOTIFY_USERNAME`
    - :attr:`mopidy.settings.SPOTIFY_PASSWORD`
    """

    # Imports inside methods are to prevent loading of __init__.py to fail on
    # missing spotify dependencies.

    def __init__(self, *args, **kwargs):
        from .library import SpotifyLibraryProvider
        from .playback import SpotifyPlaybackProvider
        from .stored_playlists import SpotifyStoredPlaylistsProvider

        super(SpotifyBackend, self).__init__(*args, **kwargs)

        self.current_playlist = CurrentPlaylistController(backend=self)

        library_provider = SpotifyLibraryProvider(backend=self)
        self.library = LibraryController(backend=self,
            provider=library_provider)

        playback_provider = SpotifyPlaybackProvider(backend=self)
        self.playback = PlaybackController(backend=self,
            provider=playback_provider)

        stored_playlists_provider = SpotifyStoredPlaylistsProvider(
            backend=self)
        self.stored_playlists = StoredPlaylistsController(backend=self,
            provider=stored_playlists_provider)

        self.uri_handlers = [u'spotify:', u'http://open.spotify.com/']

        self.gstreamer = None
        self.spotify = None

    def on_start(self):
        gstreamer_refs = ActorRegistry.get_by_class(GStreamer)
        assert len(gstreamer_refs) == 1, 'Expected exactly one running gstreamer.'
        self.gstreamer = gstreamer_refs[0].proxy()

        self.spotify = self._connect()

    def _connect(self):
        from .session_manager import SpotifySessionManager

        logger.info(u'Mopidy uses SPOTIFY(R) CORE')
        logger.debug(u'Connecting to Spotify')
        spotify = SpotifySessionManager(
            settings.SPOTIFY_USERNAME, settings.SPOTIFY_PASSWORD)
        spotify.start()
        return spotify

@@ -2542,16 +2542,166 @@
 = None%0A%0A
+        # Fail early if settings are not present%0A        self.username = settings.SPOTIFY_USERNAME%0A        self.password = settings.SPOTIFY_PASSWORD%0A%0A
     def 
@@ -2910,24 +2910,76 @@
 0%5D.proxy()%0A%0A
+        logger.info(u'Mopidy uses SPOTIFY(R) CORE')%0A
         self
@@ -3094,60 +3094,8 @@
 er%0A%0A
-        logger.info(u'Mopidy uses SPOTIFY(R) CORE')%0A
     
@@ -3181,73 +3181,36 @@
 ger(
-%0A            settings.SPOTIFY_USERNAME, settings.SPOTIFY_PASSWORD
+self.username, self.password
 )%0A  

Make Spotify backend fail early if settings is incomplete