1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +00:00

Nicify commands sent to the daemon

This commit is contained in:
spl0k 2019-04-20 11:51:34 +02:00
parent ba289065ce
commit 22a207c79f

View File

@ -20,14 +20,25 @@ __all__ = [ 'Daemon', 'DaemonClient', 'DaemonUnavailableError' ]
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
WATCHER = 0
W_ADD = 0
W_DEL = 1
class DaemonUnavailableError(Exception): class DaemonUnavailableError(Exception):
pass pass
class DaemonCommand(object):
def apply(self, connection, *args):
raise NotImplementedError()
class WatcherCommand(DaemonCommand):
def __init__(self, folder):
self._folder = folder
class AddWatchedFolderCommand(WatcherCommand):
def apply(self, connection, watcher):
watcher.add_folder(self._folder)
class RemoveWatchedFolder(WatcherCommand):
def apply(self, connection, watcher):
watcher.remove_folder(self._folder)
class DaemonClient(object): class DaemonClient(object):
def __init__(self, address = None): def __init__(self, address = None):
self.__address = address or get_current_config().DAEMON['socket'] self.__address = address or get_current_config().DAEMON['socket']
@ -45,13 +56,13 @@ class DaemonClient(object):
if not isinstance(folder, strtype): if not isinstance(folder, strtype):
raise TypeError('Expecting string, got ' + str(type(folder))) raise TypeError('Expecting string, got ' + str(type(folder)))
with self.__get_connection() as c: with self.__get_connection() as c:
c.send((WATCHER, W_ADD, (folder,))) c.send(AddWatchedFolderCommand(folder))
def remove_watched_folder(self, folder): def remove_watched_folder(self, folder):
if not isinstance(folder, strtype): if not isinstance(folder, strtype):
raise TypeError('Expecting string, got ' + str(type(folder))) raise TypeError('Expecting string, got ' + str(type(folder)))
with self.__get_connection() as c: with self.__get_connection() as c:
c.send((WATCHER, W_DEL, (folder,))) c.send(RemoveWatchedFolder(folder))
class Daemon(object): class Daemon(object):
def __init__(self, address): def __init__(self, address):
@ -60,16 +71,10 @@ class Daemon(object):
self.__watcher = None self.__watcher = None
def __handle_connection(self, connection): def __handle_connection(self, connection):
try: cmd = connection.recv()
module, cmd, args = connection.recv() logger.debug('Received %s', cmd)
logger.debug('Received %s %s %s', module, cmd, args) if self.__watcher is not None and isinstance(cmd, WatcherCommand):
if module == WATCHER and self.__watcher is not None: cmd.apply(connection, self.__watcher)
if cmd == W_ADD:
self.__watcher.add_folder(*args)
elif cmd == W_DEL:
self.__watcher.remove_folder(*args)
except ValueError:
logger.warn('Received unknown data')
def run(self, config): def run(self, config):
self.__listener = Listener(address = self.__address, authkey = get_secret_key('daemon_key')) self.__listener = Listener(address = self.__address, authkey = get_secret_key('daemon_key'))
@ -85,4 +90,5 @@ class Daemon(object):
def terminate(self): def terminate(self):
self.__listener.close() self.__listener.close()
if self.__watcher is not None:
self.__watcher.stop() self.__watcher.stop()