1 from Plugins.Plugin import PluginDescriptor
2 from Components.PluginComponent import plugins
4 from os import path as os_path, walk as os_walk
5 from string import lower
6 from mimetypes import guess_type
8 def getExtension(file):
18 (type, _) = guess_type(file)
20 # Detect some mimetypes unknown to dm7025
21 # TODO: do mimetypes.add_type once should be better
22 ext = getExtension(file)
24 return "application/x-debian-package"
26 return "application/ogg"
28 return "application/x-dream-package"
32 def __init__(self, name, mimetypes= [], paths_to_scan = [], description = "", openfnc = None):
33 self.mimetypes = mimetypes
35 self.paths_to_scan = paths_to_scan
36 self.description = description
37 self.openfnc = openfnc
39 def checkFile(self, file):
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)
47 return "<Scanner " + self.name + ">"
49 def open(self, list, *args, **kwargs):
50 if self.openfnc is not None:
51 self.openfnc(list, *args, **kwargs)
54 def __init__(self, path, with_subdirs = False):
56 self.with_subdirs = with_subdirs
59 return self.path + "(" + str(self.with_subdirs) + ")"
61 # we will use this in a set(), so we need to implement __hash__ and __cmp__
63 return self.path.__hash__() ^ self.with_subdirs.__hash__()
65 def __cmp__(self, other):
66 if self.path < other.path:
68 elif self.path > other.path:
71 return self.with_subdirs.__cmp__(other.with_subdirs)
74 def __init__(self, path, mimetype = None, size = None, autodetect = True):
76 if mimetype is None and autodetect:
77 self.mimetype = getType(path)
79 self.mimetype = mimetype
83 return "<ScanFile " + self.path + " (" + str(self.mimetype) + ", " + str(self.size) + " MB)>"
86 print "execute", option
90 (_, scanner, files, session) = option
91 scanner.open(files, session)
93 def scanDevice(mountpoint):
96 for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
98 if not isinstance(l, list):
102 print "scanner:", scanner
106 # merge all to-be-scanned paths, with priority to
109 paths_to_scan = set()
111 # first merge them all...
113 paths_to_scan.update(set(s.paths_to_scan))
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))
122 paths_to_scan = list(paths_to_scan)
125 for p in paths_to_scan:
126 path = os_path.join(mountpoint, p.path)
128 for root, dirs, files in os_walk(path):
130 sfile = ScanFile(os_path.join(root, f))
132 s.handleFile(res, sfile)
134 # if we really don't want to scan subdirs, stop here.
135 if not p.with_subdirs:
138 # res is a dict with scanner -> [ScanFiles]
141 def openList(session, files):
142 if not isinstance(files, list):
147 for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
149 if not isinstance(l, list):
153 print "scanner:", scanner
159 s.handleFile(res, file)
161 choices = [ (r.description, r, res[r], session) for r in res ]
164 from Screens.ChoiceBox import ChoiceBox
166 session.openWithCallback(
169 title = "The following viewers were found...",
179 def openFile(session, mimetype, file):
180 return openList(session, [ScanFile(file, mimetype)])