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 return "video/x-dvd-iso"
33 elif file[-12:].lower() == "video_ts.ifo":
38 def __init__(self, name, mimetypes= [], paths_to_scan = [], description = "", openfnc = None):
39 self.mimetypes = mimetypes
41 self.paths_to_scan = paths_to_scan
42 self.description = description
43 self.openfnc = openfnc
45 def checkFile(self, file):
48 def handleFile(self, res, file):
49 if (self.mimetypes is None or file.mimetype in self.mimetypes) and self.checkFile(file):
50 res.setdefault(self, []).append(file)
53 return "<Scanner " + self.name + ">"
55 def open(self, list, *args, **kwargs):
56 if self.openfnc is not None:
57 self.openfnc(list, *args, **kwargs)
60 def __init__(self, path, with_subdirs = False):
62 self.with_subdirs = with_subdirs
65 return self.path + "(" + str(self.with_subdirs) + ")"
67 # we will use this in a set(), so we need to implement __hash__ and __cmp__
69 return self.path.__hash__() ^ self.with_subdirs.__hash__()
71 def __cmp__(self, other):
72 if self.path < other.path:
74 elif self.path > other.path:
77 return self.with_subdirs.__cmp__(other.with_subdirs)
80 def __init__(self, path, mimetype = None, size = None, autodetect = True):
82 if mimetype is None and autodetect:
83 self.mimetype = getType(path)
85 self.mimetype = mimetype
89 return "<ScanFile " + self.path + " (" + str(self.mimetype) + ", " + str(self.size) + " MB)>"
92 print "execute", option
96 (_, scanner, files, session) = option
97 scanner.open(files, session)
99 def scanDevice(mountpoint):
102 for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
104 if not isinstance(l, list):
108 print "scanner:", scanner
112 # merge all to-be-scanned paths, with priority to
115 paths_to_scan = set()
117 # first merge them all...
119 paths_to_scan.update(set(s.paths_to_scan))
121 # ...then remove with_subdir=False when same path exists
122 # with with_subdirs=True
123 for p in set(paths_to_scan):
124 if p.with_subdirs == True and ScanPath(path=p.path) in paths_to_scan:
125 paths_to_scan.remove(ScanPath(path=p.path))
128 paths_to_scan = list(paths_to_scan)
130 from Components.Harddisk import HarddiskManager
131 class CdromManager(HarddiskManager):
134 cdaman = CdromManager()
135 blockdev = mountpoint.split('/')[2]
136 error, blacklisted, removable, is_cdrom, partitions = cdaman.getBlockDevInfo(blockdev)
139 for p in paths_to_scan:
140 path = os_path.join(mountpoint, p.path)
142 for root, dirs, files in os_walk(path):
144 path = os_path.join(root, f)
145 if is_cdrom and path.endswith(".wav") and path[-13:-6] == ("/track-"):
146 sfile = ScanFile(path,"audio/x-cda")
148 sfile = ScanFile(path)
150 s.handleFile(res, sfile)
152 # if we really don't want to scan subdirs, stop here.
153 if not p.with_subdirs:
156 # res is a dict with scanner -> [ScanFiles]
159 def openList(session, files):
160 if not isinstance(files, list):
165 for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
167 if not isinstance(l, list):
171 print "scanner:", scanner
177 s.handleFile(res, file)
179 choices = [ (r.description, r, res[r], session) for r in res ]
182 from Screens.ChoiceBox import ChoiceBox
184 session.openWithCallback(
187 title = "The following viewers were found...",
197 def openFile(session, mimetype, file):
198 return openList(session, [ScanFile(file, mimetype)])