Merge
[zukebox] / zukebox_server / src / services / playlist / zukebox_playlist.py
1
2 import os
3
4 from brisa.core import log
5 from brisa.upnp.device import Service, ServiceController
6
7 pjoin = os.path.join
8
9 class PlayListOutBoundExcept(Exception):
10     def __rep__(self):
11         return "Play List Out of Bounds!"
12
13
14
15 class PlayList(Service):
16     """Class PlayList
17     Introduction
18     ============
19     Implements a playlist for ZukeBox server.
20     """
21
22     service_type = "urn:schemas-upnp-org:service:PlayList:1"
23     service_name = "PlayList"
24
25     def __init__(self, positions=10, xml_path):
26         scpd_path = pjoin(xml_path, "zukebox-playlist-scpd.xml")
27         Service.__init__(self, service_name, service_type, '', scpd_path,
28                 PlayListControl(positions, service_type))
29
30     def get_playlist(self):
31         return self.control_controller.get_playlist()
32
33 class PlayListControl(ServiceController):
34
35     def __init__(self, positions, serv_type):
36         ServiceController.__init__(self, serv_type)
37         self.positions = positions
38         self.list = []
39         self.current = 0
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
47     def soap_IsLocked(self, *args, **kwargs):
48         locked = True
49         if not len(self.list) == self.positions:
50             locked = False
51         rt = {"Locked": locked}
52         return {"IsLockedResponse": rt}
53
54     def soap_IsAvailble(self, *args, **kwargs):
55         availble = False
56         if not len(self.list) == 0:
57             availble = True
58         rt = {"Availble": availble}
59         return {"IsAvailbleResponse": rt}
60
61     def soap_IsAvailble(self, *args, **kwargs):
62         availble = False
63         if not len(self.list) == 0:
64             availble = True
65         rt = {"Availble": availble}
66         return {"IsAvailbleResponse": rt}
67
68     def soap_Append(self, *args, **kwargs):
69         """Put a object in the playlist
70         """
71         if not self.is_locked():
72             self.current_uri = kwargs["CurrentURI"]
73             self.current_uri_metadata = kwargs["CurrentURIMetaData"]
74             self.from_name = kwargs["FromName"]
75             self.to_name = kwargs["ToName"]
76             self.list.append(self.current_uri)
77
78             return {"Append": {}}
79         else:
80             raise PlayListOutBoundExcept()
81
82     def soap_Drop(self, *args, **kwargs):
83         """Pop the object at position passed by index
84         """
85         if self.is_availble():
86             index = kwargs["Index"]
87             self.list.pop(index)
88             return {"Drop": {}}
89         else:
90             raise PlayListOutBoundExcept()
91
92     def soap_GetSizeOfPlayList(self, *args, **kwargs):
93         """Return the size of playlist"""
94         lenght = len(self.list)
95         rt = {"PlayListSize": lenght}
96         return {"GetSizeOfPlayListResponse": rt}
97
98     def soap_GetCurrent(self, *args, **kwargs):
99         if self.is_availble():
100             rt = {"CurrentPosition": self.list[self.current]}
101             return {"GetCurrentResponse": rt}
102
103     def clean_playlist(self):
104         if self.is_availble():
105             self.list = []
106
107     def get_playlist(self):
108         return self.list