source: mpd-status/trunk/mpdstatus.py @ 32

Last change on this file since 32 was 32, checked in by simon, 17 years ago

A doc-comment, shock! Remove my custom list of prefixes.

File size: 2.0 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 os.path import sep, splitext
21from sre import compile
22import sys
23
24state_map = {
25    'play': u'Playing',
26    'pause': u'Paused',
27    'stop': u'Stopped'
28    }
29"""Mapping from the states we get from mpd to nicer display names."""
30
31fields = ['artist', 'album', 'track', 'title']
32"""The fields to display."""
33
34strip_paths = []
35"""List of path prefixes to remove from the filename for display."""
36
37
38strip_expr = compile(u'(%s)/' % ur'|'.join(strip_paths))
39
40def get_status(connection):
41    """Get playing state and song information.
42   
43    @return: Play state and song information.
44    @rtype: C{dict}
45    @param connection: mpd connection to use.
46    @type connection: C{mpdclient2.mpd_connection}
47    """
48    songinfo = {}
49    songinfo['state'] = state_map[connection.status()['state']]
50    song = connection.currentsong()
51    songinfo['song'] = \
52        u' - '.join([unicode(song[f])
53        for f in fields
54            if song.has_key(f)]) \
55            or unicode(strip_expr.sub(u'',
56                    splitext(unicode(song['file']))[0]).replace(sep, u' - '))
57    return songinfo
58
59if __name__ == '__main__':
60    import locale, codecs
61    from mpdclient2 import connect
62    enc = locale.getpreferredencoding()
63    sys.stdout = codecs.getwriter(enc)(sys.__stdout__)
64    format = u'♫ %(state)s: %(song)s'
65    print format % get_status(connect())
Note: See TracBrowser for help on using the repository browser.