1 from re import compile as re_compile
2 from os import path as os_path, listdir
3 from MenuList import MenuList
4 from Components.Harddisk import harddiskmanager
6 from Tools.Directories import SCOPE_SKIN_IMAGE, resolveFilename
8 from enigma import RT_HALIGN_LEFT, eListboxPythonMultiContent, \
9 eServiceReference, eServiceCenter, gFont
10 from Tools.LoadPixmap import LoadPixmap
27 def FileEntryComponent(name, absolute = None, isDir = False):
28 res = [ (absolute, isDir) ]
29 res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
31 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
33 extension = name.split('.')
34 extension = extension[-1].lower()
35 if EXTENSIONS.has_key(extension):
36 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
40 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
44 class FileList(MenuList):
45 def __init__(self, directory, showDirectories = True, showFiles = True, showMountpoints = True, matchingPattern = None, useServiceRef = False, inhibitDirs = False, inhibitMounts = False, isTop = False, enableWrapAround = False, additionalExtensions = None):
46 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
47 self.additional_extensions = additionalExtensions
49 self.current_directory = None
50 self.current_mountpoint = None
51 self.useServiceRef = useServiceRef
52 self.showDirectories = showDirectories
53 self.showMountpoints = showMountpoints
54 self.showFiles = showFiles
56 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
57 self.matchingPattern = matchingPattern
58 self.inhibitDirs = inhibitDirs or []
59 self.inhibitMounts = inhibitMounts or []
61 self.refreshMountpoints()
62 self.changeDir(directory)
63 self.l.setFont(0, gFont("Regular", 18))
64 self.l.setItemHeight(23)
65 self.serviceHandler = eServiceCenter.getInstance()
67 def refreshMountpoints(self):
68 self.mountpoints = [os_path.join(p.mountpoint, "") for p in harddiskmanager.getMountedPartitions()]
69 self.mountpoints.sort(reverse = True)
71 def getMountpoint(self, file):
72 file = os_path.join(os_path.realpath(file), "")
73 for m in self.mountpoints:
74 if file.startswith(m):
78 def getMountpointLink(self, file):
79 if os_path.realpath(file) == file:
80 return self.getMountpoint(file)
84 mp = self.getMountpoint(file)
86 file = os_path.dirname(file)
87 while last != "/" and mp == self.getMountpoint(file):
89 file = os_path.dirname(file)
90 return os_path.join(last, "")
92 def getSelection(self):
93 if self.l.getCurrentSelection() is None:
95 return self.l.getCurrentSelection()[0]
97 def getCurrentEvent(self):
98 l = self.l.getCurrentSelection()
99 if not l or l[0][1] == True:
102 return self.serviceHandler.info(l[0][0]).getEvent(l[0][0])
104 def getFileList(self):
107 def inParentDirs(self, dir, parents):
108 dir = os_path.realpath(dir)
110 if dir.startswith(p):
114 def changeDir(self, directory, select = None):
117 if directory and not os_path.isdir(directory):
119 # if we are just entering from the list of mount points:
120 if self.current_directory is None:
121 if directory and self.showMountpoints:
122 self.current_mountpoint = self.getMountpointLink(directory)
124 self.current_mountpoint = None
125 self.current_directory = directory
129 if directory is None and self.showMountpoints: # present available mountpoints
130 for p in harddiskmanager.getMountedPartitions():
131 path = os_path.join(p.mountpoint, "")
132 if path not in self.inhibitMounts and not self.inParentDirs(path, self.inhibitDirs):
133 self.list.append(FileEntryComponent(name = p.description, absolute = path, isDir = True))
136 elif self.useServiceRef:
137 root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
138 if self.additional_extensions:
139 root.setName(self.additional_extensions)
140 serviceHandler = eServiceCenter.getInstance()
141 list = serviceHandler.list(root)
148 if s.flags & s.mustDescent:
149 directories.append(s.getPath())
155 if os_path.exists(directory):
156 files = listdir(directory)
160 if os_path.isdir(directory + x):
161 directories.append(directory + x + "/")
164 if directory is not None and self.showDirectories and not self.isTop:
165 if directory == self.current_mountpoint and self.showMountpoints:
166 self.list.append(FileEntryComponent(name = "<" +_("List of Storage Devices") + ">", absolute = None, isDir = True))
167 elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
168 self.list.append(FileEntryComponent(name = "<" +_("Parent Directory") + ">", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
170 if self.showDirectories:
171 for x in directories:
172 if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
173 name = x.split('/')[-2]
174 self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
178 if self.useServiceRef:
180 name = path.split('/')[-1]
185 if (self.matchingPattern is None) or re_compile(self.matchingPattern).search(path):
186 self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
188 self.l.setList(self.list)
190 if select is not None:
196 if isinstance(p, eServiceReference):
203 def getCurrentDirectory(self):
204 return self.current_directory
206 def canDescent(self):
207 if self.getSelection() is None:
209 return self.getSelection()[1]
212 if self.getSelection() is None:
214 self.changeDir(self.getSelection()[0], select = self.current_directory)
216 def getFilename(self):
217 if self.getSelection() is None:
219 x = self.getSelection()[0]
220 if isinstance(x, eServiceReference):
224 def getServiceRef(self):
225 if self.getSelection() is None:
227 x = self.getSelection()[0]
228 if isinstance(x, eServiceReference):
233 harddiskmanager.on_partition_list_change.append(self.partitionListChanged)
236 harddiskmanager.on_partition_list_change.remove(self.partitionListChanged)
239 self.changeDir(self.current_directory, self.getFilename())
241 def partitionListChanged(self, action, device):
242 self.refreshMountpoints()
243 if self.current_directory is None: