1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2009 Efrim <efrim@azylum.org> {{{
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 from os import environ
23 from socket import error as SocketError
26 from mpd import (MPDClient, CommandError)
27 except ImportError, err:
28 print 'ERROR: "%s"\n\nPlease install python-mpd module.\n' % err
32 def get_mpd_environ():#{{{
34 Retrieve MPD env. var.
36 con_id = dict({'host': 'localhost',
39 mpd_host_env = environ.get('MPD_HOST')
42 # mpd_host_env = ['pass', 'host'] because MPD_HOST=pass@host
43 mpd_host_env = mpd_host_env.split('@')
44 mpd_host_env.reverse()
45 con_id.update({'host': mpd_host_env[0]})
46 if len(mpd_host_env) > 1:
47 print 'passwd set in MPD_HOST'
48 passwd = mpd_host_env[1]
50 con_id.update({'port': int(environ.get('MPD_PORT', con_id.get('port')))})
51 return (con_id, passwd)#}}}
54 def mpdConnect(client, con_id):#{{{
56 Simple wrapper to connect MPD.
59 client.connect(**con_id)
65 def mpdAuth(client, secret):#{{{
70 client.password(secret)
79 con_id, passwd = get_mpd_environ()
80 ## MPD object instance
82 if mpdConnect(client, con_id):
83 #print 'Got connected!'
86 print 'ERROR: fail to connect MPD server.'
89 ## Auth if password is set non False
91 if mpdAuth(client, passwd):
95 print 'ERROR: fail trying to pass auth. Check password?'
100 def collapse_tags(value):
101 if isinstance(value, list):
102 #self.__dict__[tag] = ", ".join(set(value))
103 self.collapse_tags_bool = True
104 return ', '.join(set(value))
108 if __name__ == '__main__':
113 # vim: ai ts=4 sw=4 sts=4 expandtab