/fstmerge/examples/emesene/rev1286-1505/right-branch-1505/plugins_base/currentSong/CurrentSong.py

https://github.com/RoDaniel/featurehouse · Python · 150 lines · 142 code · 0 blank · 8 comment · 26 complexity · 261eed9da08ff513666b8d3adc4c9ba4 MD5 · raw file

  1. import os
  2. import gettext
  3. class CurrentSong(object):
  4. customConfig = {}
  5. def __init__( self ):
  6. self.playing = ''
  7. self.artist = ''
  8. self.title = ''
  9. self.album = ''
  10. self.filename = ''
  11. style = '%title - %artist (%album)'
  12. self.setStyle( style )
  13. self.dictCommand = {
  14. 'show': (self.cmd_Show,'Show playing song',True)
  15. }
  16. self._log = []
  17. self.status = 'unknown'
  18. def log(self, type, message):
  19. print "%s: %s" % (type, message)
  20. self._log.append((type, message))
  21. def getSongDict(self):
  22. songinfo = {}
  23. songinfo['artist'] = self.artist
  24. songinfo['title'] = self.title
  25. songinfo['album'] = self.album
  26. return songinfo
  27. def getCurrentSong( self ):
  28. '''return the formated current song'''
  29. return self.parseStyle()
  30. def parseStyle( self ):
  31. '''return a parsed style according to the value of the variables'''
  32. if self.title == '' and self.artist == '' and self.album == '':
  33. return ''
  34. else:
  35. return self.style.replace('%artist', self.artist)\
  36. .replace('%title', self.title).replace('%album', self.album)
  37. def check( self ):
  38. '''check if there was a change in the song'''
  39. return False
  40. def isPlaying( self ):
  41. '''check if the player is playing'''
  42. return False
  43. def isRunning( self ):
  44. '''check if the player is running'''
  45. return False
  46. def getStatus( self ):
  47. '''check if everything is OK to start the plugin
  48. return a tuple whith a boolean and a message
  49. if OK -> ( True , 'some message' )
  50. else -> ( False , 'error message' )'''
  51. return ( True, 'OK' )
  52. def setStyle( self, string ):
  53. '''set the style'''
  54. self.style = '\\0Music\\01\\0' + string.replace('%title', '{0}').replace('%artist', '{1}').replace('%album', '{2}') + '\\0%title\\0%artist\\0%album\\0\\0'
  55. def is_on_path(self, fname):
  56. for p in os.environ['PATH'].split(os.pathsep):
  57. if os.path.isfile(os.path.join(p, fname)):
  58. return True
  59. def start( self ):
  60. pass
  61. def stop( self ):
  62. pass
  63. def getCoverPath( self ):
  64. return None
  65. def updateConfig( self ):
  66. pass
  67. def cmd_Show( self , *args):
  68. if self.artist == '' and self.album == '' and self.title == '':
  69. return ( False , 'Not Playing' )
  70. cm = self.getCurrentSong()
  71. cm = cm[cm.find( '\\0Music\\01\\0')+12:]
  72. cmargs = cm.split('\\0')
  73. cm = cmargs[0]
  74. for args in range(1, len(cmargs)):
  75. cm = cm.replace( '{%s}' %str(args-1), cmargs[args])
  76. if cm == '':
  77. return ( False , 'Not Playing' )
  78. return ( True, cm )
  79. ROOT_NAME = 'org.freedesktop.DBus'
  80. ROOT_PATH = '/org/freedesktop/DBus'
  81. DBUS = False
  82. class DbusBase( CurrentSong ):
  83. def __init__( self, name = '', callback = None ):
  84. CurrentSong.__init__( self )
  85. global DBUS
  86. try:
  87. import dbus
  88. dbus_version = getattr(dbus, 'version', (0,0,0))
  89. if dbus_version >= (0,41,0) and dbus_version < (0,80,0):
  90. dbus.SessionBus()
  91. import dbus.glib
  92. elif dbus_version >= (0,80,0):
  93. from dbus.mainloop.glib import DBusGMainLoop
  94. DBusGMainLoop(set_as_default=True)
  95. dbus.SessionBus()
  96. else:
  97. self.log('error', 'python-dbus is too old!')
  98. raise
  99. except Exception, e:
  100. self.log('error', 'cant start dbus')
  101. DBUS = False
  102. else:
  103. DBUS = True
  104. if not DBUS:
  105. return
  106. self.module = dbus
  107. self.bus = dbus.SessionBus()
  108. self.root = self.bus.get_object( ROOT_NAME, ROOT_PATH )
  109. self.isNocWaiting = False
  110. if name and callback:
  111. self.reset( name, callback )
  112. def reset( self, name, callback ):
  113. self.log( 'info', 'reset player: ' + str(name) )
  114. self.status = 'not running'
  115. if self.isNameActive( name ):
  116. self.log( 'info', 'player running: ' + str(name) )
  117. self.status = 'running'
  118. callback()
  119. else:
  120. self.log( 'info', 'not running, listening NameOwnerChanged' )
  121. self.status = 'not running'
  122. def noc(changedName, *args):
  123. if str(changedName) == name and self.isNameActive(name):
  124. self.log( 'info', 'player running: ' + str(name) )
  125. self.status = 'running'
  126. callback()
  127. self.isNocWaiting = False
  128. self.isNocWaiting = True
  129. self.bus.add_signal_receiver( noc, 'NameOwnerChanged', \
  130. ROOT_NAME, None, ROOT_PATH )
  131. def setCurrentSongData( self ):
  132. self.artist = ''
  133. self.title = ''
  134. self.album = ''
  135. self.filename = ''
  136. def getStatus( self ):
  137. '''don't override this'''
  138. global DBUS
  139. if os.name != 'posix':
  140. return ( False, _( 'This plugin only works in posix systems' ) )
  141. if not DBUS:
  142. return ( False, _( 'D-Bus cannot be initialized' ) )
  143. return ( True, 'Ok' )
  144. def isPlaying( self ):
  145. return False
  146. def check( self ):
  147. return False
  148. def isNameActive( self, name ):
  149. '''a helper for your class, so don't override it'''
  150. return bool( self.root.NameHasOwner(name) )