-
Notifications
You must be signed in to change notification settings - Fork 4
/
__init__.py
74 lines (55 loc) · 1.82 KB
/
__init__.py
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
from typing import Callable
from .path import PathEntry
COLORS = {
'symlink_target': "{CYAN}",
'owner_user': "{INTENSE_YELLOW}",
'owner_group': "{BLUE}",
'size_unit': "{CYAN}",
}
XlsdSortMethod = Callable[[list[PathEntry]], list[PathEntry]]
XLSD_SORT_METHODS: dict[str, XlsdSortMethod] = {}
def xlsd_register_sort_method(name: str):
"""
Register a function that will be used to sort all direntries.
"""
def decorator(func: XlsdSortMethod):
XLSD_SORT_METHODS[name] = func
return func
return decorator
def _direntry_lowercase_name(entry: PathEntry) -> str:
"""
Return the lowercase name for a DirEntry.
This is used to sort a list of DirEntry by name.
"""
return entry.name.lower()
@xlsd_register_sort_method('directories_first')
def xlsd_sort_directories_first(entries: list[PathEntry]) -> list[PathEntry]:
"""
Sort the entries in alphabetical order, directories first.
"""
directories = []
files = []
for entry in entries:
try:
if entry.is_dir():
directories.append(entry)
else:
files.append(entry)
except OSError: # Probably circular symbolic link
directories.append(entry)
directories.sort(key=_direntry_lowercase_name)
files.sort(key=_direntry_lowercase_name)
return directories + files
@xlsd_register_sort_method('alphabetical')
def xlsd_sort_alphabetical(entries: list[PathEntry]) -> list[PathEntry]:
"""
Sort the entries in alphabetical order.
"""
entries.sort(key=_direntry_lowercase_name)
return entries
@xlsd_register_sort_method('as_is')
def xlsd_sort_as_is(entries: list[PathEntry]) -> list[PathEntry]:
"""
Keep the entries in the same order they were returned by the OS.
"""
return entries