diff --git a/supysonic/config.py b/supysonic/config.py index 8c4a2cb..2c5f495 100644 --- a/supysonic/config.py +++ b/supysonic/config.py @@ -16,6 +16,10 @@ except ImportError: import os import tempfile +current_config = None +def get_current_config(): + return current_config or DefaultConfig() + class DefaultConfig(object): DEBUG = False @@ -47,6 +51,9 @@ class DefaultConfig(object): TRANSCODING = {} MIMETYPES = {} + def __init__(self): + current_config = self + class IniConfig(DefaultConfig): common_paths = [ '/etc/supysonic', @@ -56,6 +63,8 @@ class IniConfig(DefaultConfig): ] def __init__(self, paths): + super(IniConfig, self).__init__() + parser = RawConfigParser() parser.read(paths) diff --git a/supysonic/daemon.py b/supysonic/daemon.py index adef113..8fa1848 100644 --- a/supysonic/daemon.py +++ b/supysonic/daemon.py @@ -11,12 +11,12 @@ import logging from multiprocessing.connection import Client, Listener -from .config import IniConfig +from .config import get_current_config from .py23 import strtype from .utils import get_secret_key from .watcher import SupysonicWatcher -__all__ = [ 'Daemon', 'DaemonClient' ] +__all__ = [ 'Daemon', 'DaemonClient', 'DaemonUnavailableError' ] logger = logging.getLogger(__name__) @@ -25,13 +25,21 @@ WATCHER = 0 W_ADD = 0 W_DEL = 1 +class DaemonUnavailableError(Exception): + pass + class DaemonClient(object): def __init__(self, address = None): - self.__address = address or IniConfig.from_common_locations().DAEMON['socket'] + self.__address = address or get_current_config().DAEMON['socket'] self.__key = get_secret_key('daemon_key') def __get_connection(self): - return Client(address = self.__address, authkey = self.__key) + if not self.__address: + raise DaemonUnavailableError('No daemon address set') + try: + return Client(address = self.__address, authkey = self.__key) + except (FileNotFoundError, ConnectionRefusedError): + raise DaemonUnavailableError("Couldn't connect to daemon at {}".format(self.__address)) def add_watched_folder(self, folder): if not isinstance(folder, strtype): diff --git a/supysonic/managers/folder.py b/supysonic/managers/folder.py index ffbcb50..a4aa2fd 100644 --- a/supysonic/managers/folder.py +++ b/supysonic/managers/folder.py @@ -13,7 +13,7 @@ import uuid from pony.orm import select from pony.orm import ObjectNotFound -from ..daemon import DaemonClient +from ..daemon import DaemonClient, DaemonUnavailableError from ..db import Folder, Track, Artist, Album, User, RatingTrack, StarredTrack from ..py23 import strtype @@ -47,7 +47,7 @@ class FolderManager: folder = Folder(root = True, name = name, path = path) try: DaemonClient().add_watched_folder(path) - except (ConnectionRefusedError, FileNotFoundError): + except DaemonUnavailableError: pass return folder @@ -60,7 +60,7 @@ class FolderManager: try: DaemonClient().remove_watched_folder(folder.path) - except (ConnectionRefusedError, FileNotFoundError): + except DaemonUnavailableError: pass for user in User.select(lambda u: u.last_play.root_folder == folder):