1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00

Changed how the DaemonClient get its address + better exception

I'm still not satisfied but can't come up with a good solution
This commit is contained in:
spl0k 2019-04-10 21:14:46 +02:00
parent 751f00dac8
commit afbd84a5b9
3 changed files with 24 additions and 7 deletions

View File

@ -16,6 +16,10 @@ except ImportError:
import os import os
import tempfile import tempfile
current_config = None
def get_current_config():
return current_config or DefaultConfig()
class DefaultConfig(object): class DefaultConfig(object):
DEBUG = False DEBUG = False
@ -47,6 +51,9 @@ class DefaultConfig(object):
TRANSCODING = {} TRANSCODING = {}
MIMETYPES = {} MIMETYPES = {}
def __init__(self):
current_config = self
class IniConfig(DefaultConfig): class IniConfig(DefaultConfig):
common_paths = [ common_paths = [
'/etc/supysonic', '/etc/supysonic',
@ -56,6 +63,8 @@ class IniConfig(DefaultConfig):
] ]
def __init__(self, paths): def __init__(self, paths):
super(IniConfig, self).__init__()
parser = RawConfigParser() parser = RawConfigParser()
parser.read(paths) parser.read(paths)

View File

@ -11,12 +11,12 @@ import logging
from multiprocessing.connection import Client, Listener from multiprocessing.connection import Client, Listener
from .config import IniConfig from .config import get_current_config
from .py23 import strtype from .py23 import strtype
from .utils import get_secret_key from .utils import get_secret_key
from .watcher import SupysonicWatcher from .watcher import SupysonicWatcher
__all__ = [ 'Daemon', 'DaemonClient' ] __all__ = [ 'Daemon', 'DaemonClient', 'DaemonUnavailableError' ]
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -25,13 +25,21 @@ WATCHER = 0
W_ADD = 0 W_ADD = 0
W_DEL = 1 W_DEL = 1
class DaemonUnavailableError(Exception):
pass
class DaemonClient(object): class DaemonClient(object):
def __init__(self, address = None): 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') self.__key = get_secret_key('daemon_key')
def __get_connection(self): 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): def add_watched_folder(self, folder):
if not isinstance(folder, strtype): if not isinstance(folder, strtype):

View File

@ -13,7 +13,7 @@ import uuid
from pony.orm import select from pony.orm import select
from pony.orm import ObjectNotFound 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 ..db import Folder, Track, Artist, Album, User, RatingTrack, StarredTrack
from ..py23 import strtype from ..py23 import strtype
@ -47,7 +47,7 @@ class FolderManager:
folder = Folder(root = True, name = name, path = path) folder = Folder(root = True, name = name, path = path)
try: try:
DaemonClient().add_watched_folder(path) DaemonClient().add_watched_folder(path)
except (ConnectionRefusedError, FileNotFoundError): except DaemonUnavailableError:
pass pass
return folder return folder
@ -60,7 +60,7 @@ class FolderManager:
try: try:
DaemonClient().remove_watched_folder(folder.path) DaemonClient().remove_watched_folder(folder.path)
except (ConnectionRefusedError, FileNotFoundError): except DaemonUnavailableError:
pass pass
for user in User.select(lambda u: u.last_play.root_folder == folder): for user in User.select(lambda u: u.last_play.root_folder == folder):