Access settings through mopidy.config which can do error handling and in the future combine settings with command line arguments
This commit is contained in:
parent
d620fad453
commit
a9f4860147
@ -1,5 +1,23 @@
|
|||||||
|
from mopidy import settings
|
||||||
|
|
||||||
def get_version():
|
def get_version():
|
||||||
return u'0'
|
return u'0'
|
||||||
|
|
||||||
def get_mpd_protocol_version():
|
def get_mpd_protocol_version():
|
||||||
return u'0.15.0'
|
return u'0.15.0'
|
||||||
|
|
||||||
|
class ConfigError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Config(object):
|
||||||
|
def __getattr__(self, attr):
|
||||||
|
if not hasattr(settings, attr):
|
||||||
|
raise ConfigError(u'Setting "%s" is not set.' % attr)
|
||||||
|
value = getattr(settings, attr)
|
||||||
|
if type(value) != bool and not value:
|
||||||
|
raise ConfigError(u'Setting "%s" is empty.' % attr)
|
||||||
|
if type(value) == unicode:
|
||||||
|
value = value.encode('utf-8')
|
||||||
|
return value
|
||||||
|
|
||||||
|
config = Config()
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import sys
|
|||||||
sys.path.insert(0,
|
sys.path.insert(0,
|
||||||
os.path.abspath(os.path.join(os.path.dirname(__file__), '../')))
|
os.path.abspath(os.path.join(os.path.dirname(__file__), '../')))
|
||||||
|
|
||||||
from mopidy import settings
|
from mopidy import config, ConfigError
|
||||||
from mopidy.server import MpdServer
|
from mopidy.server import MpdServer
|
||||||
from mopidy.backends.despotify import DespotifyBackend
|
from mopidy.backends.despotify import DespotifyBackend
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ def _setup_logging(verbosity_level):
|
|||||||
else:
|
else:
|
||||||
level = logging.INFO
|
level = logging.INFO
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
format=settings.CONSOLE_LOG_FORMAT,
|
format=config.CONSOLE_LOG_FORMAT,
|
||||||
level=level,
|
level=level,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,3 +33,5 @@ if __name__ == '__main__':
|
|||||||
main()
|
main()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
sys.exit('\nInterrupted by user')
|
sys.exit('\nInterrupted by user')
|
||||||
|
except ConfigError, e:
|
||||||
|
sys.exit('%s' % e)
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import sys
|
|||||||
|
|
||||||
import spytify
|
import spytify
|
||||||
|
|
||||||
from mopidy import settings
|
from mopidy import config
|
||||||
from mopidy.backends.base import BaseBackend
|
from mopidy.backends.base import BaseBackend
|
||||||
|
|
||||||
logger = logging.getLogger(u'backends.despotify')
|
logger = logging.getLogger(u'backends.despotify')
|
||||||
@ -17,25 +17,12 @@ def decode(string):
|
|||||||
class DespotifyBackend(BaseBackend):
|
class DespotifyBackend(BaseBackend):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
logger.info(u'Connecting to Spotify')
|
logger.info(u'Connecting to Spotify')
|
||||||
self.spotify = spytify.Spytify(self._username, self._password)
|
self.spotify = spytify.Spytify(
|
||||||
|
config.SPOTIFY_USERNAME, config.SPOTIFY_PASSWORD)
|
||||||
logger.info(u'Preloading data')
|
logger.info(u'Preloading data')
|
||||||
self._playlists
|
self._playlists
|
||||||
logger.debug(u'Done preloading data')
|
logger.debug(u'Done preloading data')
|
||||||
|
|
||||||
@property
|
|
||||||
def _username(self):
|
|
||||||
username = encode(settings.SPOTIFY_USERNAME)
|
|
||||||
if not username:
|
|
||||||
sys.exit(u'Setting SPOTIFY_USERNAME is not set.')
|
|
||||||
return username
|
|
||||||
|
|
||||||
@property
|
|
||||||
def _password(self):
|
|
||||||
password = encode(settings.SPOTIFY_PASSWORD)
|
|
||||||
if not password:
|
|
||||||
sys.exit(u'Setting SPOTIFY_PASSWORD is not set.')
|
|
||||||
return password
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _playlists(self):
|
def _playlists(self):
|
||||||
if not hasattr(self, '_x_playlists') or not self._x_playlists:
|
if not hasattr(self, '_x_playlists') or not self._x_playlists:
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import socket
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from mopidy import settings
|
from mopidy import config
|
||||||
from mopidy.session import MpdSession
|
from mopidy.session import MpdSession
|
||||||
|
|
||||||
logger = logging.getLogger(u'mpdserver')
|
logger = logging.getLogger(u'mpdserver')
|
||||||
@ -16,11 +16,11 @@ class MpdServer(asyncore.dispatcher):
|
|||||||
self.backend = backend
|
self.backend = backend
|
||||||
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self.set_reuse_addr()
|
self.set_reuse_addr()
|
||||||
self.bind((settings.MPD_SERVER_HOSTNAME, settings.MPD_SERVER_PORT))
|
self.bind((config.MPD_SERVER_HOSTNAME, config.MPD_SERVER_PORT))
|
||||||
self.listen(1)
|
self.listen(1)
|
||||||
self.started_at = int(time.time())
|
self.started_at = int(time.time())
|
||||||
logger.info(u'Please connect to %s port %s using an MPD client.',
|
logger.info(u'Please connect to %s port %s using an MPD client.',
|
||||||
settings.MPD_SERVER_HOSTNAME, settings.MPD_SERVER_PORT)
|
config.MPD_SERVER_HOSTNAME, config.MPD_SERVER_PORT)
|
||||||
|
|
||||||
def handle_accept(self):
|
def handle_accept(self):
|
||||||
(client_socket, client_address) = self.accept()
|
(client_socket, client_address) = self.accept()
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
import asynchat
|
import asynchat
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from mopidy import get_mpd_protocol_version, settings
|
from mopidy import get_mpd_protocol_version, config
|
||||||
from mopidy.exceptions import MpdAckError
|
from mopidy.exceptions import MpdAckError
|
||||||
from mopidy.handler import MpdHandler
|
from mopidy.handler import MpdHandler
|
||||||
|
|
||||||
logger = logging.getLogger(u'mpdsession')
|
logger = logging.getLogger(u'mpdsession')
|
||||||
|
|
||||||
def indent(string, places=4, linebreak=settings.MPD_LINE_TERMINATOR):
|
def indent(string, places=4, linebreak=config.MPD_LINE_TERMINATOR):
|
||||||
lines = string.split(linebreak)
|
lines = string.split(linebreak)
|
||||||
if len(lines) == 1:
|
if len(lines) == 1:
|
||||||
return string
|
return string
|
||||||
@ -23,8 +23,8 @@ class MpdSession(asynchat.async_chat):
|
|||||||
self.server = server
|
self.server = server
|
||||||
self.client_address = client_address
|
self.client_address = client_address
|
||||||
self.input_buffer = []
|
self.input_buffer = []
|
||||||
self.set_terminator(settings.MPD_LINE_TERMINATOR.encode(
|
self.set_terminator(config.MPD_LINE_TERMINATOR.encode(
|
||||||
settings.MPD_LINE_ENCODING))
|
config.MPD_LINE_ENCODING))
|
||||||
self.handler = handler_class(session=self, backend=backend)
|
self.handler = handler_class(session=self, backend=backend)
|
||||||
self.send_response(u'OK MPD %s' % get_mpd_protocol_version())
|
self.send_response(u'OK MPD %s' % get_mpd_protocol_version())
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ class MpdSession(asynchat.async_chat):
|
|||||||
def found_terminator(self):
|
def found_terminator(self):
|
||||||
data = ''.join(self.input_buffer).strip()
|
data = ''.join(self.input_buffer).strip()
|
||||||
self.input_buffer = []
|
self.input_buffer = []
|
||||||
input = data.decode(settings.MPD_LINE_ENCODING)
|
input = data.decode(config.MPD_LINE_ENCODING)
|
||||||
logger.debug(u'Input: %s', indent(input))
|
logger.debug(u'Input: %s', indent(input))
|
||||||
self.handle_request(input)
|
self.handle_request(input)
|
||||||
|
|
||||||
@ -54,12 +54,12 @@ class MpdSession(asynchat.async_chat):
|
|||||||
return self.send_response(u'ACK %s' % e)
|
return self.send_response(u'ACK %s' % e)
|
||||||
|
|
||||||
def handle_response(self, response):
|
def handle_response(self, response):
|
||||||
self.send_response(settings.MPD_LINE_TERMINATOR.join(response))
|
self.send_response(config.MPD_LINE_TERMINATOR.join(response))
|
||||||
|
|
||||||
def send_response(self, output):
|
def send_response(self, output):
|
||||||
logger.debug(u'Output: %s', indent(output))
|
logger.debug(u'Output: %s', indent(output))
|
||||||
output = u'%s%s' % (output, settings.MPD_LINE_TERMINATOR)
|
output = u'%s%s' % (output, config.MPD_LINE_TERMINATOR)
|
||||||
data = output.encode(settings.MPD_LINE_ENCODING)
|
data = output.encode(config.MPD_LINE_ENCODING)
|
||||||
self.push(data)
|
self.push(data)
|
||||||
|
|
||||||
def stats_uptime(self):
|
def stats_uptime(self):
|
||||||
|
|||||||
@ -11,3 +11,4 @@ try:
|
|||||||
from mopidy.local_settings import *
|
from mopidy.local_settings import *
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user