add orBottomToTop, orRightToLeft, orLeftToRight(orHorizontal), orTopToBottom(OrVertic...
[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                 self.displayname = None
62                 try:
63                         file = open(filename, "r")
64                 except IOError:
65                         return None
66                 while True:
67                         entry = file.readline().strip()
68                         if entry == "":
69                                 break
70                         if entry.startswith("#EXTINF:"):
71                                 extinf = entry.split(',',1)
72                                 if len(extinf) > 1:
73                                         self.displayname = extinf[1]
74                                 # TODO: use e2 facilities to create a service ref from file
75                         elif entry[0] != "#":
76                                 if entry[0] == "/":
77                                         sref = ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + entry)
78                                 elif entry.startswith("http"):
79                                         sref = ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + entry.replace(':',"%3a"))
80                                 else:
81                                         sref = ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + os.path.dirname(filename) + "/" + entry)
82                                 if self.displayname:
83                                         sref.ref.setName(self.displayname)
84                                         self.displayname = None
85                                 self.addService(sref)
86                 file.close()
87                 return self.list
88                 
89         def save(self, filename = None):
90                 return self.ERROR
91         
92 class PlaylistIOPLS(PlaylistIO):
93         def __init__(self):
94                 PlaylistIO.__init__(self)
95         
96         def open(self, filename):
97                 self.clear()
98                 try:
99                         file = open(filename, "r")
100                 except IOError:
101                         return None
102                 entry = file.readline().strip()
103                 if entry == "[playlist]": # extended pls
104                         while True:
105                                 entry = file.readline().strip()
106                                 if entry == "":
107                                         break
108                                 if entry[0:4] == "File":
109                                         pos = entry.find('=') + 1
110                                         newentry = entry[pos:]
111                                         # TODO: use e2 facilities to create a service ref from file
112                                         if newentry[0] == "/":
113                                                 self.addService(ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + newentry))
114                                         else:
115                                                 self.addService(ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + os.path.dirname(filename) + "/" + newentry))
116                 else:
117                         playlist = PlaylistIOM3U()
118                         return playlist.open(filename)
119                 file.close()
120                 return self.list
121                 
122         def save(self, filename = None):
123                 return self.ERROR