PluginSystem: implement new PluginDescriptor option "needsRestart" to define if a...
[enigma2.git] / lib / python / Components / Playlist.py
1 from ServiceReference import ServiceReference
2 from enigma import eServiceReference
3 import os
4
5 class PlaylistIO:
6         def __init__(self):
7                 self.list = []
8         
9         # returns a list of services or None if filename is not a valid playlist
10         def open(self, filename):
11                 return None
12         
13         OK = 0
14         FILEEXISTS = 1
15         WRITEERROR = 2
16         ERROR = 3
17         UNSUPPORTED_FILES_IN_PLAYLIST = 4
18         
19         REMOTE_PROTOS = ["http", "https", "udp", "rtsp", "rtp", "mmp"]
20         
21         def save(self, filename = None):
22                 return self.ERROR
23                 
24         def clear(self):
25                 del self.list[:]
26                 
27         def addService(self, service):
28                 self.list.append(service)
29
30         def getRef(self, filename, entry):
31                 if entry[0] == "/":
32                         path = entry
33                 else:
34                         path = os.path.dirname(filename) + "/" + entry
35                         for proto in self.REMOTE_PROTOS:
36                                 if entry.startswith(proto):
37                                         path = entry
38                 ref = eServiceReference(4097, 0, path)
39                 return ServiceReference(ref)
40
41 class PlaylistIOInternal(PlaylistIO):
42         def __init__(self):
43                 PlaylistIO.__init__(self)
44
45         def open(self, filename):
46                 self.clear()
47                 try:
48                         file = open(filename, "r")
49                 except IOError:
50                         return None
51                 while True:
52                         entry = file.readline().strip()
53                         if entry == "":
54                                 break
55                         self.addService(ServiceReference(entry))
56                 file.close()
57                 return self.list
58                 
59         def save(self, filename = None):
60                 print "Writing playlist into file", filename
61                 file = open(filename, "w")
62                 for x in self.list:
63                         file.write(str(x) + "\n")
64                 file.close()
65                 
66                 return self.OK
67         
68 class PlaylistIOM3U(PlaylistIO):
69         def __init__(self):
70                 PlaylistIO.__init__(self)
71         
72         def open(self, filename):
73                 self.clear()
74                 self.displayname = None
75                 try:
76                         file = open(filename, "r")
77                 except IOError:
78                         return None
79                 while True:
80                         entry = file.readline().strip()
81                         if entry == "":
82                                 break
83                         if entry.startswith("#EXTINF:"):
84                                 extinf = entry.split(',',1)
85                                 if len(extinf) > 1:
86                                         self.displayname = extinf[1]
87                                 # TODO: use e2 facilities to create a service ref from file
88                         elif entry[0] != "#":
89                                 sref = PlaylistIO.getRef(self, filename, entry)
90                                 if self.displayname:
91                                         sref.ref.setName(self.displayname)
92                                         self.displayname = None
93                                 self.addService(sref)
94                 file.close()
95                 return self.list
96                 
97         def save(self, filename = None):
98                 return self.ERROR
99         
100 class PlaylistIOPLS(PlaylistIO):
101         def __init__(self):
102                 PlaylistIO.__init__(self)
103         
104         def open(self, filename):
105                 self.clear()
106                 try:
107                         file = open(filename, "r")
108                 except IOError:
109                         return None
110                 entry = file.readline().strip()
111                 if entry == "[playlist]": # extended pls
112                         while True:
113                                 entry = file.readline().strip()
114                                 if entry == "":
115                                         break
116                                 if entry[0:4] == "File":
117                                         pos = entry.find('=') + 1
118                                         newentry = entry[pos:]
119                                         sref = PlaylistIO.getRef(self, filename, newentry)
120                                         self.addService(sref)
121                 else:
122                         playlist = PlaylistIOM3U()
123                         return playlist.open(filename)
124                 file.close()
125                 return self.list
126                 
127         def save(self, filename = None):
128                 return self.ERROR