Skip to content

Commit e38f51c

Browse files
committed
Add function to "paths" for sorting paths.
1 parent f845b44 commit e38f51c

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

domdf_python_tools/paths.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
import stat
5151
import sys
5252
import tempfile
53-
from collections import deque
54-
from typing import IO, Any, Callable, ContextManager, Iterable, Iterator, List, Optional, TypeVar, Union
53+
from collections import defaultdict, deque
54+
from typing import Dict, IO, Any, Callable, ContextManager, Iterable, Iterator, List, Optional, TypeVar, Union
5555

5656
# this package
5757
from domdf_python_tools.compat import nullcontext
@@ -78,6 +78,7 @@
7878
"matchglob",
7979
"unwanted_dirs",
8080
"TemporaryPathPlus",
81+
"sort_paths",
8182
]
8283

8384
newline_default = object()
@@ -1005,3 +1006,33 @@ def cleanup(self) -> None:
10051006

10061007
def __enter__(self) -> PathPlus:
10071008
return self.name
1009+
1010+
1011+
def sort_paths(*paths: PathLike) -> List[PathPlus]:
1012+
"""
1013+
Sort the ``paths`` by directory, then by file.
1014+
1015+
:param paths:
1016+
1017+
.. versionadded:: 2.6.0
1018+
"""
1019+
1020+
directories: Dict[str, List[PathPlus]] = defaultdict(list)
1021+
local_contents: List[PathPlus] = []
1022+
files: List[PathPlus] = []
1023+
1024+
for obj in [PathPlus(path) for path in paths]:
1025+
if len(obj.parts) > 1:
1026+
key = obj.parts[0]
1027+
directories[key].append(obj)
1028+
else:
1029+
local_contents.append(obj)
1030+
1031+
# sort directories
1032+
directories = {directory: directories[directory] for directory in sorted(directories.keys())}
1033+
1034+
for directory, contents in directories.items():
1035+
contents = [path.relative_to(directory) for path in contents]
1036+
files.extend(PathPlus(directory) / path for path in sort_paths(*contents))
1037+
1038+
return files + sorted(local_contents)

0 commit comments

Comments
 (0)