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