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