First path restriction test

pull/62/head
Thomas Bouve 2021-09-05 21:46:38 +02:00
rodzic 6da5fe6df1
commit d6e0568709
1 zmienionych plików z 20 dodań i 8 usunięć

Wyświetl plik

@ -6,28 +6,40 @@ import sys
from typing import List, Sequence, Iterable, Optional
def get_subpaths(path: str) -> List[str]:
def get_subpaths(path: str, root_path: str) -> List[str]:
"""Walk a path and return a list of subpaths."""
if os.path.isfile(path):
path = os.path.dirname(path)
path = strip_root_path(path, root_path)
paths = [path]
path, tail = os.path.split(path)
while tail:
paths.append(path)
path, tail = os.path.split(path)
try:
# Add Windows drive letters, but remove the current drive
drives = get_drive_letters()
drives.remove(paths[-1])
paths.extend(drives)
except ValueError:
pass
if not root_path:
try:
# Add Windows drive letters, but remove the current drive
drives = get_drive_letters()
drives.remove(paths[-1])
paths.extend(drives)
except ValueError:
pass
return paths
def strip_root_path(path: str, root_path: str) -> str:
"""Remove a root path from a path"""
if path == root_path:
return os.path.sep
elif path.startswith(root_path):
return path[len(root_path):]
return ''
def has_parent(path: str) -> bool:
"""Check if a path has a parent folder."""
return os.path.basename(path) != ''