]> kaliko git repositories - mpd-goodies.git/blob - src/lib/mpdutils.py
* reorganized source.
[mpd-goodies.git] / src / lib / mpdutils.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 Efrim <efrim@azylum.org> {{{
4 #
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.
9 #
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.
14 #
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/>.
17 #
18 #  }}}
19
20 import sys
21
22 from os import environ
23 from socket import error as SocketError
24
25 try:
26     from mpd import (MPDClient, CommandError)
27 except ImportError, err:
28     print 'ERROR: "%s"\n\nPlease install python-mpd module.\n' % err
29     sys.exit(1)
30
31
32 def get_mpd_environ():#{{{
33     """
34     Retrieve MPD env. var.
35     """
36     con_id = dict({'host': 'localhost',
37                    'port': int(6600)})
38     passwd = None
39     mpd_host_env = environ.get('MPD_HOST')
40     if mpd_host_env:
41         # If password is set:
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]
49
50     con_id.update({'port': int(environ.get('MPD_PORT', con_id.get('port')))})
51     return (con_id, passwd)#}}}
52
53
54 def mpdConnect(client, con_id):#{{{
55     """
56     Simple wrapper to connect MPD.
57     """
58     try:
59         client.connect(**con_id)
60     except SocketError:
61         return False
62     return True#}}}
63
64
65 def mpdAuth(client, secret):#{{{
66     """
67     Authenticate
68     """
69     try:
70         client.password(secret)
71     except CommandError:
72         return False
73     return True#}}}
74
75
76 def mconnect(host=None, port=None):#{{{
77     """"""
78     ## get connection id from ENV VAR
79     con_id, passwd = get_mpd_environ()
80     if host:
81         con_id.update({'host': host})
82     if port:
83         con_id.update({'port': port})
84     ## MPD object instance
85     client = MPDClient()
86     if mpdConnect(client, con_id):
87         #print 'Got connected!'
88         True
89     else:
90         print 'ERROR: fail to connect MPD server.'
91         sys.exit(1)
92
93     ## Auth if password is set non False
94     if passwd:
95         if mpdAuth(client, passwd):
96             #print 'Pass auth!'
97             True
98         else:
99             print 'ERROR: fail trying to pass auth. Check password?'
100             client.disconnect()
101             sys.exit(1)
102     return client#}}}
103
104 def collapse_tags(value):
105     if isinstance(value, list):
106         #self.__dict__[tag] = ", ".join(set(value))
107         self.collapse_tags_bool = True
108         return ', '.join(set(value))
109     return value#}}}
110
111 # Script starts here
112 if __name__ == '__main__':
113     cli = mconnect()
114     cli.disconnect()
115
116 # VIM MODLINE
117 # vim: ai ts=4 sw=4 sts=4 expandtab
118