add +
[enigma2.git] / lib / python / Components / Scanner.py
1 from Plugins.Plugin import PluginDescriptor
2 from Components.PluginComponent import plugins
3
4 from os import path as os_path, walk as os_walk
5 from string import lower
6 from mimetypes import guess_type
7
8 def getExtension(file):
9         p = file.rfind('.')
10         if p == -1:
11                 ext = ""
12         else:   
13                 ext = file[p+1:]
14
15         return lower(ext)
16
17 def getType(file):
18         (type, _) = guess_type(file)
19         if type is None:
20                 # Detect some mimetypes unknown to dm7025
21                 # TODO: do mimetypes.add_type once should be better
22                 ext = getExtension(file)
23                 if ext == "ipk":
24                         return "application/x-debian-package"
25                 elif ext == "ogg":
26                         return "application/ogg"
27                 elif ext == "dmpkg":
28                         return "application/x-dream-package"
29                 elif ext == "ts":
30                         return "video/MP2T"
31                 elif ext == "iso":
32                         return "video/x-dvd-iso"
33                 elif file[-12:].lower() == "video_ts.ifo":
34                         return "video/x-dvd"
35         return type
36
37 class Scanner:
38         def __init__(self, name, mimetypes= [], paths_to_scan = [], description = "", openfnc = None):
39                 self.mimetypes = mimetypes
40                 self.name = name
41                 self.paths_to_scan = paths_to_scan
42                 self.description = description
43                 self.openfnc = openfnc
44
45         def checkFile(self, file):
46                 return True
47
48         def handleFile(self, res, file):
49                 if (self.mimetypes is None or file.mimetype in self.mimetypes) and self.checkFile(file):
50                         res.setdefault(self, []).append(file)
51
52         def __repr__(self):
53                 return "<Scanner " + self.name + ">"
54
55         def open(self, list, *args, **kwargs):
56                 if self.openfnc is not None:
57                         self.openfnc(list, *args, **kwargs)
58
59 class ScanPath:
60         def __init__(self, path, with_subdirs = False):
61                 self.path = path
62                 self.with_subdirs = with_subdirs
63
64         def __repr__(self):
65                 return self.path + "(" + str(self.with_subdirs) + ")"
66
67         # we will use this in a set(), so we need to implement __hash__ and __cmp__
68         def __hash__(self):
69                 return self.path.__hash__() ^ self.with_subdirs.__hash__()
70
71         def __cmp__(self, other):
72                 if self.path < other.path:
73                         return -1
74                 elif self.path > other.path:
75                         return +1
76                 else:
77                         return self.with_subdirs.__cmp__(other.with_subdirs)
78
79 class ScanFile:
80         def __init__(self, path, mimetype = None, size = None, autodetect = True):
81                 self.path = path
82                 if mimetype is None and autodetect:
83                         self.mimetype = getType(path)
84                 else:
85                         self.mimetype = mimetype
86                 self.size = size
87
88         def __repr__(self):
89                 return "<ScanFile " + self.path + " (" + str(self.mimetype) + ", " + str(self.size) + " MB)>"
90
91 def execute(option):
92         print "execute", option
93         if option is None:
94                 return
95
96         (_, scanner, files, session) = option
97         scanner.open(files, session)
98
99 def scanDevice(mountpoint):
100         scanner = [ ]
101
102         for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
103                 l = p()
104                 if not isinstance(l, list):
105                         l = [l]
106                 scanner += l
107
108         print "scanner:", scanner
109
110         res = { }
111
112         # merge all to-be-scanned paths, with priority to 
113         # with_subdirs.
114
115         paths_to_scan = set()
116
117         # first merge them all...
118         for s in scanner:
119                 paths_to_scan.update(set(s.paths_to_scan))
120
121         # ...then remove with_subdir=False when same path exists
122         # with with_subdirs=True
123         for p in set(paths_to_scan):
124                 if p.with_subdirs == True and ScanPath(path=p.path) in paths_to_scan:
125                         paths_to_scan.remove(ScanPath(path=p.path))
126
127         # convert to list
128         paths_to_scan = list(paths_to_scan)
129
130         # now scan the paths
131         for p in paths_to_scan:
132                 path = os_path.join(mountpoint, p.path)
133
134                 for root, dirs, files in os_walk(path):
135                         for f in files:
136                                 sfile = ScanFile(os_path.join(root, f))
137                                 for s in scanner:
138                                         s.handleFile(res, sfile)
139
140                         # if we really don't want to scan subdirs, stop here.
141                         if not p.with_subdirs:
142                                 del dirs[:]
143
144         # res is a dict with scanner -> [ScanFiles]
145         return res
146
147 def openList(session, files):
148         if not isinstance(files, list):
149                 files = [ files ]
150
151         scanner = [ ]
152
153         for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
154                 l = p()
155                 if not isinstance(l, list):
156                         l = [l]
157                 scanner += l
158
159         print "scanner:", scanner
160
161         res = { }
162
163         for file in files:
164                 for s in scanner:
165                         s.handleFile(res, file)
166
167         choices = [ (r.description, r, res[r], session) for r in res ]
168         Len = len(choices)
169         if Len > 1:
170                 from Screens.ChoiceBox import ChoiceBox
171
172                 session.openWithCallback(
173                         execute,
174                         ChoiceBox,
175                         title = "The following viewers were found...",
176                         list = choices
177                 )
178                 return True
179         elif Len:
180                 execute(choices[0])
181                 return True
182
183         return False
184
185 def openFile(session, mimetype, file):
186         return openList(session, [ScanFile(file, mimetype)])