mirror of
https://github.com/SteamDeckHomebrew/decky-loader.git
synced 2026-07-11 23:11:56 +00:00
Use os.scandir vs os.listdir for filepicker_ls
This commit is contained in:
+14
-14
@@ -181,30 +181,30 @@ class Utilities:
|
|||||||
await service_stop(helpers.REMOTE_DEBUGGER_UNIT)
|
await service_stop(helpers.REMOTE_DEBUGGER_UNIT)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def filepicker_ls(self, path, include_files=True):
|
async def filepicker_ls(self, path, include_files : bool = True, max : int = 1000, page : int = 1):
|
||||||
# def sorter(file): # Modification time
|
# def sorter(file): # Modification time
|
||||||
# if os.path.isdir(os.path.join(path, file)) or os.path.isfile(os.path.join(path, file)):
|
# if os.path.isdir(os.path.join(path, file)) or os.path.isfile(os.path.join(path, file)):
|
||||||
# return os.path.getmtime(os.path.join(path, file))
|
# return os.path.getmtime(os.path.join(path, file))
|
||||||
# return 0
|
# return 0
|
||||||
# file_names = sorted(os.listdir(path), key=sorter, reverse=True) # TODO provide more sort options
|
# file_names = sorted(os.listdir(path), key=sorter, reverse=True) # TODO provide more sort options
|
||||||
file_names = sorted(os.listdir(path)) # Alphabetical
|
|
||||||
|
|
||||||
files = []
|
items = list(os.scandir(path))
|
||||||
|
files = sorted([x for x in items if x.is_file()], key=lambda x: x.name)
|
||||||
|
folders = sorted([x for x in items if x.is_dir()], key=lambda x: x.name)
|
||||||
|
|
||||||
for file in file_names:
|
def to_dict(entry : os.DirEntry[str], is_dir : bool):
|
||||||
full_path = os.path.join(path, file)
|
return {
|
||||||
is_dir = os.path.isdir(full_path)
|
"isdir": is_dir,
|
||||||
|
"name": entry.name,
|
||||||
if is_dir or include_files:
|
"realpath": os.path.realpath(entry.path)
|
||||||
files.append({
|
}
|
||||||
"isdir": is_dir,
|
|
||||||
"name": file,
|
all = [to_dict(x, True) for x in folders] + ([to_dict(x, False) for x in files] if include_files else [])
|
||||||
"realpath": os.path.realpath(full_path)
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"realpath": os.path.realpath(path),
|
"realpath": os.path.realpath(path),
|
||||||
"files": files
|
"files": all[(page-1)*max:(page)*max],
|
||||||
|
"total": len(items)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Based on https://stackoverflow.com/a/46422554/13174603
|
# Based on https://stackoverflow.com/a/46422554/13174603
|
||||||
|
|||||||
Reference in New Issue
Block a user