File: CurrentSong.py

package info (click to toggle)
emesene 1.0-dist-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 4,596 kB
  • ctags: 3,006
  • sloc: python: 25,171; makefile: 14; sh: 1
file content (214 lines) | stat: -rw-r--r-- 6,779 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import gettext

class CurrentSong(object):

    customConfig = {}
    def __init__( self ):
        self.playing = ''
        self.artist = ''
        self.title = ''
        self.album = ''
        self.filename = ''
        
        style = '%title - %artist (%album)'
        
        self.setStyle( style )
        
        self.dictCommand = {
            'show': (self.cmd_Show,'Show playing song',True)
        }

        self._log = []
        self.status = 'unknown'

    def log(self, type, message):
        print "%s: %s" % (type, message)
        self._log.append((type, message))
    
    def getSongDict(self):
        songinfo = {}
        songinfo['artist'] = self.artist 
        songinfo['title'] = self.title
        songinfo['album'] = self.album
        return songinfo

    def getCurrentSong( self ):
        '''return the formated current song'''
        return self.parseStyle()
        
    def parseStyle( self ):
        '''return a parsed style according to the value of the variables'''
        if self.title == '' and self.artist == '' and self.album == '':
            return ''
        else:
            return self.style.replace('%artist', self.artist)\
                .replace('%title', self.title).replace('%album', self.album)
        
    def check( self ):
        '''check if there was a change in the song'''
        return False
        
    def isPlaying( self ):
        '''check if the player is playing'''
        return False

    def isRunning( self ):
        '''check if the player is running'''
        return False
        
    def getStatus( self ):
        '''check if everything is OK to start the plugin
        return a tuple whith a boolean and a message
        if OK -> ( True , 'some message' )
        else -> ( False , 'error message' )'''
        
        return ( True, 'OK' )
    
    def setStyle( self, string ):
        '''set the style'''
        self.style = '\\0Music\\01\\0' + string.replace('%title', '{0}').replace('%artist', '{1}').replace('%album', '{2}') + '\\0%title\\0%artist\\0%album\\0\\0'
        
    def is_on_path(self, fname):
        for p in os.environ['PATH'].split(os.pathsep):
            if os.path.isfile(os.path.join(p, fname)):
                return True
    
    # for plugins that need connecting/disconnecting signals
    def start( self ):
        pass
        
    def stop( self ):
        pass

    def getCoverPath( self ):
        return None
    
    def updateConfig( self ):
        pass
    
    def cmd_Show( self , *args):
        if self.artist == '' and self.album == '' and self.title == '':
            return ( False , 'Not Playing' )
        cm =  self.getCurrentSong()
        cm = cm[cm.find( '\\0Music\\01\\0')+12:]
        cmargs = cm.split('\\0')
        cm = cmargs[0]
        for args in range(1, len(cmargs)):
            cm = cm.replace( '{%s}' %str(args-1), cmargs[args])
        if cm == '':
            return ( False , 'Not Playing' )

        return ( True, cm )
    
ROOT_NAME = 'org.freedesktop.DBus'
ROOT_PATH = '/org/freedesktop/DBus'

DBUS = False

class DbusBase( CurrentSong ):
    
    def __init__( self, name = '', callback = None ):
        CurrentSong.__init__( self )
         
        global DBUS
        
        # http://listen-project.org/browser/trunk-0.6/src/dbus_manager.py#L29
        try:
            import dbus
            dbus_version = getattr(dbus, 'version', (0,0,0))
            if dbus_version >= (0,41,0) and dbus_version < (0,80,0):
                dbus.SessionBus()
                import dbus.glib
            elif dbus_version >= (0,80,0):
                from dbus.mainloop.glib import DBusGMainLoop
                DBusGMainLoop(set_as_default=True)
                dbus.SessionBus()
            else:
                self.log('error', 'python-dbus is too old!')
                raise
        except Exception, e:
            self.log('error', 'cant start dbus')
            DBUS = False
        else:
            DBUS = True
        
        if not DBUS:
            return
        self.module = dbus
        self.bus = dbus.SessionBus()
        self.root = self.bus.get_object( ROOT_NAME, ROOT_PATH )
        
        self.isNocWaiting = False
        if name and callback:
            self.reset( name, callback )
        
    def reset( self, name, callback ):
        self.log( 'info', 'reset player: ' + str(name) )
        self.status = 'not running'
        if self.isNameActive( name ):
            self.log( 'info', 'player running: ' + str(name) )
            self.status = 'running'
            callback()
        else:
            self.log( 'info', 'not running, listening NameOwnerChanged' )
            self.status = 'not running'
            def noc(changedName, *args):
                if str(changedName) == name and self.isNameActive(name):
                    self.log( 'info', 'player running: ' + str(name) )
                    self.status = 'running'
                    callback()
                    self.isNocWaiting = False
            # noc == name owner changed
            self.isNocWaiting = True
            self.bus.add_signal_receiver( noc, 'NameOwnerChanged', \
                                          ROOT_NAME, None, ROOT_PATH )
        
    def setCurrentSongData( self ):
        self.artist = ''
        self.title = ''
        self.album = ''
        self.filename = ''

    def getStatus( self ):
        '''don't override this'''
        
        global DBUS
        
        if os.name != 'posix':
            return ( False, _( 'This plugin only works in posix systems' ) )
        
        if not DBUS:
            return ( False, _( 'D-Bus cannot be initialized' ) )
        
        return ( True, 'Ok' )

    def isPlaying( self ):
        return False
        
    def check( self ):
        return False
    
    def isNameActive( self, name ):
        '''a helper for your class, so don't override it'''
        return bool( self.root.NameHasOwner(name) )