save playlist on leaving the media player
[enigma2.git] / lib / python / Components / Playlist.py
1 from ServiceReference import ServiceReference
2
3 class PlaylistIO:       
4         def __init__(self):
5                 self.list = []
6         
7         # returns a list of services or None if filename is not a valid playlist
8         def open(self, filename):
9                 return None
10         
11         OK = 0
12         FILEEXISTS = 1
13         WRITEERROR = 2
14         ERROR = 3
15         UNSUPPORTED_FILES_IN_PLAYLIST = 4
16         
17         def save(self, filename = None):
18                 return self.ERROR
19                 
20         def clear(self):
21                 del self.list[:]
22                 
23         def addService(self, service):
24                 self.list.append(service)
25                 
26                 
27 class PlaylistIOInternal(PlaylistIO):
28         def __init__(self):
29                 PlaylistIO.__init__(self)
30         
31         def open(self, filename):
32                 self.clear()
33                 try:
34                         file = open(filename, "r")
35                 except IOError:
36                         return None
37                 while True:
38                         entry = file.readline().strip()
39                         if entry == "":
40                                 break
41                         self.addService(ServiceReference(entry))
42                 file.close()
43                 return self.list
44                 
45         def save(self, filename = None):
46                 print "Writing playlist into file", filename
47                 file = open(filename, "w")
48                 for x in self.list:
49                         file.write(str(x) + "\n")
50                 file.close()
51                 
52                 return self.OK