handle multiple configuration files correctly. Patch by Pieter Grimmerink
[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         return type
30
31 class Scanner:
32         def __init__(self, name, mimetypes= [], paths_to_scan = [], description = "", openfnc = None):
33                 self.mimetypes = mimetypes
34                 self.name = name
35                 self.paths_to_scan = paths_to_scan
36                 self.description = description
37                 self.openfnc = openfnc
38
39         def checkFile(self, file):
40                 return True
41
42         def handleFile(self, res, file):
43                 if (self.mimetypes is None or file.mimetype in self.mimetypes) and self.checkFile(file):
44                         res.setdefault(self, []).append(file)
45
46         def __repr__(self):
47                 return "<Scanner " + self.name + ">"
48
49         def open(self, list, *args, **kwargs):
50                 if self.openfnc is not None:
51                         self.openfnc(list, *args, **kwargs)
52
53 class ScanPath:
54         def __init__(self, path, with_subdirs = False):
55                 self.path = path
56                 self.with_subdirs = with_subdirs
57
58         def __repr__(self):
59                 return self.path + "(" + str(self.with_subdirs) + ")"
60
61         # we will use this in a set(), so we need to implement __hash__ and __cmp__
62         def __hash__(self):
63                 return self.path.__hash__() ^ self.with_subdirs.__hash__()
64
65         def __cmp__(self, other):
66                 if self.path < other.path:
67                         return -1
68                 elif self.path > other.path:
69                         return +1
70                 else:
71                         return self.with_subdirs.__cmp__(other.with_subdirs)
72
73 class ScanFile:
74         def __init__(self, path, mimetype = None, size = None, autodetect = True):
75                 self.path = path
76                 if mimetype is None and autodetect:
77                         self.mimetype = getType(path)
78                 else:
79                         self.mimetype = mimetype
80                 self.size = size
81
82         def __repr__(self):
83                 return "<ScanFile " + self.path + " (" + str(self.mimetype) + ", " + str(self.size) + " MB)>"
84
85 def execute(option):
86         print "execute", option
87         if option is None:
88                 return
89
90         (_, scanner, files, session) = option
91         scanner.open(files, session)
92
93 def scanDevice(mountpoint):
94         scanner = [ ]
95
96         for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
97                 l = p()
98                 if not isinstance(l, list):
99                         l = [l]
100                 scanner += l
101
102         print "scanner:", scanner
103
104         res = { }
105
106         # merge all to-be-scanned paths, with priority to 
107         # with_subdirs.
108
109         paths_to_scan = set()
110
111         # first merge them all...
112         for s in scanner:
113                 paths_to_scan.update(set(s.paths_to_scan))
114
115         # ...then remove with_subdir=False when same path exists
116         # with with_subdirs=True
117         for p in set(paths_to_scan):
118                 if p.with_subdirs == True and ScanPath(path=p.path) in paths_to_scan:
119                         paths_to_scan.remove(ScanPath(path=p.path))
120
121         # convert to list
122         paths_to_scan = list(paths_to_scan)
123
124         # now scan the paths
125         for p in paths_to_scan:
126                 path = os_path.join(mountpoint, p.path)
127
128                 for root, dirs, files in os_walk(path):
129                         for f in files:
130                                 sfile = ScanFile(os_path.join(root, f))
131                                 for s in scanner:
132                                         s.handleFile(res, sfile)
133
134                         # if we really don't want to scan subdirs, stop here.
135                         if not p.with_subdirs:
136                                 del dirs[:]
137
138         # res is a dict with scanner -> [ScanFiles]
139         return res
140
141 def openList(session, files):
142         if not isinstance(files, list):
143                 files = [ files ]
144
145         scanner = [ ]
146
147         for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
148                 l = p()
149                 if not isinstance(l, list):
150                         l = [l]
151                 scanner += l
152
153         print "scanner:", scanner
154
155         res = { }
156
157         for file in files:
158                 for s in scanner:
159                         s.handleFile(res, file)
160
161         choices = [ (r.description, r, res[r], session) for r in res ]
162         Len = len(choices)
163         if Len > 1:
164                 from Screens.ChoiceBox import ChoiceBox
165
166                 session.openWithCallback(
167                         execute,
168                         ChoiceBox,
169                         title = "The following viewers were found...",
170                         list = choices
171                 )
172                 return True
173         elif Len:
174                 execute(choices[0])
175                 return True
176
177         return False
178
179 def openFile(session, mimetype, file):
180         return openList(session, [ScanFile(file, mimetype)])