|
4 | 4 |
|
5 | 5 | The functions in this module make it easy to:
|
6 | 6 |
|
7 |
| -* check if a file exists and is writeable |
8 |
| -* check if a file and its parent directories exist and are writeable |
| 7 | +* check if a file exists and is writable |
| 8 | +* check if a file and its parent directories exist and are writable |
9 | 9 | * check if a file exists and is readable
|
10 | 10 | * check if a path exists and is a directory
|
11 | 11 | * open an appropriate reader or writer based on the file extension
|
|
47 | 47 | import io
|
48 | 48 | import os
|
49 | 49 | import sys
|
| 50 | +import warnings |
50 | 51 | from contextlib import contextmanager
|
51 | 52 | from pathlib import Path
|
52 | 53 | from typing import IO
|
@@ -90,41 +91,61 @@ def assert_directory_exists(path: Path) -> None:
|
90 | 91 |
|
91 | 92 |
|
92 | 93 | def assert_path_is_writeable(path: Path, parent_must_exist: bool = True) -> None:
|
93 |
| - """Asserts the following: |
94 |
| - If the file exists then it must also be writeable. Else if the path is not a file and |
95 |
| - parent_must_exist is true then assert that the parent directory exists and is writeable. |
96 |
| - Else if the path is not a directory and parent_must_exist is false then look at each parent |
97 |
| - directory until one is found that exists and is writeable. |
| 94 | + """ |
| 95 | + A deprecated alias for `assert_path_is_writable()`. |
| 96 | + """ |
| 97 | + warnings.warn( |
| 98 | + "assert_path_is_writeable is deprecated, use assert_path_is_writable instead", |
| 99 | + DeprecationWarning, |
| 100 | + ) |
| 101 | + |
| 102 | + assert_path_is_writable(path=path, parent_must_exist=parent_must_exist) |
| 103 | + |
| 104 | + |
| 105 | +def assert_path_is_writable(path: Path, parent_must_exist: bool = True) -> None: |
| 106 | + """ |
| 107 | + Assert that a filepath is writable. |
| 108 | +
|
| 109 | + Specifically: |
| 110 | + - If the file exists then it must also be writable. |
| 111 | + - Else if the path is not a file and `parent_must_exist` is true, then assert that the parent |
| 112 | + directory exists and is writable. |
| 113 | + - Else if the path is not a directory and `parent_must_exist` is false, then look at each parent |
| 114 | + directory until one is found that exists and is writable. |
98 | 115 |
|
99 | 116 | Args:
|
100 |
| - path: Path to check |
101 |
| - parent_must_exist: True/False the parent directory must exist, the default is True |
| 117 | + path: Path to check |
| 118 | + parent_must_exist: If True, the file's parent directory must exist. Otherwise, at least one |
| 119 | + directory in the path's components must exist. |
| 120 | +
|
| 121 | + Raises: |
| 122 | + AssertionError: If any of the above conditions are not met. |
102 | 123 |
|
103 | 124 | Example:
|
104 |
| - assert_path_is_writeable(path = Path("example.txt")) |
| 125 | + assert_path_is_writable(path = Path("example.txt")) |
105 | 126 | """
|
106 |
| - # If file exists, it must be writeable |
| 127 | + # If file exists, it must be writable |
107 | 128 | if path.exists():
|
108 | 129 | assert path.is_file() and os.access(
|
109 | 130 | path, os.W_OK
|
110 |
| - ), f"File exists but is not writeable: {path}" |
| 131 | + ), f"File exists but is not writable: {path}" |
111 | 132 |
|
112 | 133 | # Else if file doesnt exist and parent_must_exist is True then check
|
113 |
| - # that path.absolute().parent exists, is a directory and is writeable |
| 134 | + # that path.absolute().parent exists, is a directory and is writable |
114 | 135 | elif parent_must_exist:
|
115 | 136 | parent = path.absolute().parent
|
116 | 137 | assert (
|
117 | 138 | parent.exists() & parent.is_dir() & os.access(parent, os.W_OK)
|
118 |
| - ), f"Path does not exist and parent isn't extent/writable: {path}" |
| 139 | + ), f"Path does not exist and parent isn't extant/writable: {path}" |
119 | 140 |
|
120 | 141 | # Else if file doesn't exist and parent_must_exist is False, test parent until
|
121 |
| - # you find the first extent path, and check that it is a directory and is writeable. |
| 142 | + # you find the first extant path, and check that it is a directory and is writable. |
122 | 143 | else:
|
123 | 144 | for parent in path.parents:
|
124 | 145 | if parent.exists():
|
125 | 146 | assert os.access(
|
126 | 147 | parent, os.W_OK
|
127 |
| - ), f"File does not have a writeable parent directory: {path}" |
| 148 | + ), f"File does not have a writable parent directory: {path}" |
128 | 149 | return
|
129 | 150 | raise AssertionError(f"No parent directories exist for: {path}")
|
130 | 151 |
|
|
0 commit comments