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

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

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

File size: 2.0 KB
RevLine 
[11]1#!/usr/bin/env python
[16]2# -*- coding: utf-8 -*-
[11]3
[13]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
[27]20from os.path import sep, splitext
21from sre import compile
[31]22import sys
[32]23
[27]24state_map = {
[31]25    'play': u'Playing',
26    'pause': u'Paused',
27    'stop': u'Stopped'
[27]28    }
[32]29"""Mapping from the states we get from mpd to nicer display names."""
30
[27]31fields = ['artist', 'album', 'track', 'title']
[32]32"""The fields to display."""
[11]33
[32]34strip_paths = []
35"""List of path prefixes to remove from the filename for display."""
36
37
[31]38strip_expr = compile(u'(%s)/' % ur'|'.join(strip_paths))
[11]39
[28]40def get_status(connection):
[32]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    """
[28]48    songinfo = {}
49    songinfo['state'] = state_map[connection.status()['state']]
50    song = connection.currentsong()
51    songinfo['song'] = \
[31]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' - '))
[29]57    return songinfo
[28]58
59if __name__ == '__main__':
[31]60    import locale, codecs
[28]61    from mpdclient2 import connect
[31]62    enc = locale.getpreferredencoding()
63    sys.stdout = codecs.getwriter(enc)(sys.__stdout__)
64    format = u'♫ %(state)s: %(song)s'
[29]65    print format % get_status(connect())
Note: See TracBrowser for help on using the repository browser.