Permalink
Browse files

Cleaner interface to the MPD server

There's still a lot of things to do:

* remove the explicit call() method and implements __getattribute__
  instead to proxy the calls to the underlying mpd.MPDClient() object

* rename the object MPDH into something nicer

* implement a nice object to remove the crufty calls to mpdh.get():
  currsong() should return some more high level than now, which knows
  how to handle song information (adapter around the result of
  mpd.currentsong())

* try to remove the prevsonginfo/prevstatus/prevcon from main.py
  • Loading branch information...
1 parent 315a16b commit 3f0c18dcdecf910e55b6c6d8306e8869ffda2401 @multani committed Oct 15, 2011
Showing with 196 additions and 197 deletions.
  1. +19 −16 sonata/cli.py
  2. +26 −26 sonata/current.py
  3. +13 −13 sonata/library.py
  4. +49 −49 sonata/main.py
  5. +77 −81 sonata/mpdhelper.py
  6. +12 −12 sonata/playlists.py
View
@@ -90,7 +90,6 @@ def execute_cmds(self):
"""If arguments were passed, perform action on them."""
if self.cmds:
main = CliMain(self)
- mpdh.suppress_mpd_errors(True)
main.mpd_connect()
for cmd in self.cmds:
main.execute_cmd(cmd)
@@ -126,7 +125,10 @@ def __init__(self, args):
self.config.settings_load_real(library.library_set_data)
args.apply_profile_arg(self.config)
- self.client = mpd.MPDClient()
+ c = mpd.MPDClient()
+ self.MPDH = mpdh.MPDHelper(c)
+ # XXX Should be configurable from the outside
+ self.MPDH.suppress_error = True
def mpd_connect(self):
host, port, password = misc.mpd_env_vars()
@@ -137,38 +139,38 @@ def mpd_connect(self):
if not password:
password = self.config.password[self.config.profile_num]
- mpdh.call(self.client, 'connect', host, port)
+ self.MPDH.call('connect', host, port)
if password:
- mpdh.call(self.client, 'password', password)
+ self.MPDH.call('password', password)
def execute_cmd(self, cmd):
- self.status = mpdh.status(self.client)
+ self.status = self.MPDH.status()
if not self.status:
print _(('Unable to connect to MPD.\nPlease check your Sonata '
'preferences or MPD_HOST/MPD_PORT environment variables.'))
sys.exit(1)
- self.songinfo = mpdh.currsong(self.client)
+ self.songinfo = self.MPDH.currsong()
getattr(self, "_execute_%s" % cmd)()
def _execute_play(self):
- mpdh.call(self.client, 'play')
+ self.MPDH.call('play')
def _execute_pause(self):
- mpdh.call(self.client, 'pause', 1)
+ self.MPDH.call('pause', 1)
def _execute_stop(self):
- mpdh.call(self.client, 'stop')
+ self.MPDH.call('stop')
def _execute_next(self):
- mpdh.call(self.client, 'next')
+ self.MPDH.call('next')
def _execute_prev(self):
- mpdh.call(self.client, 'previous')
+ self.MPDH.call('previous')
def _execute_bool(self, cmd):
"""Set the reverse the value of cmd"""
- mpdh.call(self.client, cmd, int(not int(self.status[cmd])))
+ self.MPDH.call(cmd, int(not int(self.status[cmd])))
def _execute_random(self):
self._execute_bool('random')
@@ -178,9 +180,9 @@ def _execute_repeat(self):
def _execute_pp(self):
if self.status['state'] in ['play']:
- mpdh.call(self.client, 'pause', 1)
+ self.MPDH.call('pause', 1)
elif self.status['state'] in ['pause', 'stop']:
- mpdh.call(self.client, 'play')
+ self.MPDH.call('play')
def _execute_info(self):
if self.status['state'] in ['play', 'pause']:
@@ -193,8 +195,9 @@ def _execute_info(self):
(_("File"), ('file',)),
]
for pretty, cmd in cmds:
- mpdh.conout("%s: %s" % (pretty,
- mpdh.get(self.songinfo, *cmd)))
+ # XXX we should know the encoding of the string instead...
+ print ("%s: %s" % (pretty, mpdh.get(self.songinfo, *cmd))
+ ).encode(locale.getpreferredencoding(), "replace")
at, _length = [int(c) for c in self.status['time'].split(':')]
at_time = misc.convert_time(at)
try:
View
@@ -32,11 +32,11 @@
class Current(object):
- def __init__(self, config, client, TAB_CURRENT, on_current_button_press,
+ def __init__(self, config, MPDH, TAB_CURRENT, on_current_button_press,
connected, sonata_loaded, songinfo, update_statusbar,
iterate_now, libsearchfilter_get_style, new_tab):
self.config = config
- self.client = client
+ self.MPDH = MPDH
self.on_current_button_press = on_current_button_press
self.connected = connected
self.sonata_loaded = sonata_loaded
@@ -236,10 +236,10 @@ def current_update(self, prevstatus_playlist, new_playlist_length):
self.current.set_model(None)
if prevstatus_playlist:
- changed_songs = mpdh.call(self.client, 'plchanges',
+ changed_songs = self.MPDH.call('plchanges',
prevstatus_playlist)
else:
- changed_songs = mpdh.call(self.client, 'plchanges', 0)
+ changed_songs = self.MPDH.call('plchanges', 0)
self.current_songs = []
newlen = int(new_playlist_length)
@@ -482,11 +482,11 @@ def sort(self, mode, column=None):
songs.sort(key=lambda x: x["sortby"])
pos = 0
- mpdh.call(self.client, 'command_list_ok_begin')
+ self.MPDH.call('command_list_ok_begin')
for item in songs:
- mpdh.call(self.client, 'moveid', item["id"], pos)
+ self.MPDH.call('moveid', item["id"], pos)
pos += 1
- mpdh.call(self.client, 'command_list_end')
+ self.MPDH.call('command_list_end')
self.iterate_now()
self.header_update_column_indicators()
@@ -520,12 +520,12 @@ def on_sort_reverse(self, _action):
gtk.main_iteration()
top = 0
bot = len(self.currentdata)-1
- mpdh.call(self.client, 'command_list_ok_begin')
+ self.MPDH.call('command_list_ok_begin')
while top < bot:
- mpdh.call(self.client, 'swap', top, bot)
+ self.MPDH.call('swap', top, bot)
top = top + 1
bot = bot - 1
- mpdh.call(self.client, 'command_list_end')
+ self.MPDH.call('command_list_end')
self.iterate_now()
def on_dnd(self, treeview, drag_context, x, y, selection, _info,
@@ -554,12 +554,12 @@ def on_dnd(self, treeview, drag_context, x, y, selection, _info,
paths[i] = paths[i][len(musicdir):]
if len(paths[i]) == 0:
paths[i] = "/"
- listallinfo = mpdh.call(self.client, 'listallinfo',
+ listallinfo = self.MPDH.call('listallinfo',
paths[i])
for item in listallinfo:
if 'file' in item:
mpdpaths.append(mpdh.get(item, 'file'))
- elif mpdh.mpd_major_version(self.client) >= 0.14:
+ elif self.MPDH.version >= (0, 14):
# Add local file, available in mpd 0.14. This currently
# work because python-mpd does not support unix socket
# paths, won't which is needed for authentication for
@@ -583,7 +583,7 @@ def on_dnd(self, treeview, drag_context, x, y, selection, _info,
else:
songid = len(self.currentdata)
for mpdpath in mpdpaths:
- mpdh.call(self.client, 'addid', mpdpath, songid)
+ self.MPDH.call('addid', mpdpath, songid)
self.iterate_now()
return
@@ -606,7 +606,7 @@ def on_dnd(self, treeview, drag_context, x, y, selection, _info,
# We will manipulate self.current_songs and model to prevent
# the entire playlist from refreshing
offset = 0
- mpdh.call(self.client, 'command_list_ok_begin')
+ self.MPDH.call('command_list_ok_begin')
for source in drag_sources:
index, i, songid, text = source
if drop_info:
@@ -619,10 +619,10 @@ def on_dnd(self, treeview, drag_context, x, y, selection, _info,
self.current_songs.insert(dest, self.current_songs[index])
if dest < index + 1:
self.current_songs.pop(index + 1)
- mpdh.call(self.client, 'moveid', songid, dest)
+ self.MPDH.call('moveid', songid, dest)
else:
self.current_songs.pop(index)
- mpdh.call(self.client, 'moveid', songid, dest - 1)
+ self.MPDH.call('moveid', songid, dest - 1)
model.insert(dest, model[index])
moved_iters += [model.get_iter((dest,))]
model.remove(i)
@@ -631,17 +631,17 @@ def on_dnd(self, treeview, drag_context, x, y, selection, _info,
self.current_songs[index])
if dest < index:
self.current_songs.pop(index + 1)
- mpdh.call(self.client, 'moveid', songid, dest + 1)
+ self.MPDH.call('moveid', songid, dest + 1)
else:
self.current_songs.pop(index)
- mpdh.call(self.client, 'moveid', songid, dest)
+ self.MPDH.call('moveid', songid, dest)
model.insert(dest + 1, model[index])
moved_iters += [model.get_iter((dest + 1,))]
model.remove(i)
else:
#dest = int(self.status['playlistlength']) - 1
dest = len(self.currentdata) - 1
- mpdh.call(self.client, 'moveid', songid, dest)
+ self.MPDH.call('moveid', songid, dest)
self.current_songs.insert(dest + 1, self.current_songs[index])
self.current_songs.pop(index)
model.insert(dest + 1, model[index])
@@ -658,7 +658,7 @@ def on_dnd(self, treeview, drag_context, x, y, selection, _info,
# decreased by 1
if index < source[0] < dest:
source[0] -= 1
- mpdh.call(self.client, 'command_list_end')
+ self.MPDH.call('command_list_end')
# we are manipulating the model manually for speed, so...
self.current_update_skip = True
@@ -683,7 +683,7 @@ def on_current_click(self, _treeview, path, _column):
return
try:
i = model.get_iter(path)
- mpdh.call(self.client, 'playid', self.current_get_songid(i, model))
+ self.MPDH.call('playid', self.current_get_songid(i, model))
except:
pass
self.sel_rows = False
@@ -727,7 +727,7 @@ def searchfilter_on_enter(self, _entry):
song_id = self.current_get_songid(model.get_iter_first(), model)
if song_id:
self.searchfilter_toggle(None)
- mpdh.call(self.client, 'playid', song_id)
+ self.MPDH.call('playid', song_id)
def searchfilter_feed_loop(self, editable):
# Lets only trigger the searchfilter_loop if 200ms pass
@@ -927,7 +927,7 @@ def on_remove(self):
if len(selected) == len(self.currentdata) and \
not self.filterbox_visible:
# Everything is selected, clear:
- mpdh.call(self.client, 'clear')
+ self.MPDH.call('clear')
elif len(selected) > 0:
# we are manipulating the model manually for speed, so...
self.current_update_skip = True
@@ -936,18 +936,18 @@ def on_remove(self):
# If we remove an item from the filtered results, this
# causes a visual refresh in the interface.
self.current.set_model(None)
- mpdh.call(self.client, 'command_list_ok_begin')
+ self.MPDH.call('command_list_ok_begin')
for path in selected:
if not self.filterbox_visible:
rownum = path[0]
else:
rownum = self.filter_row_mapping[path[0]]
i = self.currentdata.get_iter((rownum, 0))
- mpdh.call(self.client, 'deleteid', self.current_get_songid(i,
+ self.MPDH.call('deleteid', self.current_get_songid(i,
self.currentdata))
# Prevents the entire playlist from refreshing:
self.current_songs.pop(rownum)
self.currentdata.remove(i)
- mpdh.call(self.client, 'command_list_end')
+ self.MPDH.call('command_list_end')
if not self.filterbox_visible:
self.current.set_model(model)
View
@@ -46,13 +46,13 @@ def library_get_data(data, *args):
class Library(object):
- def __init__(self, config, client, artwork, TAB_LIBRARY, album_filename,
+ def __init__(self, config, MPDH, artwork, TAB_LIBRARY, album_filename,
settings_save, filtering_entry_make_red,
filtering_entry_revert_color, filter_key_pressed,
on_add_item, connected, on_library_button_press, new_tab):
self.artwork = artwork
self.config = config
- self.client = client
+ self.MPDH = MPDH
self.librarymenu = None # cyclic dependency, set later
self.album_filename = album_filename
self.settings_save = settings_save
@@ -548,7 +548,7 @@ def library_populate_filesystem_data(self, path):
# Use cache if possible...
bd = self.lib_view_filesystem_cache
else:
- for item in mpdh.call(self.client, 'lsinfo', path):
+ for item in self.MPDH.call('lsinfo', path):
if 'directory' in item:
name = mpdh.get(item, 'directory').split('/')[-1]
data = self.library_set_data(path=mpdh.get(item,
@@ -625,7 +625,7 @@ def library_populate_toplevel_data(self, genreview=False, artistview=False,
elif albumview:
albums = []
untagged_found = False
- for item in mpdh.call(self.client, 'listallinfo', '/'):
+ for item in self.MPDH.call('listallinfo', '/'):
if 'file' in item and 'album' in item:
album = mpdh.get(item, 'album')
artist = mpdh.get(item, 'artist', self.NOTAG)
@@ -857,14 +857,14 @@ def library_return_list_items(self, itemtype, genre=None, artist=None,
for song in songs:
items.append(mpdh.get(song, itemtype))
else:
- items = mpdh.call(self.client, 'list', itemtype, *s)
+ items = self.MPDH.call('list', itemtype, *s)
for item in items:
if len(item) > 0:
results.append(item)
else:
if genre is None and artist is None and album is None and year \
is None:
- for item in mpdh.call(self.client, 'list', itemtype):
+ for item in self.MPDH.call('list', itemtype):
if len(item) > 0:
results.append(item)
if ignore_case:
@@ -885,7 +885,7 @@ def library_return_count(self, genre=None, artist=None, album=None,
num_songs = 0
for s in searches:
- if '' in s and mpdh.mpd_major_version(self.client) <= 0.13:
+ if '' in s and self.MPDH.version <= (0, 13):
# Can't return count for empty tags, use search instead:
@@ -895,7 +895,7 @@ def library_return_count(self, genre=None, artist=None, album=None,
else:
- count = mpdh.call(self.client, 'count', *s)
+ count = self.MPDH.call('count', *s)
playtime += mpdh.get(count, 'playtime', 0, True)
num_songs += mpdh.get(count, 'songs', 0, True)
@@ -996,7 +996,7 @@ def library_return_search_items(self, genre=None, artist=None, album=None,
num_songs = 0
results = []
- if '' in s and mpdh.mpd_major_version(self.client) <= 0.13:
+ if '' in s and self.MPDH.version <= (0, 13):
# Can't search for empty tags, search broader and
# filter instead:
@@ -1016,7 +1016,7 @@ def library_return_search_items(self, genre=None, artist=None, album=None,
if len(args_tuple) == 0:
return None, 0, 0
- items = mpdh.call(self.client, 'search', *args_tuple)
+ items = self.MPDH.call('search', *args_tuple)
if items is not None:
for item in items:
if strip_type is None or (strip_type is not None and not \
@@ -1263,7 +1263,7 @@ def get_path_child_filenames(self, return_root, selected_only=True):
def library_get_path_files_recursive(self, path):
results = []
- for item in mpdh.call(self.client, 'lsinfo', path):
+ for item in self.MPDH.call('lsinfo', path):
if 'directory' in item:
results = results + self.library_get_path_files_recursive(
mpdh.get(item, 'directory'))
@@ -1373,8 +1373,8 @@ def libsearchfilter_do_search(self, searchby, todo):
if not self.prevlibtodo_base in todo:
# Do library search based on first two letters:
self.prevlibtodo_base = todo[:2]
- self.prevlibtodo_base_results = mpdh.call(
- self.client, 'search', searchby, self.prevlibtodo_base)
+ self.prevlibtodo_base_results = self.MPDH.call('search', searchby,
+ self.prevlibtodo_base)
subsearch = False
else:
subsearch = True
Oops, something went wrong.

0 comments on commit 3f0c18d

Please sign in to comment.