forked from OpenPLi/enigma2-binary-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyFileList.py
More file actions
291 lines (263 loc) · 9.93 KB
/
myFileList.py
File metadata and controls
291 lines (263 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#######################################################################
#
# Vali's File-List with some extendet functions
# based on FileList(Enigma-2)
#
# Coded by Vali (c)2009-2011
# Main idea and getTSLength/getTSInfo/Sort functions by DarkVolli
# Support: www.dreambox-tools.info
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#######################################################################
from os import path as os_path, listdir, stat as os_stat
from Components.MenuList import MenuList
from Components.Harddisk import harddiskmanager
from enigma import RT_HALIGN_LEFT, eListboxPythonMultiContent, eServiceReference, eServiceCenter, gFont, iServiceInformation
from Tools.LoadPixmap import LoadPixmap
from Tools.Directories import SCOPE_PLUGINS, resolveFilename
def FileEntryComponent(name, absolute=None, isDir=False):
res = [(absolute, isDir)]
res.append((eListboxPythonMultiContent.TYPE_TEXT, 40, 2, 1000, 22, 0, RT_HALIGN_LEFT, name))
png = LoadPixmap(resolveFilename(SCOPE_PLUGINS, "Extensions/LCD4linux/data/dir.png")) if isDir else None
if png is not None:
res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 12, 3, 20, 20, png))
return res
class FileList(MenuList):
def __init__(self, directory, showDirectories=True, showFiles=True, showMountpoints=True, matchingPattern=None, useServiceRef=False, inhibitDirs=False, inhibitMounts=False, isTop=False, enableWrapAround=True, additionalExtensions=None):
MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
self.additional_extensions = additionalExtensions
self.mountpoints = []
self.current_directory = None
self.current_mountpoint = None
self.useServiceRef = useServiceRef
self.showDirectories = showDirectories
self.showMountpoints = showMountpoints
self.showFiles = showFiles
self.isTop = isTop
self.matchingPattern = matchingPattern
self.inhibitDirs = inhibitDirs or []
self.inhibitMounts = inhibitMounts or []
self.refreshMountpoints()
direct = directory
if directory.endswith("/"):
se = directory
else:
se = os_path.basename(directory)
direct = "%s/" % directory
self.changeDir(direct, se)
self.l.setFont(0, gFont("Regular", 18))
self.l.setItemHeight(26)
self.serviceHandler = eServiceCenter.getInstance()
def refreshMountpoints(self):
self.mountpoints = [os_path.join(p.mountpoint, "") for p in harddiskmanager.getMountedPartitions()]
self.mountpoints.sort(reverse=True)
def getMountpoint(self, file):
file = os_path.join(os_path.realpath(file), "")
for m in self.mountpoints:
if file.startswith(m):
return m
return False
def getMountpointLink(self, file):
if os_path.realpath(file) == file:
return self.getMountpoint(file)
else:
if file[-1] == "/":
file = file[:-1]
mp = self.getMountpoint(file)
last = file
file = os_path.dirname(file)
while last != "/" and mp == self.getMountpoint(file):
last = file
file = os_path.dirname(file)
return os_path.join(last, "")
def getSelection(self):
if self.l.getCurrentSelection() is None:
return ""
return self.l.getCurrentSelection()[0]
def getCurrentEvent(self):
l = self.l.getCurrentSelection()
return None if not l or l[0][1] == True else self.serviceHandler.info(l[0][0]).getEvent(l[0][0])
def getFileList(self):
return self.list
def inParentDirs(self, dir, parents):
dir = os_path.realpath(dir)
for p in parents:
if dir.startswith(p):
return True
return False
def changeDir(self, directory, select=None):
self.list = []
if self.current_directory is None:
self.current_mountpoint = self.getMountpointLink(directory) if directory and self.showMountpoints else None
self.current_directory = directory
directories = []
files = []
if directory is None and self.showMountpoints:
for p in harddiskmanager.getMountedPartitions():
path = os_path.join(p.mountpoint, "")
if path not in self.inhibitMounts and not self.inParentDirs(path, self.inhibitDirs):
self.list.append(FileEntryComponent(name=p.description, absolute=path, isDir=True))
files = []
directories = []
elif directory is None:
files = []
directories = []
elif self.useServiceRef:
root = eServiceReference("2:0:1:0:0:0:0:0:0:0:%s" % directory)
if self.additional_extensions:
root.setName(self.additional_extensions)
serviceHandler = eServiceCenter.getInstance()
list = serviceHandler.list(root)
while True:
s = list.getNext()
if not s.valid():
del list
break
if s.flags & s.mustDescent:
directories.append(s.getPath())
else:
files.append(s)
directories.sort()
files.sort()
else:
if os_path.exists(directory):
try:
files = listdir(directory)
except Exception:
files = []
files.sort()
tmpfiles = files[:]
for x in tmpfiles:
if os_path.isdir("%s%s" % (directory, x)):
directories.append("%s%s/" % (directory, x))
files.remove(x)
if directory is not None and self.showDirectories and not self.isTop:
if directory == self.current_mountpoint is not None and self.showMountpoints is not None:
self.list.append(FileEntryComponent(name="<%s>" % _("List of Storage Devices"), absolute=None, isDir=True))
elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
self.list.append(FileEntryComponent(name="<%s>" % _("Parent Directory"), absolute='/'.join(directory.split('/')[:-2]) + '/', isDir=True))
if self.showDirectories:
for x in directories:
if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
name = x.split('/')[-2]
self.list.append(FileEntryComponent(name=name, absolute=x, isDir=True))
if self.showFiles:
nx = 0
for x in files:
if self.useServiceRef:
path = x.getPath()
name = path.split('/')[-1]
else:
path = "%s%s" % (directory, x)
name = x
nx = None
EXext = os_path.splitext(path)[1]
EXext = EXext.replace(".", "")
EXext = EXext.lower()
if (EXext == ""):
EXext = "nothing"
if (self.matchingPattern is None) or (EXext in self.matchingPattern):
if nx is None:
self.list.append(FileEntryComponent(name=name, absolute=x, isDir=False))
else:
res = [(x, False)]
res.append((eListboxPythonMultiContent.TYPE_TEXT, 40, 2, 1000, 22, 0, RT_HALIGN_LEFT, "%s [%s]" % (name, self.getTSLength(path))))
self.list.append(res)
self.l.setList(self.list)
if select is not None:
i = 0
self.moveToIndex(0)
select2 = select
if select.endswith("/"):
select2 = os_path.basename(select[:-1])
for x in self.list:
p = x[0][0]
if isinstance(p, eServiceReference):
p = p.getPath()
if p == select or p == select2:
self.moveToIndex(i)
i += 1
def getCurrentDirectory(self):
return self.current_directory
def canDescent(self):
return False if self.getSelection() == "" else self.getSelection()[1]
def descent(self):
if self.getSelection() != "":
se = ""
if self.current_directory is not None:
se = self.current_directory if self.current_directory.endswith("/") else os_path.basename(self.current_directory)
self.changeDir(self.getSelection()[0], select=se)
def getFilename(self):
if self.getSelection() == "":
return ""
x = self.getSelection()[0]
if isinstance(x, eServiceReference):
x = x.getPath()
return x
def getServiceRef(self):
if self.getSelection() == "":
return None
x = self.getSelection()[0]
return x if isinstance(x, eServiceReference) else None
def execBegin(self):
harddiskmanager.on_partition_list_change.append(self.partitionListChanged)
def execEnd(self):
harddiskmanager.on_partition_list_change.remove(self.partitionListChanged)
def refresh(self):
se = ""
if self.getFilename() != "":
se = self.getFilename() if self.getFilename().endswith("/") else os_path.basename(self.getFilename())
self.changeDir(self.current_directory, se)
def partitionListChanged(self, action, device):
self.refreshMountpoints()
if self.current_directory is None:
self.refresh()
def getTSInfo(self, path):
if path.endswith(".ts"):
serviceref = eServiceReference("1:0:0:0:0:0:0:0:0:0:%s" % path)
if not serviceref.valid():
return None
serviceHandler = eServiceCenter.getInstance()
info = serviceHandler.info(serviceref)
if info is not None:
txt = info.getName(serviceref)
description = info.getInfoString(serviceref, iServiceInformation.sDescription)
if not txt.endswith(".ts"):
return "%s - %s" % (txt, description) if description != "" else txt
else:
evt = info.getEvent(serviceref)
return "%s - %s" % (evt.getEventName(), evt.getShortDescription()) if evt else None
def getTSLength(self, path):
tslen = ""
if path.endswith(".ts"):
serviceref = eServiceReference("1:0:0:0:0:0:0:0:0:0:%s" % path)
serviceHandler = eServiceCenter.getInstance()
info = serviceHandler.info(serviceref)
tslen = info.getLength(serviceref)
return "%d:%02d" % (tslen / 60, tslen % 60) if tslen > 0 else ""
def byNameFunc(self, a, b):
return cmp(b[0][1], a[0][1]) or cmp(a[1][7], b[1][7])
def sortName(self):
self.list.sort(self.byNameFunc)
self.l.setList(self.list)
self.moveToIndex(0)
def byDateFunc(self, a, b):
try:
stat1 = os_stat(self.current_directory + a[0][0])
stat2 = os_stat(self.current_directory + b[0][0])
except Exception:
return 0
return cmp(b[0][1], a[0][1]) or cmp(stat2.st_ctime, stat1.st_ctime)
def sortDate(self):
self.list.sort(self.byDateFunc)
self.l.setList(self.list)
self.moveToIndex(0)