Check whether a file or a directory exists at the expected path. Args: path_str (str): Path to check. path_type (Literal[, optional): What kind of file do we expect at the path. Choose among `file`, `dir`, `auto`. Defaults to 'auto
(
path_str: str,
path_type: Literal['file', 'dir', 'auto'] = 'auto',
)
| 62 | |
| 63 | |
| 64 | def check_path_existence( |
| 65 | path_str: str, |
| 66 | path_type: Literal['file', 'dir', 'auto'] = 'auto', |
| 67 | ) -> Existence: |
| 68 | """Check whether a file or a directory exists at the expected path. |
| 69 | |
| 70 | Args: |
| 71 | path_str (str): |
| 72 | Path to check. |
| 73 | path_type (Literal[, optional): |
| 74 | What kind of file do we expect at the path. |
| 75 | Choose among `file`, `dir`, `auto`. |
| 76 | Defaults to 'auto'. path_type = path_type.lower() |
| 77 | |
| 78 | Raises: |
| 79 | KeyError: if `path_type` conflicts with `path_str` |
| 80 | |
| 81 | Returns: |
| 82 | Existence: |
| 83 | 0. FileExist: file at path_str exists. |
| 84 | 1. DirectoryExistEmpty: folder at path exists and. |
| 85 | 2. DirectoryExistNotEmpty: folder at path_str exists and not empty. |
| 86 | 3. MissingParent: its parent doesn't exist. |
| 87 | 4. DirectoryNotExist: expect a folder at path_str, but not found. |
| 88 | 5. FileNotExist: expect a file at path_str, but not found. |
| 89 | """ |
| 90 | path_type = path_type.lower() |
| 91 | assert path_type in {'file', 'dir', 'auto'} |
| 92 | pathinfo = Path(path_str) |
| 93 | if not pathinfo.parent.is_dir(): |
| 94 | return Existence.MissingParent |
| 95 | suffix = pathinfo.suffix.lower() |
| 96 | if path_type == 'dir' or\ |
| 97 | path_type == 'auto' and suffix == '': |
| 98 | if pathinfo.is_dir(): |
| 99 | if len(os.listdir(path_str)) == 0: |
| 100 | return Existence.DirectoryExistEmpty |
| 101 | else: |
| 102 | return Existence.DirectoryExistNotEmpty |
| 103 | else: |
| 104 | return Existence.DirectoryNotExist |
| 105 | elif path_type == 'file' or\ |
| 106 | path_type == 'auto' and suffix != '': |
| 107 | if pathinfo.is_file(): |
| 108 | return Existence.FileExist |
| 109 | elif pathinfo.is_dir(): |
| 110 | if len(os.listdir(path_str)) == 0: |
| 111 | return Existence.DirectoryExistEmpty |
| 112 | else: |
| 113 | return Existence.DirectoryExistNotEmpty |
| 114 | if path_str.endswith('/'): |
| 115 | return Existence.DirectoryNotExist |
| 116 | else: |
| 117 | return Existence.FileNotExist |
| 118 | |
| 119 | |
| 120 | def prepare_output_path(output_path: str, |
no outgoing calls
no test coverage detected