Package elisa :: Package plugins :: Package bad :: Package rhythmbox :: Module rb_media
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.bad.rhythmbox.rb_media

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  __maintainer__ = 'Philippe Normand <philippe@fluendo.com>' 
 18   
 19  from elisa.core import component 
 20  from elisa.core import media_uri 
 21  from elisa.base_components.media_provider import MediaProvider 
 22  from elisa.core.bus import bus_message  
 23  from elisa.core.observers.dict import DictObservable 
 24   
 25  from elisa.core import common 
 26  from twisted.internet import defer, threads 
 27   
 28  from elisa.extern.coherence import et 
 29  import os 
 30  import urllib 
 31   
 32  DEFAULT_DB_PATH = os.path.join(os.path.expanduser('~'), 
 33                                 '.gnome2','rhythmbox','rhythmdb.xml') 
 34   
35 -class RbMedia(MediaProvider):
36 default_config = {'db_path': DEFAULT_DB_PATH} 37 config_doc = {'db_path': 'absolute path to rhythmdb.xml file'} 38
39 - def initialize(self):
40 xml_db = self.config.get('db_path', DEFAULT_DB_PATH) 41 if not os.path.exists(xml_db): 42 msg = "Rhythmbox db not found at: %r" % xml_db 43 raise component.InitializeFailure(self.name, msg) 44 45 uri = "rb:///" 46 action_type = bus_message.MediaLocation.ActionType.LOCATION_ADDED 47 msg = bus_message.ForeignApplication(action_type, "Rhythmbox", 48 'rb', uri, 49 media_types=['audio',], 50 theme_icon='rhythmbox' 51 ) 52 common.application.bus.send_message(msg) 53 54 threads.deferToThread(self._load_db, xml_db)
55
56 - def _load_db(self, xml_db):
57 self._tree = et.parse_xml(open(xml_db).read()) 58 self._entries = list(self._tree.findall(".//entry")) 59 self._artists = {}
60
61 - def _get_artists(self):
62 if self._artists == {}: 63 for entry in self._entries: 64 if entry.get('type') == 'song': 65 title = entry.find('title').text 66 artist_title = entry.find('artist').text 67 album_title = entry.find('album').text 68 location = urllib.unquote(entry.find('location').text) 69 mtime = entry.find('mtime').text 70 71 track_number = entry.find('track-number') 72 if track_number is not None: 73 track_number = int(track_number.text) 74 else: 75 track_number = 0 76 77 artist = self._artists.get(artist_title, {}) 78 if album_title not in artist: 79 artist[album_title] = {track_number: (title, location, 80 mtime)} 81 else: 82 artist[album_title][track_number] = (title, location, 83 mtime) 84 85 self._artists[artist_title] = artist 86 return self._artists
87
89 return { 'rb': 0 }
90
91 - def _blocking_get_media_type(self, uri):
92 mime_type = '' 93 if uri.path == '/': 94 file_type = 'directory' 95 else: 96 file_type = 'audio' 97 return {'file_type': file_type, 'mime_type': mime_type}
98
99 - def _blocking_is_directory(self, uri):
100 return uri.path == '/'
101
102 - def has_children_with_types(self, uri, media_types):
103 return threads.deferToThread(self._blocking_has_children_with_types, 104 uri, media_types)
105
106 - def _blocking_has_children_with_types(self, uri, media_types):
107 has_children = False 108 if 'directory' in media_types: 109 if uri.get_param('track', None) is None: 110 has_children = True 111 return has_children
112
113 - def get_direct_children(self, uri, l):
114 d = threads.deferToThread(self._blocking_get_direct_children, uri, l) 115 return d
116
117 - def get_real_uri(self, uri):
118 real_uri = media_uri.MediaUri(uri) 119 artist = uri.get_param('artist', None) 120 album = uri.get_param('album', None) 121 track = uri.get_param('track', None) 122 if None not in (artist, album, track): 123 albums = self._get_artists().get(artist,{}) 124 track = int(track) 125 if album in albums and track in albums[album]: 126 real_uri = media_uri.MediaUri(albums[album][track][1]) 127 return real_uri
128
129 - def _blocking_get_direct_children(self, uri, children):
130 self.debug("Retrieving children of: %s", uri) 131 artists = self._get_artists() 132 133 artist = uri.get_param('artist', None) 134 album = uri.get_param('album', None) 135 136 self.debug("URI artist and album are: %s - %s" % (artist,album)) 137 138 if artist != None and album != None: 139 # process songs of given album of the artist 140 for track_id, (track, location, mtime) in artists.get(artist).get(album).iteritems(): 141 metadata = DictObservable() 142 child_uri = media_uri.MediaUri('rb:///track') 143 child_uri.set_params({'artist': artist, 'album': album, 144 'track': track_id}) 145 child_uri.label = track 146 metadata['artist'] = artist 147 metadata['album'] = album 148 metadata['song'] = track 149 metadata['file_type'] = 'audio' 150 metadata['fs_mtime'] = mtime 151 self.debug("Appendind %r with metadata %s" % (child_uri, 152 metadata)) 153 children.append((child_uri, metadata)) 154 155 elif artist != None and album == None: 156 # list albums of given artist 157 for album_name, album in artists.get(artist,{}).iteritems(): 158 metadata = DictObservable() 159 child_uri = media_uri.MediaUri(uri.parent) 160 metadata['artist'] = artist 161 metadata['album'] = album_name 162 child_uri.set_params({'artist' : artist, 163 'album' : album_name}) 164 child_uri.label = album_name 165 self.debug("Appendind %r with metadata %s" % (child_uri, 166 metadata)) 167 children.append((child_uri, metadata)) 168 169 elif uri.get_param('list_artists'): 170 # list artists 171 for artist in artists.keys(): 172 metadata = DictObservable() 173 child_uri = media_uri.MediaUri(uri.parent) 174 child_uri.set_params({'artist': artist}) 175 child_uri.label = artist 176 metadata['artist'] = artist 177 self.debug("Appendind %r with metadata %s" % (child_uri, 178 metadata)) 179 children.append((child_uri,metadata)) 180 elif uri.get_param('list_albums'): 181 # list albums 182 for artist, albums in artists.iteritems(): 183 for album_name, album in albums.iteritems(): 184 metadata = DictObservable() 185 child_uri = media_uri.MediaUri(uri.parent) 186 metadata['artist'] = artist 187 metadata['album'] = album_name 188 child_uri.set_params({'artist' : artist, 189 'album' : album_name}) 190 child_uri.label = album_name 191 self.debug("Appendind %r with metadata %s" % (child_uri, 192 metadata)) 193 children.append((child_uri, metadata)) 194 else: 195 # build main menu 196 children = self._build_main_menu(children) 197 198 self.debug("Retrieved children: %r", children) 199 return children
200
201 - def _build_main_menu(self, children):
202 203 artists = media_uri.MediaUri('rb:///?list_artists=1') 204 artists.label = "By artist" 205 children.append((artists,{})) 206 207 albums = media_uri.MediaUri('rb:///?list_albums=1') 208 albums.label = "By album" 209 children.append((albums,{})) 210 211 return children
212 213 ## def _blocking_next_location(self, from_uri, root=None): 214 ## next_uri = None 215