]> kaliko git repositories - mpd-goodies.git/blob - mtopls
* fixes some typo in mfade
[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     Add the current track to playlist.
37
38     The default playlist, if none specified, is named after the current track's
39     genre within /var/lib/mpd/playlists/ (creating it if not existing).
40
41     Obviously the script is meant to be executed on hosts where
42     /var/lib/mpd/playlists/ makes sense or where file entries in the playlist
43     make sense for you (cf. "man 5 mpd.conf" especially
44     save_absolute_paths_in_playlists option).
45
46 """
47
48 class MtoPls(object):
49     """
50     """
51
52     def __init__(self):
53         """"""
54         self.pls_path = None
55         self.cli = mconnect()
56         self.current = self.cli.currentsong()
57         self.cli.disconnect()
58         self._consume_sopt()
59         self._create_playlist()
60         self._controls_perm()
61         self._run()
62
63     def _consume_sopt(self):
64         """"""
65         if len(sys.argv) >= 2 and \
66                 sys.argv[1] in ['-h', '--help', 'help']:
67             sys.stdout.write(USAGE)
68             sys.exit(1)
69         if len(sys.argv) == 2:
70             self.pls_path = sys.argv[1]
71             if not dirname(self.pls_path):
72                 self.pls_path = abspath(sys.argv[1])
73             print >> sys.stdout, ('Playlist set to "%s"' % self.pls_path)
74             return
75         if len(sys.argv) == 1:
76             self._set_playlist()
77             return
78         sys.stdout.write(USAGE)
79         sys.exit(1)
80
81     def _set_playlist(self):
82         """Set playlist is none is given"""
83
84         mpd_playlists = '/var/lib/mpd/playlists/'
85         if not access(mpd_playlists, F_OK):
86             sys.stderr.write('Error: No access to "%s"' % self.pls_path)
87             sys.exit(1)
88         genre = self.current.get('genre', None)
89         if not genre:
90             sys.stderr.write('Error: No genre set in %s\n' %
91                     self.current.get('file'))
92             print >> sys.stdout, ('Please provide a playlist.')
93             sys.exit(1)
94         genre += '.m3u'
95         self.pls_path = join('/var/lib/mpd/playlists/', genre)
96
97     def _create_playlist(self):
98         if not isfile(self.pls_path):
99             # TODO: add M3U header
100             print >> sys.stdout, ('Create new playlist: %s' % self.pls_path)
101             open(self.pls_path, 'a').close()
102
103     def _controls_perm(self):
104         if not access(dirname(self.pls_path), F_OK):
105             sys.stderr.write('Error: Not existing path: "%s"' % self.pls_path)
106             sys.exit(1)
107         if not access(self.pls_path, W_OK):
108             sys.stderr.write('Error: No write access to path: "%s"' % self.pls_path)
109             sys.exit(1)
110         if not isfile(self.pls_path):
111             sys.stderr.write('Error: Not a regular file: "%s"' % self.pls_path)
112             sys.exit(1)
113         return True
114
115     def _run(self):
116         """"""
117         # TODO: controls either file is already in playlist or not
118         print >> sys.stdout, ('Writing to %s' % self.pls_path)
119         fd = open(self.pls_path, 'a')
120         fd.write(self.current.get('file'))
121         fd.close()
122         pass
123
124 # Script starts here
125 if __name__ == '__main__':
126     MtoPls()
127
128 # VIM MODLINE
129 # vim: ai ts=4 sw=4 sts=4 expandtab