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 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")
16 (type, _) = guess_type(file)
18 # Detect some unknown types
19 if file[-12:].lower() == "video_ts.ifo":
25 ext = file[p+1:].lower()
27 if ext == "dat" and file[-11:-6].lower() == "avseq":
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 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))
121 from Components.Harddisk import harddiskmanager
122 blockdev = mountpoint.rstrip("/").rsplit('/',1)[-1]
123 error, blacklisted, removable, is_cdrom, partitions, medium_found = harddiskmanager.getBlockDevInfo(blockdev)
126 for p in paths_to_scan:
127 path = os_path.join(mountpoint, p.path)
129 for root, dirs, files in os_walk(path):
131 path = os_path.join(root, f)
132 if is_cdrom and path.endswith(".wav") and path[-13:-6] == ("/track-"):
133 sfile = ScanFile(path,"audio/x-cda")
135 sfile = ScanFile(path)
137 s.handleFile(res, sfile)
139 # if we really don't want to scan subdirs, stop here.
140 if not p.with_subdirs:
143 # res is a dict with scanner -> [ScanFiles]
146 def openList(session, files):
147 if not isinstance(files, list):
152 for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
154 if not isinstance(l, list):
158 print "scanner:", scanner
164 s.handleFile(res, file)
166 choices = [ (r.description, r, res[r], session) for r in res ]
169 from Screens.ChoiceBox import ChoiceBox
171 session.openWithCallback(
174 title = "The following viewers were found...",
184 def openFile(session, mimetype, file):
185 return openList(session, [ScanFile(file, mimetype)])