Changeset 219

Show
Ignore:
Timestamp:
07/24/2008 11:00:13 PM (6 months ago)
Author:
funkiedamouse
Message:

Added docstring comments and doctest invocation.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/plwm/mw_xmms2.py

    r211 r219  
    11# mw_xmms2.py -- display xmms2 track title in the mode window 
    22# 
    3 #       Copyright (C) 2008  David H. Bronke <whitelynx@gmail.com> 
     3#   Copyright (C) 2008  David H. Bronke <whitelynx@gmail.com> 
    44#   Based on mw_xmms.py, Copyright (C) 2004  Mark Tigges <mtigges@gmail.com> 
    55# 
    6 #       This program is free software; you can redistribute it and/or modify 
    7 #       it under the terms of the GNU General Public License as published by 
    8 #       the Free Software Foundation; either version 2 of the License, or 
    9 #       (at your option) any later version. 
    10 # 
    11 #       This program is distributed in the hope that it will be useful, 
    12 #       but WITHOUT ANY WARRANTY; without even the implied warranty of 
    13 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    14 #       GNU General Public License for more details. 
    15 # 
    16 #       You should have received a copy of the GNU General Public License 
    17 #       along with this program; if not, write to the Free Software 
    18 #       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
     6#   This program is free software; you can redistribute it and/or modify 
     7#   it under the terms of the GNU General Public License as published by 
     8#   the Free Software Foundation; either version 2 of the License, or 
     9#   (at your option) any later version. 
     10# 
     11#   This program is distributed in the hope that it will be useful, 
     12#   but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     14#   GNU General Public License for more details. 
     15# 
     16#   You should have received a copy of the GNU General Public License 
     17#   along with this program; if not, write to the Free Software 
     18#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    1919 
    2020"""mw_xmms2.py - Display xmms2 track title in the mode window. 
    2121 
    2222This requires xmms2's Python bindings, which live in the xmmsclient module. 
     23 
    2324""" 
    2425 
     
    2627 
    2728try: 
    28         import xmmsclient, os 
    29  
    30         XMMS2TimerEvent = event.new_event_type() 
    31         XMMS2ReconnectEvent = event.new_event_type() 
    32  
    33         class ModeWindowXMMS2: 
    34                 """WM mixin providing an XMMS2 status display in the ModeWindow. 
    35                 """ 
    36  
    37                 mw_xmms2_position = 0.3 
    38                 mw_xmms2_justification = modewindow.LEFT 
    39  
    40                 def __wm_init__(self): 
    41                         self.wm_xmms2_message = modewindow.Message(self.mw_xmms2_position, 
    42                                 self.mw_xmms2_justification) 
    43  
    44                         self.mw_xmms2_connection = xmmsclient.XMMS("mw_xmms2") 
    45                         self.mw_xmms2_connect () 
    46  
    47                         for s in self.screens: 
    48                                 s.modewindow_add_message(self.wm_xmms2_message) 
    49  
    50                         self.dispatch.add_handler(XMMS2TimerEvent, self.mw_xmms2_tick) 
    51                         self.dispatch.add_handler(XMMS2ReconnectEvent, self.mw_xmms2_reconnect) 
    52                         self.mw_xmms2_update() 
    53  
    54  
    55                 def mw_xmms2_connect(self): 
    56                         try: 
    57                                 self.mw_xmms2_connection.connect(os.getenv("XMMS_PATH")) 
    58                         except IOError, detail: 
    59                                 print "mw_xmms2: Connection failed:", detail 
    60  
    61  
    62                 def mw_xmms2_ms_to_time(self, ms): 
    63                         minutes = ms/1000.0/60 
    64                         seconds = (minutes-int(minutes))*60 
    65                         return '%d:%02d' % (minutes, seconds) 
    66  
    67  
    68                 def mw_xmms2_update(self): 
    69                         """Get the artist and title of the currently-playing track. 
    70                         """ 
    71  
    72                         if self.mw_xmms2_connection.get_fd() == -1: 
    73                                 self.mw_xmms2_connect() 
    74                         else: 
    75                                 result = self.mw_xmms2_connection.playback_current_id() 
    76                                 result.wait() 
    77                                 if result.iserror(): 
    78                                         print "mw_xmms2: playback current id returns error, %s" % result.get_error() 
    79  
    80                                         self.wm_xmms2_message.set_text("XMMS2 Error: %s" % result.get_error()) 
    81  
    82                                         # There was some problem, so we'll try again in 20 seconds. 
    83                                         self.events.add_timer(event.TimerEvent(XMMS2ReconnectEvent, after = 20)) 
    84  
    85                                 if result.value() == 0: 
    86                                         curstatus = "Stopped" 
    87  
    88                                 else: 
    89                                         result = self.mw_xmms2_connection.medialib_get_info(result.value()) 
    90                                         result.wait() 
    91                                         if result.iserror(): 
    92                                                 print "mw_xmms2: medialib get info returns error, %s" % result.get_error() 
    93  
    94                                                 self.wm_xmms2_message.set_text("XMMS2 Error: %s" % result.get_error()) 
    95  
    96                                                 # There was some problem, so we'll try again in 20 seconds. 
    97                                                 self.events.add_timer(event.TimerEvent(XMMS2ReconnectEvent, after = 20)) 
    98  
    99                                         minfo = result.value() 
    100                                         try: 
    101                                                 duration = self.mw_xmms2_ms_to_time(minfo["duration"]) 
    102                                         except KeyError: 
    103                                                 duration = "?:??" 
    104  
    105                                         try: 
    106                                                 artist = minfo["artist"] 
    107                                         except KeyError: 
    108                                                 artist = "No artist" 
    109  
    110                                         try: 
    111                                                 title = minfo["title"] 
    112                                         except KeyError: 
    113                                                 title = "No title" 
    114  
    115                                         if artist == "No artist" and title == "No title": 
    116                                                 try: 
    117                                                         stitle = minfo["file"] 
    118                                                 except KeyError: 
    119                                                         stitle = "No file" 
    120                                         else: 
    121                                                 stitle = "%s - %s" % (title, artist) 
    122  
    123  
    124                                         result = self.mw_xmms2_connection.playback_playtime() 
    125                                         result.wait() 
    126                                         if result.iserror(): 
    127                                                 print "mw_xmms2: playback playtime returns error, %s" % result.get_error() 
    128  
    129                                                 self.wm_xmms2_message.set_text("XMMS2 Error: %s" % result.get_error()) 
    130  
    131                                                 # There was some problem, so we'll try again in 20 seconds. 
    132                                                 self.events.add_timer(event.TimerEvent(XMMS2ReconnectEvent, after = 20)) 
    133  
    134                                         stime = result.value() 
    135                                         elapsed = self.mw_xmms2_ms_to_time(stime) 
    136  
    137  
    138                                         result = self.mw_xmms2_connection.playback_status() 
    139                                         result.wait() 
    140                                         if result.iserror(): 
    141                                                 print "mw_xmms2: playback status returns error, %s" % result.get_error() 
    142  
    143                                                 self.wm_xmms2_message.set_text("XMMS2 Error: %s" % result.get_error()) 
    144  
    145                                                 # There was some problem, so we'll try again in 20 seconds. 
    146                                                 self.events.add_timer(event.TimerEvent(XMMS2ReconnectEvent, after = 20)) 
    147  
    148                                         status = "Unknown" 
    149                                         statnum = result.value() 
    150                                         if statnum == xmmsclient.PLAYBACK_STATUS_STOP: 
    151                                                 status = "Stopped" 
    152                                         elif statnum == xmmsclient.PLAYBACK_STATUS_PLAY: 
    153                                                 status = "Playing" 
    154                                         elif statnum == xmmsclient.PLAYBACK_STATUS_PAUSE: 
    155                                                 status = "Paused" 
    156  
    157                                         # Format the message 
    158                                         if status == "Stopped": 
    159                                                 curstatus = '%s <%s>' % (status, stitle) 
    160                                         else: 
    161                                                 curstatus = '%s <%s> [%s/%s]' % (status, stitle, elapsed, duration) 
    162  
    163                                 self.events.add_timer(event.TimerEvent(XMMS2TimerEvent, after = 1)) 
    164  
    165                                 self.wm_xmms2_message.set_text(str(curstatus)) 
    166  
    167  
    168                 def mw_xmms2_reconnect(self, evt): 
    169                         self.mw_xmms2_connect() 
    170                         self.mw_xmms2_update() 
    171  
    172  
    173                 def mw_xmms2_tick(self, evt): 
    174                         self.mw_xmms2_update() 
     29    import xmmsclient, os 
     30 
     31    XMMS2TimerEvent = event.new_event_type() 
     32    XMMS2ReconnectEvent = event.new_event_type() 
     33 
     34    class ModeWindowXMMS2: 
     35        """WM mixin providing an XMMS2 status display in the ModeWindow. 
     36 
     37        Example: 
     38 
     39        >>> from plwm import mw_xmms2, wmanager, modewindow 
     40        >>> class MyScreen(wmanager.Screen, modewindow.ModeWindowScreen): 
     41        ...     pass 
     42        ...  
     43        >>> class MyWM(wmanager.WindowManager, mw_xmms2.ModeWindowXMMS2): 
     44        ...     screen_class = MyScreen 
     45        ...     mw_xmms2_position = 0.7 
     46        ...     mw_xmms2_justification = modewindow.RIGHT 
     47        ...  
     48 
     49        """ 
     50 
     51        mw_xmms2_position = 0.3 
     52        mw_xmms2_justification = modewindow.LEFT 
     53 
     54        def __wm_init__(self): 
     55            """Create a mode window message and connect to the XMMS2 server.""" 
     56            self.wm_xmms2_message = modewindow.Message(self.mw_xmms2_position, 
     57                self.mw_xmms2_justification) 
     58 
     59            self.mw_xmms2_connection = xmmsclient.XMMS("mw_xmms2") 
     60            self.mw_xmms2_connect () 
     61 
     62            for s in self.screens: 
     63                s.modewindow_add_message(self.wm_xmms2_message) 
     64 
     65            self.dispatch.add_handler(XMMS2TimerEvent, self.mw_xmms2_tick) 
     66            self.dispatch.add_handler(XMMS2ReconnectEvent, self.mw_xmms2_reconnect) 
     67            self.mw_xmms2_update() 
     68 
     69 
     70        def mw_xmms2_connect(self): 
     71            """Connect to the XMMS2 server.""" 
     72            try: 
     73                self.mw_xmms2_connection.connect(os.getenv("XMMS_PATH")) 
     74            except IOError, detail: 
     75                print "mw_xmms2: Connection failed:", detail 
     76 
     77 
     78        def mw_xmms2_ms_to_time(self, ms): 
     79            """Convert from milliseconds to a human-readable time format.""" 
     80            minutes = ms/1000.0/60 
     81            seconds = (minutes-int(minutes))*60 
     82            return '%d:%02d' % (minutes, seconds) 
     83 
     84 
     85        def mw_xmms2_update(self): 
     86            """Update the mode window message. 
     87             
     88            Query the artist and title of the currently-playing tracko and set 
     89            the message accordingly. 
     90 
     91            """ 
     92            if self.mw_xmms2_connection.get_fd() == -1: 
     93                self.mw_xmms2_connect() 
     94            else: 
     95                result = self.mw_xmms2_connection.playback_current_id() 
     96                result.wait() 
     97                if result.iserror(): 
     98                    print "mw_xmms2: playback current id returns error, %s" % result.get_error() 
     99 
     100                    self.wm_xmms2_message.set_text("XMMS2 Error: %s" % result.get_error()) 
     101 
     102                    # There was some problem, so we'll try again in 20 seconds. 
     103                    self.events.add_timer(event.TimerEvent(XMMS2ReconnectEvent, after = 20)) 
     104 
     105                if result.value() == 0: 
     106                    curstatus = "Stopped" 
     107 
     108                else: 
     109                    result = self.mw_xmms2_connection.medialib_get_info(result.value()) 
     110                    result.wait() 
     111                    if result.iserror(): 
     112                        print "mw_xmms2: medialib get info returns error, %s" % result.get_error() 
     113 
     114                        self.wm_xmms2_message.set_text("XMMS2 Error: %s" % result.get_error()) 
     115 
     116                        # There was some problem, so we'll try again in 20 seconds. 
     117                        self.events.add_timer(event.TimerEvent(XMMS2ReconnectEvent, after = 20)) 
     118 
     119                    minfo = result.value() 
     120                    try: 
     121                        duration = self.mw_xmms2_ms_to_time(minfo["duration"]) 
     122                    except KeyError: 
     123                        duration = "?:??" 
     124 
     125                    try: 
     126                        artist = minfo["artist"] 
     127                    except KeyError: 
     128                        artist = "No artist" 
     129 
     130                    try: 
     131                        title = minfo["title"] 
     132                    except KeyError: 
     133                        title = "No title" 
     134 
     135                    if artist == "No artist" and title == "No title": 
     136                        try: 
     137                            stitle = minfo["file"] 
     138                        except KeyError: 
     139                            stitle = "No file" 
     140                    else: 
     141                        stitle = "%s - %s" % (title, artist) 
     142 
     143 
     144                    result = self.mw_xmms2_connection.playback_playtime() 
     145                    result.wait() 
     146                    if result.iserror(): 
     147                        print "mw_xmms2: playback playtime returns error, %s" % result.get_error() 
     148 
     149                        self.wm_xmms2_message.set_text("XMMS2 Error: %s" % result.get_error()) 
     150 
     151                        # There was some problem, so we'll try again in 20 seconds. 
     152                        self.events.add_timer(event.TimerEvent(XMMS2ReconnectEvent, after = 20)) 
     153 
     154                    stime = result.value() 
     155                    elapsed = self.mw_xmms2_ms_to_time(stime) 
     156 
     157 
     158                    result = self.mw_xmms2_connection.playback_status() 
     159                    result.wait() 
     160                    if result.iserror(): 
     161                        print "mw_xmms2: playback status returns error, %s" % result.get_error() 
     162 
     163                        self.wm_xmms2_message.set_text("XMMS2 Error: %s" % result.get_error()) 
     164 
     165                        # There was some problem, so we'll try again in 20 seconds. 
     166                        self.events.add_timer(event.TimerEvent(XMMS2ReconnectEvent, after = 20)) 
     167 
     168                    status = "Unknown" 
     169                    statnum = result.value() 
     170                    if statnum == xmmsclient.PLAYBACK_STATUS_STOP: 
     171                        status = "Stopped" 
     172                    elif statnum == xmmsclient.PLAYBACK_STATUS_PLAY: 
     173                        status = "Playing" 
     174                    elif statnum == xmmsclient.PLAYBACK_STATUS_PAUSE: 
     175                        status = "Paused" 
     176 
     177                    # Format the message 
     178                    if status == "Stopped": 
     179                        curstatus = '%s <%s>' % (status, stitle) 
     180                    else: 
     181                        curstatus = '%s <%s> [%s/%s]' % (status, stitle, elapsed, duration) 
     182 
     183                self.events.add_timer(event.TimerEvent(XMMS2TimerEvent, after = 1)) 
     184 
     185                self.wm_xmms2_message.set_text(str(curstatus)) 
     186 
     187 
     188        def mw_xmms2_reconnect(self, evt): 
     189            """Reconnect to the XMMS2 server.""" 
     190            self.mw_xmms2_connect() 
     191            self.mw_xmms2_update() 
     192 
     193 
     194        def mw_xmms2_tick(self, evt): 
     195            self.mw_xmms2_update() 
    175196 
    176197 
    177198except: 
    178199 
    179         import sys 
    180         sys.stderr.write('mw_xmms2: could not load xmmsclient python module\n') 
    181  
    182         class ModeWindowXMMS2: 
    183                 pass 
     200    import sys 
     201    sys.stderr.write('mw_xmms2: could not load xmmsclient python module\n') 
     202 
     203    class ModeWindowXMMS2: 
     204        """You do not have XMMS2's Python bindings installed. 
     205         
     206        The XMMS2 Python bindings are required for mw_xmms2. 
     207        """ 
     208        pass 
     209 
     210def _test(): 
     211    import doctest 
     212    doctest.testmod() 
     213 
     214if __name__ == "__main__": 
     215    _test() 
     216