1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | # Copyright © 2005, Simon E. Ward |
---|
4 | # |
---|
5 | # This program is free software; you can redistribute it and/or |
---|
6 | # modify it under the terms of version 2 of the GNU General Public |
---|
7 | # License as published by the Free Software Foundation. |
---|
8 | # |
---|
9 | # This program is distributed in the hope that it will be useful, |
---|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
12 | # General Public License for more details. |
---|
13 | # |
---|
14 | # You should have received a copy of the GNU General Public License |
---|
15 | # along with this program; if not, write to the Free Software |
---|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
17 | # 02110-1301, USA |
---|
18 | |
---|
19 | from subprocess import Popen, PIPE |
---|
20 | from os.path import basename, splitext |
---|
21 | |
---|
22 | fieldnames = [ |
---|
23 | 'artist', 'album', 'track', 'title', 'length', 'filename', |
---|
24 | 'status', 'pnum', 'time', 'percent' |
---|
25 | ] |
---|
26 | delim = '::' |
---|
27 | format = delim.join([ |
---|
28 | "[%artist% - ]", "[%album% - ]", "[%track% - ]", |
---|
29 | "[%title%]", "[%time%]", "[%file%]" |
---|
30 | ]) |
---|
31 | cmd = ['mpc', '--format', format] |
---|
32 | |
---|
33 | lines = Popen(cmd, stdout=PIPE).communicate()[0].split('\n') |
---|
34 | |
---|
35 | if len(lines) >= 3: |
---|
36 | fields = dict(zip( |
---|
37 | fieldnames, |
---|
38 | map( |
---|
39 | lambda s: s.strip('\"'), |
---|
40 | lines[0].split(delim) + lines[1].split() |
---|
41 | ) |
---|
42 | )) |
---|
43 | status = fields['status'].strip('[]').capitalize() + ':' |
---|
44 | if fields['title']: |
---|
45 | title = '%(artist)s%(album)s%(track)s%(title)s' % fields |
---|
46 | else: |
---|
47 | title = splitext(basename(fields['filename']))[0] |
---|
48 | time = '%(time)s/%(length)s %(percent)s' % fields |
---|
49 | outstr = '%s %s [%s]' % (status, title, time) |
---|
50 | else: |
---|
51 | outstr = '--Not playing--' |
---|
52 | |
---|
53 | print outstr + ' |' |
---|
54 | |
---|