cut away 1/4th of enigma loading time by loading languages when they are used
[enigma2.git] / lib / python / Components / Playlist.py
1 from ServiceReference import ServiceReference
2 import os
3
4 class PlaylistIO:       
5         def __init__(self):
6                 self.list = []
7         
8         # returns a list of services or None if filename is not a valid playlist
9         def open(self, filename):
10                 return None
11         
12         OK = 0
13         FILEEXISTS = 1
14         WRITEERROR = 2
15         ERROR = 3
16         UNSUPPORTED_FILES_IN_PLAYLIST = 4
17         
18         def save(self, filename = None):
19                 return self.ERROR
20                 
21         def clear(self):
22                 del self.list[:]
23                 
24         def addService(self, service):
25                 self.list.append(service)
26                 
27                 
28 class PlaylistIOInternal(PlaylistIO):
29         def __init__(self):
30                 PlaylistIO.__init__(self)
31         
32         def open(self, filename):
33                 self.clear()
34                 try:
35                         file = open(filename, "r")
36                 except IOError:
37                         return None
38                 while True:
39                         entry = file.readline().strip()
40                         if entry == "":
41                                 break
42                         self.addService(ServiceReference(entry))
43                 file.close()
44                 return self.list
45                 
46         def save(self, filename = None):
47                 print "Writing playlist into file", filename
48                 file = open(filename, "w")
49                 for x in self.list:
50                         file.write(str(x) + "\n")
51                 file.close()
52                 
53                 return self.OK
54         
55 class PlaylistIOM3U(PlaylistIO):
56         def __init__(self):
57                 PlaylistIO.__init__(self)
58         
59         def open(self, filename):
60                 self.clear()
61                 try:
62                         file = open(filename, "r")
63                 except IOError:
64                         return None
65                 while True:
66                         entry = file.readline().strip()
67                         if entry == "":
68                                 break
69                         if entry[0] != "#":
70                                 # TODO: use e2 facilities to create a service ref from file
71                                 if entry[0] == "/":
72                                         self.addService(ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + entry))
73                                 else:
74                                         self.addService(ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + os.path.dirname(filename) + "/" + entry))
75                 file.close()
76                 return self.list
77                 
78         def save(self, filename = None):
79                 return self.ERROR
80         
81 class PlaylistIOPLS(PlaylistIO):
82         def __init__(self):
83                 PlaylistIO.__init__(self)
84         
85         def open(self, filename):
86                 self.clear()
87                 try:
88                         file = open(filename, "r")
89                 except IOError:
90                         return None
91                 entry = file.readline().strip()
92                 if entry == "[playlist]": # extended pls
93                         while True:
94                                 entry = file.readline().strip()
95                                 if entry == "":
96                                         break
97                                 if entry[0:4] == "File":
98                                         pos = entry.find('=') + 1
99                                         newentry = entry[pos:]
100                                         # TODO: use e2 facilities to create a service ref from file
101                                         if newentry[0] == "/":
102                                                 self.addService(ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + newentry))
103                                         else:
104                                                 self.addService(ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + os.path.dirname(filename) + "/" + newentry))
105                 else:
106                         playlist = PlaylistIOM3U()
107                         return playlist.open(filename)
108                 file.close()
109                 return self.list
110                 
111         def save(self, filename = None):
112                 return self.ERROR