]> kaliko git repositories - mpd-goodies.git/blob - lib/mpdclass.py
* start working on MPDClass and new crop command.
[mpd-goodies.git] / lib / mpdclass.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4
5 from socket import error as SocketError
6
7 try:
8     from mpd import (MPDClient, CommandError)
9 except ImportError, err:
10     print 'ERROR: "%s"\n\nPlease install python-mpd module.\n' % err
11     sys.exit(1)
12
13
14 def get_mpd_environ():#{{{
15     """
16     Retrieve MPD env. var.
17     """
18     con_id = dict({'host': 'localhost',
19                    'port': int(6600)})
20     passwd = None
21     mpd_host_env = environ.get('MPD_HOST')
22     if mpd_host_env:
23         # If password is set:
24         # mpd_host_env = ['pass', 'host'] because MPD_HOST=pass@host
25         mpd_host_env = mpd_host_env.split('@')
26         mpd_host_env.reverse()
27         con_id.update({'host': mpd_host_env[0]})
28         if len(mpd_host_env) > 1:
29             print 'passwd set in MPD_HOST'
30             passwd = mpd_host_env[1]
31
32     con_id.update({'port': int(environ.get('MPD_PORT', con_id.get('port')))})
33     return (con_id, passwd)#}}}
34
35
36 class MPDClass(object):
37     """Connect to MPD server
38     """
39
40     def __init__(self):
41         self.client = MPDClient()
42
43     def mpdConnect(self, con_id):#{{{
44         """
45         Simple wrapper to connect MPD.
46         """
47         try:
48             self.client.connect(**con_id)
49         except SocketError:
50             return False
51         return True#}}}
52
53     def mpdAuth(self, secret):#{{{
54         """ Authenticate"""
55         try:
56             self.client.password(secret)
57         except CommandError:
58             return False
59         return True#}}}
60
61     def connect(self):
62         """Connect & auth"""
63         pass
64
65 def main():
66     pass
67
68 # Script starts here
69 if __name__ == '__main__':
70     main()
71
72 # VIM MODLINE
73 # vim: ai ts=4 sw=4 sts=4 expandtab
74