Updated the soap method GetPlaylist, the name of PlayList return variable.
[zukebox] / zukebox_server / src / services / playlist / zb_playlist.py
1
2 import os
3 #from brisa.core.reactors import install_default_reactor
4 #reactor = install_default_reactor()
5
6 from brisa.core import log
7 from brisa.upnp.device import Service, ServiceController
8
9 pjoin = os.path.join
10
11 class PlayListOutBoundExcept(Exception):
12     def __init__(self, *args, **kwargs):
13         Exception.__init__(self, args, kwargs)
14
15
16 class PlayList(Service):
17     """Class PlayList
18     Introduction
19     ============
20     Implements a playlist for ZukeBox server as a service.
21     For now the playlist works with a range of 10 positions, but in the 
22     future that should change to range of undefined positions, thus
23     the playlist will be "sizeable".
24     """
25
26     srvc_type = "urn:schemas-upnp-org:service:PlayList:1"
27     srvc_name = "PlayList"
28
29     #list = [{'CurrentURI':'teste', 'CurrenteURIMetaData':'teste', 'FromName':'teste', 'ToName':'teste'},
30     #        {'CurrentURI':'teste', 'CurrenteURIMetaData':'teste', 'FromName':'teste', 'ToName':'teste'}]
31
32     def __init__(self, positions, xml_path):
33         scpd_path = pjoin(xml_path, "zukebox-playlist-scpd.xml")
34         log.info("PlayList service scpd_path = %s" % scpd_path)
35         Service.__init__(self, self.srvc_name, self.srvc_type, '', scpd_path)
36
37         self.positions = positions
38         self.list = []
39         self.current = None
40         self.prev = self.current
41         self.next = None
42         self.from_name = None
43         self.to_name = None
44         self.current_uri = None
45         self.current_uri_metadata = None
46         self.locked = 0
47         self.available = 1
48
49     def soap_IsLocked(self, *args, **kwargs):
50         if len(self.list) == self.positions:
51            self.locked = 1
52         return {"Locked": self.locked}
53
54     def soap_IsAvailable(self, *args, **kwargs):
55         if not len(self.list) < 10:
56             self.available = 0
57         return {"Available": self.available}
58
59     def soap_Append(self, *args, **kwargs):
60         """Put a object in the playlist
61         """
62         if not self.locked:
63             self.current_uri = kwargs["CurrentURI"]
64             self.current_uri_metadata = kwargs["CurrentURIMetaData"]
65             self.from_name = kwargs["FromName"]
66             self.to_name = kwargs["ToName"]
67             req = {"cur_uri": self.current_uri,
68                     "cur_uri_data": self.current_uri_metadata,
69                     "from_name": self.from_name,
70                     "to_name": self.to_name}
71             self.list.append(req)
72
73             return {}
74         else:
75             raise PlayListOutBoundExcept("Playlist is full")
76
77     def soap_Drop(self, *args, **kwargs):
78         """Pop the object at position passed by index
79         """
80         if self.available:
81             index = kwargs["Index"]
82             self.list.pop(index)
83             return {}
84         else:
85             raise PlayListOutBoundExcept("Playlist unavailable")
86
87     def soap_GetSizeOfPlayList(self, *args, **kwargs):
88         """Return the size of playlist"""
89         lenght = len(self.list)
90         return {"PlayListSize": lenght}
91
92     def soap_GetCurrentItem(self, *args, **kwargs):
93         """Get the current position from playlist"""
94         if self.available:
95             curr = self.list[self.current]
96             #for now get only uri
97             curr_uri = curr["cur_uri"]
98             return {"CurrentItem": curr_uri}
99
100     def soap_GetNextItem(self, *args, **kwargs):
101         """Get the next item position from playlist"""
102         if self.current == len(self.list) - 1:
103             self.next = self.current
104         else:
105             self.next = self.current + 1
106         self.current = self.next
107         next = self.list[self.next]
108         #for now get only next uri
109         next_uri = next["cur_uri"]
110         return {"NextItem": next_uri}
111
112     def soap_GetPreviousItem(self, *args, **kwargs):
113         """Get the previous item position from playlist"""
114         if self.current != 0:
115             self.prev = self.current - 1
116             self.current = self.prev
117         prev = self.list[self.prev]
118         #for now get only previous uri
119         prev_uri = prev["cur_uri"]
120         return {"PreviousItem": prev_uri}
121
122     def soap_GetPlayList(self, *args, **kwargs):
123         """Get the playlist"""
124         playlist = str(self.get_playlist())
125         return {"PlayList": playlist}
126
127
128     def clean_playlist(self):
129         if self.available:
130             self.list = []
131
132     def get_playlist(self):
133         return self.list