Changeset 339

Show
Ignore:
Timestamp:
06/28/06 21:33:16 (3 years ago)
Author:
nicfit
Message:

Local character encoding fixes and a better replacement method

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/plugins/gajimstatus.py

    r334 r339  
    1919#  $Id$ 
    2020################################################################################ 
    21 import os, re, locale 
     21import os, locale 
    2222import threading 
    2323from commands import getstatusoutput 
     
    4040 
    4141UPDATE_STATUS_STATES = ['chat', 'online', 'dnd'] 
     42 
     43# Test whether the local encoding can support U+266B 
    4244NOW_PLAYING_HEADER = u'\n♫:' 
     45try: 
     46    s = NOW_PLAYING_HEADER.encode(locale.getpreferredencoding()) 
     47except: 
     48    # Fallback on the original header 
     49    NOW_PLAYING_HEADER = u'\nnp:' 
    4350NOW_PLAYING_DELIM = ' - ' 
    4451NOW_PLAYING_FOOTER = '\n' 
     
    6976 
    7077    def _update_status_map(self): 
    71         now_playing_regex = re.compile('%(header)s.*%(middle)s.*%(footer)s' % \ 
    72                                        {'header': NOW_PLAYING_HEADER, 
    73                                         'middle': NOW_PLAYING_DELIM, 
    74                                         'footer': NOW_PLAYING_FOOTER, 
    75                                        }) 
    7678 
    7779        for acct in self.status_map.keys(): 
     
    9597                continue 
    9698 
    97             # FIXME 
    98             # If current status message contains data we put there, it must 
    99             # be removed 
    100             (status_msg, subs) = now_playing_regex.subn('%(now_playing)s', 
    101                                                         status_msg) 
    102             if subs == 0: 
    103                 status_msg += '%(now_playing)s' 
     99            # Substitute a previous tune with the new.  This tries to preserve 
     100            # any additional status the user has set. 
     101            if status_msg.startswith(NOW_PLAYING_HEADER): 
     102                # Status starts with a tune we put there, so just replace 
     103                status_msg = '%(now_playing)s' 
     104            else: 
     105                # User has custom status... 
     106                curr_idx = status_msg.find(NOW_PLAYING_HEADER) 
     107                if curr_idx == -1: 
     108                    # Append 
     109                    status_msg += '%(now_playing)s' 
     110                else: 
     111                    # Replace 
     112                    status_msg = status_msg[:curr_idx] + '%(now_playing)s' 
    104113            self.log.debug('Updating status template for %s: %s' % (acct, 
    105114                                                                    status_msg)) 
    106             # FIXME 
    107             #self.status_map[acct] = (status, status_msg) 
    108             self.status_map[acct] = (status, '%(now_playing)s') 
     115            self.status_map[acct] = (status, status_msg) 
    109116 
    110117    def _get_now_playing_str(self, audio_src, status_template, state_str = ''): 
     
    114121           album = audio_src.meta_data.album 
    115122 
    116            # Test whether the local encoding can support U+266B 
    117            try: 
    118                s = NOW_PLAYING_HEADER.encode(locale.getpreferredencoding()) 
    119            except: 
    120                header = '\n' 
    121            else: 
    122                header = NOW_PLAYING_HEADER 
    123  
    124123           now_playing = NOW_PLAYING_FORMAT % \ 
    125                                  {'header': header
     124                                 {'header': NOW_PLAYING_HEADER
    126125                                  'title': title, 
    127126                                  'middle': NOW_PLAYING_DELIM, 
     
    170169        stat, output = getstatusoutput(command) 
    171170        self.log.debug('%s returned:\n%s' % (command, output)) 
    172         return (stat, output
     171        return (stat, unicode(output, locale.getpreferredencoding())
    173172 
    174173    def is_configurable(self):