1 from Plugins.Plugin import PluginDescriptor
2 from Components.PluginComponent import plugins
4 from os import path as os_path, walk as os_walk, system
5 from mimetypes import guess_type, add_type
7 add_type("application/x-debian-package", ".ipk")
8 add_type("application/ogg", ".ogg")
9 add_type("audio/x-flac", ".flac")
10 add_type("application/x-dream-package", ".dmpkg")
11 add_type("application/x-dream-image", ".nfi")
12 add_type("video/MP2T", ".ts")
13 add_type("video/x-dvd-iso", ".iso")
14 add_type("video/x-matroska", ".mkv")
15 add_type("audio/x-matroska", ".mka")
18 (type, _) = guess_type(file)
20 # Detect some unknown types
21 if file[-12:].lower() == "video_ts.ifo":
27 ext = file[p+1:].lower()
29 if ext == "dat" and file[-11:-6].lower() == "avseq":
34 def __init__(self, name, mimetypes= [], paths_to_scan = [], description = "", openfnc = None):
35 self.mimetypes = mimetypes
37 self.paths_to_scan = paths_to_scan
38 self.description = description
39 self.openfnc = openfnc
41 def checkFile(self, file):
44 def handleFile(self, res, file):
45 if (self.mimetypes is None or file.mimetype in self.mimetypes) and self.checkFile(file):
46 res.setdefault(self, []).append(file)
49 return "<Scanner " + self.name + ">"
51 def open(self, list, *args, **kwargs):
52 if self.openfnc is not None:
53 self.openfnc(list, *args, **kwargs)
56 def __init__(self, path, with_subdirs = False):
58 self.with_subdirs = with_subdirs
61 return self.path + "(" + str(self.with_subdirs) + ")"
63 # we will use this in a set(), so we need to implement __hash__ and __cmp__
65 return self.path.__hash__() ^ self.with_subdirs.__hash__()
67 def __cmp__(self, other):
68 if self.path < other.path:
70 elif self.path > other.path:
73 return self.with_subdirs.__cmp__(other.with_subdirs)
76 def __init__(self, path, mimetype = None, size = None, autodetect = True):
78 if mimetype is None and autodetect:
79 self.mimetype = getType(path)
81 self.mimetype = mimetype
85 return "<ScanFile " + self.path + " (" + str(self.mimetype) + ", " + str(self.size) + " MB)>"
88 print "execute", option
92 (_, scanner, files, session) = option
93 scanner.open(files, session)
95 def scanDevice(mountpoint):
98 for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
100 if not isinstance(l, list):
104 print "scanner:", scanner
108 # merge all to-be-scanned paths, with priority to
111 paths_to_scan = set()
113 # first merge them all...
115 paths_to_scan.update(set(s.paths_to_scan))
117 # ...then remove with_subdir=False when same path exists
118 # with with_subdirs=True
119 for p in paths_to_scan:
120 if p.with_subdirs == True and ScanPath(path=p.path) in paths_to_scan:
121 paths_to_scan.remove(ScanPath(path=p.path))
123 from Components.Harddisk import harddiskmanager
124 blockdev = mountpoint.rstrip("/").rsplit('/',1)[-1]
125 error, blacklisted, removable, is_cdrom, partitions, medium_found = harddiskmanager.getBlockDevInfo(blockdev)
128 for p in paths_to_scan:
129 path = os_path.join(mountpoint, p.path)
133 for root, dirs, files in os_walk(path):
135 path = os_path.join(root, f)
136 if is_cdrom and path.endswith(".wav") and path[-13:-6] == ("/track-"):
137 sfile = ScanFile(path,"audio/x-cda")
139 sfile = ScanFile(path)
141 s.handleFile(res, sfile)
143 # if we really don't want to scan subdirs, stop here.
144 if not p.with_subdirs:
147 # res is a dict with scanner -> [ScanFiles]
150 def openList(session, files):
151 if not isinstance(files, list):
156 for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
158 if not isinstance(l, list):
162 print "scanner:", scanner
168 s.handleFile(res, file)
170 choices = [ (r.description, r, res[r], session) for r in res ]
173 from Screens.ChoiceBox import ChoiceBox
175 session.openWithCallback(
178 title = "The following viewers were found...",
188 def openFile(session, mimetype, file):
189 return openList(session, [ScanFile(file, mimetype)])