7e29d8a8ce17537966ff0c61772ab9e4ee933d16
[zukebox] / zukebox_server / src / 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_Append(self, *args, **kwargs):
62         """Put a object in the playlist
63         """
64         if not self.is_locked():
65             self.current_uri = kwargs["CurrentURI"]
66             self.current_uri_metadata = kwargs["CurrentURIMetaData"]
67             self.from_name = kwargs["FromName"]
68             self.to_name = kwargs["ToName"]
69             self.list.append(self.current_uri)
70
71             return {"Append": {}}
72         else:
73             raise PlayListOutBoundExcept()
74
75     def soap_Drop(self, *args, **kwargs):
76         """Pop the object at position passed by index
77         """
78         if self.is_availble():
79             index = kwargs["Index"]
80             self.list.pop(index)
81             return {"Drop": {}}
82         else:
83             raise PlayListOutBoundExcept()
84
85     def soap_GetSizeOfPlayList(self, *args, **kwargs):
86         """Return the size of playlist"""
87         lenght = len(self.list)
88         rt = {"PlayListSize": lenght}
89         return {"GetSizeOfPlayListResponse": rt}
90
91     def soap_GetCurrent(self, *args, **kwargs):
92         if self.is_availble():
93             rt = {"CurrentPosition": self.list[self.current]}
94             return {"GetCurrentResponse": rt}
95
96     def clean_playlist(self):
97         if self.is_availble():
98             self.list = []
99
100     def get_playlist(self):
101         return self.list