From 6f1cf7b125325ec2d95986a60ee34ca127129fc8 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 19 Sep 2009 09:32:15 +0200 Subject: mpclient: change tagtypes, urlhandlers and commands to vars --- nephilim/mpclient.py | 130 ++++++++++++++++++++-------------------- nephilim/plugins/Filebrowser.py | 2 +- nephilim/plugins/Library.py | 2 +- nephilim/plugins/Songinfo.py | 4 +- 4 files changed, 69 insertions(+), 69 deletions(-) diff --git a/nephilim/mpclient.py b/nephilim/mpclient.py index 3d74bab..31b242f 100644 --- a/nephilim/mpclient.py +++ b/nephilim/mpclient.py @@ -26,8 +26,13 @@ from song import Song, SongRef, PlaylistEntryRef class MPClient(QtCore.QObject): """This class offers another layer above pympd, with usefull events.""" # public, read-only - "a list of audio outputs" - outputs = None + logger = None + + # these don't change while mpd is running + outputs = None + tagtypes = None + urlhandlers = None + commands = None # private __password = None @@ -38,13 +43,11 @@ class MPClient(QtCore.QObject): 'time' : 0, 'length' : 0, 'xfade' : 0, 'state' : 'stop', 'single' : 0, 'consume' : 0} - _commands = None _timer_id = None #for querying status changes _db_timer_id = None #for querying db updates _db_update = None #time of last db update - logger = None # SIGNALS connect_changed = QtCore.pyqtSignal(bool) @@ -59,43 +62,12 @@ class MPClient(QtCore.QObject): consume_changed = QtCore.pyqtSignal(bool) playlist_changed = QtCore.pyqtSignal() - class Output(QtCore.QObject): - """This class represents an MPD audio output.""" - # public, const - mpclient = None - name = None - id = None - state = None - - # SIGNALS - state_changed = QtCore.pyqtSignal(bool) - - #### public #### - def __init__(self, mpclient, name, id, state): - QtCore.QObject.__init__(self) - - self.mpclient = mpclient - self.name = name - self.id = id - self.state = state - - @QtCore.pyqtSlot(bool) - def set_state(self, state): - self.mpclient.set_output(self.id, state) - - #### private #### - def mpd_toggle_state(self): - """This is called by mpclient to inform about output state change.""" - self.state = not self.state - self.state_changed.emit(self.state) def __init__(self): QtCore.QObject.__init__(self) - - self.outputs = [] - self._commands = [] - self._status = dict(MPClient._status) - self.logger = logging.getLogger('mpclient') + self.logger = logging.getLogger('mpclient') + self.__update_static() + self._status = dict(MPClient._status) def connect_mpd(self, host, port, password = None): """Connect to MPD@host:port, optionally using password.""" @@ -124,7 +96,7 @@ class MPClient(QtCore.QObject): try: self._client.password(password) self.logger.info('Successfully authenticated') - self._commands = list(self._client.commands()) + self.__update_static() except mpd.CommandError: self.logger.error('Incorrect MPD password.') def is_connected(self): @@ -181,7 +153,6 @@ class MPClient(QtCore.QObject): self._client.update(path) list(self._client.command_list_end()) - def volume(self): """Get current volume.""" return int(self._status['volume']) @@ -196,21 +167,6 @@ class MPClient(QtCore.QObject): except mpd.CommandError, e: self.logger.warning('Error setting volume (probably no outputs enabled): %s.'%e) - def urlhandlers(self): - """Returns an array of available url handlers.""" - if not self._client: - return [] - else: - return list(self._client.urlhandlers()) - def tagtypes(self): - """Returns a list of supported tags.""" - if not self.__check_command_ok('tagtypes'): - return [] - - return map(str.lower, list(self._client.tagtypes()) + ['file']) - def commands(self): - """List all currently available MPD commands.""" - return self._commands def stats(self): """Get MPD statistics.""" return self._client.stats() @@ -355,14 +311,13 @@ class MPClient(QtCore.QObject): if self.__password: self.password(self.__password) else: - self._commands = list(self._client.commands()) + self.__update_static() if not self.__check_command_ok('listallinfo'): self.logger.error('Don\'t have MPD read permission, diconnecting.') return self.disconnect_mpd() self.__update_current_song() - self.__update_outputs() self._db_update = self.stats()['db_update'] self.connect_changed.emit(True) @@ -380,8 +335,7 @@ class MPClient(QtCore.QObject): self._db_timer_id = None self._status = dict(MPClient._status) self._cur_song = None - self._commands = [] - self.outputs = [] + self.__update_static() self.connect_changed.emit(False) self.logger.info('Disconnected from MPD.') def __update_current_song(self): @@ -418,22 +372,38 @@ class MPClient(QtCore.QObject): def __check_command_ok(self, cmd): if not self._client: return self.logger.info('Not connected.') - if not cmd in self._commands: + if not cmd in self.commands: return self.logger.error('Command %s not accessible'%cmd) return True - def __update_outputs(self): - """Update the list of MPD audio outputs.""" + def __update_static(self): + """Update static values, called on connect/disconnect.""" + if self._client: + self.commands = list(self._client.commands()) + else: + self.commands = [] + if self.__check_command_ok('outputs'): outputs = [] for output in self._client.outputs(): - outputs.append(MPClient.Output(self, output['outputname'], output['outputid'], - bool(output['outputenabled']))) + outputs.append(AudioOutput(self, output['outputname'], output['outputid'], + bool(output['outputenabled']))) self.outputs = outputs else: self.outputs = [] + + if self.__check_command_ok('tagtypes'): + self.tagtypes = map(str.lower, self._client.tagtypes()) + ['file'] + else: + self.tagtypes = [] + + if self.__check_command_ok('urlhandlers'): + self.urlhandlers = list(self._client.urlhandlers()) + else: + self.urlhandlers = [] + def set_output(self, output_id, state): - """Set audio output output_id to state (0/1).""" + """Set audio output output_id to state (0/1). Called only by AudioOutput.""" if not self.__check_command_ok('enableoutput'): return if state: @@ -492,3 +462,33 @@ class MPClient(QtCore.QObject): if int(outputs[i]['outputenabled']) != int(self.outputs[i].state): self.outputs[i].mpd_toggle_state() + +class AudioOutput(QtCore.QObject): + """This class represents an MPD audio output.""" + # public, const + mpclient = None + name = None + id = None + state = None + + # SIGNALS + state_changed = QtCore.pyqtSignal(bool) + + #### public #### + def __init__(self, mpclient, name, id, state): + QtCore.QObject.__init__(self) + + self.mpclient = mpclient + self.name = name + self.id = id + self.state = state + + @QtCore.pyqtSlot(bool) + def set_state(self, state): + self.mpclient.set_output(self.id, state) + + #### private #### + def mpd_toggle_state(self): + """This is called by mpclient to inform about output state change.""" + self.state = not self.state + self.state_changed.emit(self.state) diff --git a/nephilim/plugins/Filebrowser.py b/nephilim/plugins/Filebrowser.py index 983b8a2..7740afe 100644 --- a/nephilim/plugins/Filebrowser.py +++ b/nephilim/plugins/Filebrowser.py @@ -129,7 +129,7 @@ class wgFilebrowser(QtGui.QWidget): self.view.setRootIndex(index) self.path.setText(self.model.filePath(index)) else: - if not 'file://' in self.plugin.mpclient.urlhandlers(): + if not 'file://' in self.plugin.mpclient.urlhandlers: self.logger.error('file:// handler not available. Connect via unix domain sockets.') return paths = [] diff --git a/nephilim/plugins/Library.py b/nephilim/plugins/Library.py index edc59d6..348069c 100644 --- a/nephilim/plugins/Library.py +++ b/nephilim/plugins/Library.py @@ -51,7 +51,7 @@ class Library(Plugin): self.settings.beginGroup(self.plugin.name) tags_enabled = self.settings.value('grouping').toStringList() - tags = self.plugin.mpclient.tagtypes() + tags = self.plugin.mpclient.tagtypes self.taglist = QtGui.QListWidget(self) self.taglist.setDragDropMode(QtGui.QAbstractItemView.InternalMove) for tag in [tag for tag in tags_enabled if tag in tags]: diff --git a/nephilim/plugins/Songinfo.py b/nephilim/plugins/Songinfo.py index 0701ddb..7214c41 100644 --- a/nephilim/plugins/Songinfo.py +++ b/nephilim/plugins/Songinfo.py @@ -42,7 +42,7 @@ class Songinfo(Plugin): self.settings.beginGroup(self.plugin.name) tags_enabled = self.settings.value('tagtypes').toStringList() - tags = self.plugin.mpclient.tagtypes() + tags = self.plugin.mpclient.tagtypes self.taglist = QtGui.QListWidget(self) self.taglist.setDragDropMode(QtGui.QAbstractItemView.InternalMove) for tag in [tag for tag in tags_enabled if tag in tags]: @@ -101,7 +101,7 @@ class Songinfo(Plugin): return self.SettingsWidgetSonginfo(self) def update_tagtypes(self): - self.__tags = [tag for tag in self.settings.value(self.name + '/tagtypes').toStringList() if tag in self.mpclient.tagtypes()] + self.__tags = [tag for tag in self.settings.value(self.name + '/tagtypes').toStringList() if tag in self.mpclient.tagtypes] self.o.set_tagtypes(self.__tags) def refresh(self): self.logger.info('Refreshing.') -- cgit v1.2.3