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

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

Add encoding declaration

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