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"
30 return "video/x-dvd-iso"
31 elif file[-12:].lower() == "video_ts.ifo":
36 def __init__(self, name, mimetypes= [], paths_to_scan = [], description = "", openfnc = None):
37 self.mimetypes = mimetypes
39 self.paths_to_scan = paths_to_scan
40 self.description = description
41 self.openfnc = openfnc
43 def checkFile(self, file):
46 def handleFile(self, res, file):
47 if (self.mimetypes is None or file.mimetype in self.mimetypes) and self.checkFile(file):
48 res.setdefault(self, []).append(file)
51 return "<Scanner " + self.name + ">"
53 def open(self, list, *args, **kwargs):
54 if self.openfnc is not None:
55 self.openfnc(list, *args, **kwargs)
58 def __init__(self, path, with_subdirs = False):
60 self.with_subdirs = with_subdirs
63 return self.path + "(" + str(self.with_subdirs) + ")"
65 # we will use this in a set(), so we need to implement __hash__ and __cmp__
67 return self.path.__hash__() ^ self.with_subdirs.__hash__()
69 def __cmp__(self, other):
70 if self.path < other.path:
72 elif self.path > other.path:
75 return self.with_subdirs.__cmp__(other.with_subdirs)
78 def __init__(self, path, mimetype = None, size = None, autodetect = True):
80 if mimetype is None and autodetect:
81 self.mimetype = getType(path)
83 self.mimetype = mimetype
87 return "<ScanFile " + self.path + " (" + str(self.mimetype) + ", " + str(self.size) + " MB)>"
90 print "execute", option
94 (_, scanner, files, session) = option
95 scanner.open(files, session)
97 def scanDevice(mountpoint):
100 for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
102 if not isinstance(l, list):
106 print "scanner:", scanner
110 # merge all to-be-scanned paths, with priority to
113 paths_to_scan = set()
115 # first merge them all...
117 paths_to_scan.update(set(s.paths_to_scan))
119 # ...then remove with_subdir=False when same path exists
120 # with with_subdirs=True
121 for p in set(paths_to_scan):
122 if p.with_subdirs == True and ScanPath(path=p.path) in paths_to_scan:
123 paths_to_scan.remove(ScanPath(path=p.path))
126 paths_to_scan = list(paths_to_scan)
129 for p in paths_to_scan:
130 path = os_path.join(mountpoint, p.path)
132 for root, dirs, files in os_walk(path):
134 sfile = ScanFile(os_path.join(root, f))
136 s.handleFile(res, sfile)
138 # if we really don't want to scan subdirs, stop here.
139 if not p.with_subdirs:
142 # res is a dict with scanner -> [ScanFiles]
145 def openList(session, files):
146 if not isinstance(files, list):
151 for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
153 if not isinstance(l, list):
157 print "scanner:", scanner
163 s.handleFile(res, file)
165 choices = [ (r.description, r, res[r], session) for r in res ]
168 from Screens.ChoiceBox import ChoiceBox
170 session.openWithCallback(
173 title = "The following viewers were found...",
183 def openFile(session, mimetype, file):
184 return openList(session, [ScanFile(file, mimetype)])