Skip to content

Commit

Permalink
Changes on file mpdhelper.py: PEP8 compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
Pacodastre committed Aug 30, 2010
1 parent 73ecece commit fd95076
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions sonata/mpdhelper.py
@@ -1,24 +1,30 @@

import locale, sys, os
import locale
import sys
import os
from time import strftime
from misc import remove_list_duplicates

suppress_errors = False


def suppress_mpd_errors(val):
global suppress_errors
suppress_errors = val


def status(client):
result = call(client, 'status')
if result and 'state' in result:
return result
else:
return {}


def currsong(client):
return call(client, 'currentsong')


def get(mapping, key, alt='', *sanitize_args):
"""Get a value from a mpd song and sanitize appropriately.
Expand All @@ -35,6 +41,7 @@ def get(mapping, key, alt='', *sanitize_args):
value = value[0]
return _sanitize(value, *sanitize_args) if sanitize_args else value


def _sanitize(tag, return_int=False, str_padding=0):
# Sanitizes a mpd tag; used for numerical tags. Known forms
# for the mpd tag can be "4", "4/10", and "4,10".
Expand All @@ -46,26 +53,30 @@ def _sanitize(tag, return_int=False, str_padding=0):

return tag.zfill(str_padding)


def conout(s):
# A kind of 'print' which does not throw exceptions if the string
# to print cannot be converted to console encoding; instead it
# does a "readable" conversion
print s.encode(locale.getpreferredencoding(), "replace")


def call(mpdclient, mpd_cmd, *mpd_args):
try:
retval = getattr(mpdclient, mpd_cmd)(*mpd_args)
except:
if not mpd_cmd in ['disconnect', 'lsinfo', 'listplaylists']:
if not suppress_errors:
print strftime("%Y-%m-%d %H:%M:%S") + " " + str(sys.exc_info()[1])
print '%s %s' % (strftime("%Y-%m-%d %H:%M:%S"),
str(sys.exc_info()[1]))
if mpd_cmd in ['lsinfo', 'list']:
return []
else:
return None

return retval


def mpd_major_version(client):
try:
version = getattr(client, "mpd_version", 0.0)
Expand All @@ -74,9 +85,11 @@ def mpd_major_version(client):
except:
return 0.0


def mpd_is_updating(status):
return status and status.get('updating_db', 0)


def update(mpdclient, paths, status):
# mpd 0.14.x limits the number of paths that can be
# updated within a command_list at 32. If we have
Expand Down

0 comments on commit fd95076

Please sign in to comment.