Review in the methods of playlist service
[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     def __init__(self, positions, xml_path):
30         scpd_path = pjoin(xml_path, "zukebox-playlist-scpd.xml")
31         log.info("PlayList service scpd_path = %s" % scpd_path)
32         Service.__init__(self, self.srvc_name, self.srvc_type, '', scpd_path)
33
34         self.positions = positions
35         self.list = []
36         self.current = 0
37         self.prev = self.current
38         self.next = None
39         self.from_name = None
40         self.to_name = None
41         self.current_uri = None
42         self.current_uri_metadata = None
43         self.locked = 0
44         self.available = 1
45
46     def soap_IsLocked(self, *args, **kwargs):
47         if len(self.list) == self.positions:
48            self.locked = 1
49         return {"Locked": self.locked}
50
51     def soap_IsAvailable(self, *args, **kwargs):
52         if not len(self.list) < 10:
53             self.available = 0
54         return {"Available": self.available}
55
56     def soap_Append(self, *args, **kwargs):
57         """Put a object in the playlist
58         """
59         if not self.locked:
60             self.current_uri = kwargs["CurrentURI"]
61             self.current_uri_metadata = kwargs["CurrentURIMetaData"]
62             self.from_name = kwargs["FromName"]
63             self.to_name = kwargs["ToName"]
64             req = {"cur_uri": self.current_uri,
65                     "cur_uri_data": self.current_uri_metadata,
66                     "from_name": self.from_name,
67                     "to_name": self.to_name}
68             self.list.append(req)
69
70             return {}
71         else:
72             raise PlayListOutBoundExcept("Playlist is full")
73
74     def soap_Drop(self, *args, **kwargs):
75         """Pop the object at position passed by index
76         """
77         if self.available:
78             index = kwargs["Index"]
79             self.list.pop(index)
80             return {}
81         else:
82             raise PlayListOutBoundExcept("Playlist unavailable")
83
84     def soap_GetSizeOfPlayList(self, *args, **kwargs):
85         """Return the size of playlist"""
86         lenght = len(self.list)
87         return {"PlayListSize": lenght}
88
89     def soap_GetCurrentItem(self, *args, **kwargs):
90         """Get the current position from playlist"""
91         if self.available:
92             curr = self.list[self.current]
93             #for now get only uri
94             curr_uri = curr["cur_uri"]
95             return {"CurrentItem": curr_uri}
96
97     def soap_GetNextItem(self, *args, **kwargs):
98         """Get the next item position from playlist"""
99         if self.current == len(self.list) - 1:
100             self.next = self.current
101         else:
102             self.next = self.current + 1
103         self.current = self.next
104         next = self.list[self.next]
105         #for now get only next uri
106         next_uri = next["cur_uri"]
107         return {"NextItem": next_uri}
108
109     def soap_GetPreviousItem(self, *args, **kwargs):
110         """Get the previous item position from playlist"""
111         if self.current != 0:
112             self.prev = self.current - 1
113             self.current = self.prev
114         prev = self.list[self.prev]
115         #for now get only previous uri
116         prev_uri = prev["cur_uri"]
117         return {"PreviousItem": prev_uri}
118
119     def soap_GetPlaylist(self, *args, **kwargs):
120         """Get the playlist"""
121         playlist = self.list.__str__()
122         return {"Playlist": playlist}
123
124
125     def clean_playlist(self):
126         if self.available:
127             self.list = []
128
129     def get_playlist(self):
130         return self.list