]> kaliko git repositories - mpd-goodies.git/blob - mtopls
* mfade: no longer stops playing when aborting after external volume changes
[mpd-goodies.git] / mtopls
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2009 Efrim <efrim@azylum.org> {{{
5 #
6 #   This program is free software: you can redistribute it and/or modify
7 #   it under the terms of the GNU General Public License as published by
8 #   the Free Software Foundation, either version 3 of the License, or
9 #   (at your option) any later version.
10 #
11 #   This program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU General Public License for more details.
15 #
16 #   You should have received a copy of the GNU General Public License
17 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 #  }}}
20
21 """
22 DOC:
23 """
24
25 import sys
26
27 from os import (access, F_OK, W_OK)
28 from os.path import (dirname, isfile, join, abspath)
29
30 from lib.mpdutils import mconnect
31
32 USAGE = """Usage:
33
34 mtopls [<path-to-playlist>] [--help | -h | help]
35
36     If no playlist is specifed a new one is created in /var/lib/mpd/playlist/
37     named after genre of current playlist.
38
39 """
40
41 class MtoPls(object):
42     """
43     """
44
45     def __init__(self):
46         """"""
47         self.pls_path = None
48         self.cli = mconnect()
49         self.current = self.cli.currentsong()
50         self.cli.disconnect()
51         self._consume_sopt()
52         self._create_playlist()
53         self._controls_perm()
54         self._run()
55
56     def _consume_sopt(self):
57         """"""
58         if len(sys.argv) > 2 or \
59                 len(sys.argv) == 2 and \
60                 sys.argv[1] in ['-h', '--help', 'help']:
61             sys.stdout.write(USAGE)
62             sys.exit(1)
63         if len(sys.argv) == 2:
64             self.pls_path = sys.argv[1]
65             if not dirname(self.pls_path):
66                 self.pls_path = abspath(sys.argv[1])
67             sys.stdout.write('Playlist set to "%s"\n' % self.pls_path)
68             return
69         if len(sys.argv) == 1:
70             self._set_playlist()
71             return
72         sys.stdout.write(USAGE)
73         sys.exit(1)
74
75     def _set_playlist(self):
76         """Set playlist is none is given"""
77
78         mpd_playlists = '/var/lib/mpd/playlists/'
79         if not access(mpd_playlists, F_OK):
80             sys.stderr.write('Error: No access to "%s"' % self.pls_path)
81             sys.exit(1)
82         genre = self.current.get('genre', None)
83         if not genre:
84             sys.stderr.write('Error: No genre set in %s' %
85                     self.current.get('file'))
86             sys.stdout.write('Please provide a playlist.')
87             sys.exit(1)
88         genre += '.m3u'
89         self.pls_path = join('/var/lib/mpd/playlists/', genre)
90
91     def _create_playlist(self):
92         if not isfile(self.pls_path):
93             # TODO: add M3U header
94             sys.stdout.write('Create new playlist: %s\n' % self.pls_path)
95             open(self.pls_path, 'a').close()
96
97     def _controls_perm(self):
98         if not access(dirname(self.pls_path), F_OK):
99             sys.stderr.write('Error: Not existing path: "%s"' % self.pls_path)
100             sys.exit(1)
101         if not access(self.pls_path, W_OK):
102             sys.stderr.write('Error: No write access to path: "%s"' % self.pls_path)
103             sys.exit(1)
104         if not isfile(self.pls_path):
105             sys.stderr.write('Error: Not a regular file: "%s"' % self.pls_path)
106             sys.exit(1)
107         return True
108
109     def _run(self):
110         """"""
111         # TODO: controls file is not already in playlist
112         fd = open(self.pls_path, 'a')
113         fd.write(self.current.get('file'))
114         fd.write('\n')
115         fd.close()
116
117 # Script starts here
118 if __name__ == '__main__':
119     MtoPls()
120
121 # VIM MODLINE
122 # vim: ai ts=4 sw=4 sts=4 expandtab