MCPcopy Create free account
hub / github.com/MotrixLab/FineMoGen / check_path_existence

Function check_path_existence

mogen/utils/path_utils.py:64–117  ·  view source on GitHub ↗

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',
)

Source from the content-addressed store, hash-verified

62
63
64def 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
120def prepare_output_path(output_path: str,

Callers 2

prepare_output_pathFunction · 0.85
check_input_pathFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected