-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathchecker.py
More file actions
33 lines (29 loc) · 1.09 KB
/
checker.py
File metadata and controls
33 lines (29 loc) · 1.09 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
import os
import os.path
from dataclasses import dataclass
@dataclass
class OperationOutcome:
success: bool
reason: str
def is_valid_directory(
path_to_directory: str, must_be_writable: bool = False
) -> OperationOutcome:
if (
path_to_directory
and os.path.isdir(path_to_directory)
and os.access(path_to_directory, os.R_OK)
):
if not must_be_writable or os.access(path_to_directory, os.W_OK):
return OperationOutcome(True, "")
else:
return OperationOutcome(False, f"Path {path_to_directory} is not writable.")
elif not path_to_directory:
return OperationOutcome(
False, "Variable `path_to_directory` must be a non-empty string"
)
elif not os.path.exists(path_to_directory):
return OperationOutcome(False, f"Path {path_to_directory} does not exist.")
elif not os.path.isdir(path_to_directory):
return OperationOutcome(False, f"Path {path_to_directory} is not a directory.")
else:
return OperationOutcome(False, f"`{path_to_directory}` is not a valid path.")