1 from Plugins.Plugin import PluginDescriptor
2 from os import path as os_path, walk as os_walk
3 from string import lower
5 def getExtension(file):
15 def __init__(self, name, extensions = [], paths_to_scan = [], description = "", openfnc = None):
16 self.extensions = extensions
18 self.paths_to_scan = paths_to_scan
19 self.description = description
20 self.openfnc = openfnc
22 def checkFile(self, filename):
25 def handleFile(self, res, filename, ext):
26 if (self.extensions is None or ext in self.extensions) and self.checkFile(filename):
27 res.setdefault(self, []).append(filename)
30 return "<Scanner " + self.name + ">"
32 def open(self, list, *args, **kwargs):
33 if self.openfnc is not None:
34 self.openfnc(list, *args, **kwargs)
37 def __init__(self, path, with_subdirs = False):
39 self.with_subdirs = with_subdirs
42 return self.path + "(" + str(self.with_subdirs) + ")"
44 # we will use this in a set(), so we need to implement __hash__ and __cmp__
46 return self.path.__hash__() ^ self.with_subdirs.__hash__()
48 def __cmp__(self, other):
49 if self.path < other.path:
51 elif self.path > other.path:
54 return self.with_subdirs.__cmp__(other.with_subdirs)
57 # Scanner(extensions = ["jpg", "jpe", "jpeg"],
60 # ScanPath(path = "DCIM", with_subdirs = True),
61 # ScanPath(path = "", with_subdirs = False),
64 # description = "View Photos..."
67 # Scanner(extensions = ["mpg", "vob", "ts"],
70 # ScanPath(path = ""),
71 # ScanPath(path = "movie", with_subdirs = True),
74 # description = "View Movies..."
77 # Scanner(extensions = ["mp3", "ogg"],
81 # ScanPath(path = "", with_subdirs = False),
83 # description = "Play music..."
86 # Scanner(extensions = ["ipk"],
90 # ScanPath(path = ""),
92 # description = "Install software..."
96 def ScanDevice(mountpoint):
97 from Components.PluginComponent import plugins
101 for p in plugins.getPlugins(PluginDescriptor.WHERE_FILESCAN):
103 if not isinstance(l, list):
107 print "scanner:", scanner
111 # merge all to-be-scanned paths, with priority to
114 paths_to_scan = set()
116 # first merge them all...
118 paths_to_scan.update(set(s.paths_to_scan))
120 # ...then remove with_subdir=False when same path exists
121 # with with_subdirs=True
122 for p in set(paths_to_scan):
123 if p.with_subdirs == True and ScanPath(path=p.path) in paths_to_scan:
124 paths_to_scan.remove(ScanPath(path=p.path))
127 paths_to_scan = list(paths_to_scan)
130 for p in paths_to_scan:
131 path = os_path.join(mountpoint, p.path)
133 for root, dirs, files in os_walk(path):
135 ext = getExtension(f)
136 pathname = os_path.join(root, f)
138 s.handleFile(res, pathname, ext)
140 # if we really don't want to scan subdirs, stop here.
141 if not p.with_subdirs:
144 # res is a dict with scanner -> [files]
148 print "execute", option
152 (_, scanner, files, session) = option
153 scanner.open(files, session)
156 from Screens.ChoiceBox import ChoiceBox
157 # HARDCODED - need to scan all mountpoints
158 res = ScanDevice("/hdd/")
160 list = [ (r.description, r, res[r], session) for r in res ]
163 print "nothing found"
166 session.openWithCallback(execute, ChoiceBox,
167 title = "The following files were found...",
170 def main(session, **kwargs):
173 def Plugins(**kwargs):
174 return PluginDescriptor(name="MediaScanner", description="Scan Files...", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main)