source: mpd-status/trunk/mpd-status.py @ 15

Last change on this file since 15 was 15, checked in by simon, 19 years ago

Variableise format and delimiter used to help parsing of mpc output.

File size: 1.7 KB
Line 
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
19from subprocess import Popen, PIPE
20from os.path import basename, splitext
21
22fieldnames = [
23    'artist', 'album', 'track', 'title', 'length', 'filename',
24    'status', 'pnum', 'time', 'percent'
25    ]
26delim = '::'
27format = delim.join([
28    "[%artist% - ]", "[%album% - ]", "[%track% - ]",
29    "[%title%]", "[%time%]", "[%file%]"
30    ])
31cmd = ['mpc', '--format', format]
32
33lines = Popen(cmd, stdout=PIPE).communicate()[0].split('\n')
34
35if 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)
50else:
51    outstr = '--Not playing--'
52
53print outstr + ' |'
54
Note: See TracBrowser for help on using the repository browser.