Package elisa :: Package plugins :: Package base :: Package playlists :: Module default_playlist
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.base.playlists.default_playlist

  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.base_components.playlist import Playlist 
 20  from elisa.core import common 
 21  from twisted.internet import defer 
 22  import threading, time 
 23   
24 -class DefaultPlaylist(Playlist):
25
26 - def __init__(self):
27 Playlist.__init__(self) 28 self._lock = threading.Lock() 29 self._interrupt = False
30
31 - def initialize(self):
36
37 - def get_model(self):
38 return self._model
39
40 - def loadmore(self, uri, model, start=0, end=None):
41 self.debug("loadmore %r from %r to %r", uri, start, end) 42 43 def done(r): 44 self.loading = False 45 seconds = time.time() - t0 46 self.debug("Loaded %d items in %s seconds", len(model),seconds) 47 return r
48 49 def ready_to_start(m): 50 d = self._walk(start, end, uri, model) 51 d.addCallback(lambda r: model) 52 return d
53 54 def got_media_type(media_type): 55 file_type = media_type['file_type'] 56 if file_type == "directory": 57 58 # skip first items 59 if start > 0: 60 dfr = self._walk(0, start, uri, []) 61 dfr.addCallback(ready_to_start) 62 else: 63 # update the model for items between start and end indexes 64 dfr = self._walk(start, end, uri, model) 65 dfr.addCallback(lambda r: model) 66 return dfr 67 elif file_type in self.media_types: 68 self.debug("Adding: %r", uri) 69 model.append(uri) 70 return model 71 72 if self.loading: 73 try: 74 self._lock.acquire() 75 self._interrupt = True 76 finally: 77 self._lock.release() 78 else: 79 self._interrupt = False 80 81 t0 = time.time() 82 self.loading = True 83 dfr_mtype = common.application.media_manager.get_media_type(uri) 84 dfr_mtype.addCallback(got_media_type) 85 dfr_mtype.addCallback(done) 86 return dfr_mtype 87 88 @defer.deferredGenerator
89 - def _walk(self, start, end, uri, model):
90 """ 91 This methods walks through an URI directory resource between 92 start and end, skips directory entries and update the given 93 model with file entries found. 94 """ 95 media_manager = common.application.media_manager 96 root = uri 97 current = uri 98 99 def got_media_type(media_type, location): 100 media_type = media_type['file_type'] 101 if media_type in self.media_types: 102 self.debug("Adding: %r", location) 103 model.append(location) 104 return location
105 106 def got_location(next_location): 107 if next_location: 108 d = media_manager.get_media_type(next_location) 109 d.addCallback(got_media_type, next_location) 110 return d 111 else: 112 return None 113 114 if end is not None: 115 # between start and end 116 while start < end: 117 if self._need_interrupt(): 118 break 119 dfr = media_manager.next_location(current, root=root) 120 dfr.addCallback(got_location) 121 wfd = defer.waitForDeferred(dfr) 122 yield wfd 123 current = wfd.getResult() 124 if not current: 125 break 126 else: 127 start += 1 128 else: 129 # until end 130 while current: 131 if self._need_interrupt(): 132 break 133 dfr = media_manager.next_location(current, root=root) 134 dfr.addCallback(got_location) 135 wfd = defer.waitForDeferred(dfr) 136 yield wfd 137 current = wfd.getResult() 138 if not current: 139 break 140 141 yield model 142
143 - def _need_interrupt(self):
144 try: 145 self._lock.acquire() 146 value = self._interrupt 147 if value: 148 self.log("need interrupt: %r", value) 149 finally: 150 self._lock.release() 151 152 return value
153
154 - def empty(self):
155 """ Clear playlist's 156 """ 157 try: 158 self._lock.acquire() 159 self._interrupt = True 160 finally: 161 self._lock.release() 162 self.info("Clearing playlist") 163 self._model[:]=[]
164 165
166 - def add_uri(self, uri, position=-1):
167 """ Adds an uri to the Playlist 168 169 @param uri: the uri to add. if uri is a list, it will be directly 170 added to the list model. 171 @type uri: L{elisa.core.media_uri.MediaUri} 172 @param position: position of the list to update, -1 means appending 173 to the end 174 @type position: int 175 """
176