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

Function prepare_output_path

mogen/utils/path_utils.py:120–187  ·  view source on GitHub ↗

Check output folder or file. Args: output_path (str): could be folder or file. allowed_suffix (List[str], optional): Check the suffix of `output_path`. If folder, should be [] or ['']. If could both be folder or file, should be [suffixs..., ''].

(output_path: str,
                        allowed_suffix: List[str] = [],
                        tag: str = 'output file',
                        path_type: Literal['file', 'dir', 'auto'] = 'auto',
                        overwrite: bool = True)

Source from the content-addressed store, hash-verified

118
119
120def prepare_output_path(output_path: str,
121 allowed_suffix: List[str] = [],
122 tag: str = 'output file',
123 path_type: Literal['file', 'dir', 'auto'] = 'auto',
124 overwrite: bool = True) -> None:
125 """Check output folder or file.
126
127 Args:
128 output_path (str): could be folder or file.
129 allowed_suffix (List[str], optional):
130 Check the suffix of `output_path`. If folder, should be [] or [''].
131 If could both be folder or file, should be [suffixs..., ''].
132 Defaults to [].
133 tag (str, optional): The `string` tag to specify the output type.
134 Defaults to 'output file'.
135 path_type (Literal[, optional):
136 Choose `file` for file and `dir` for folder.
137 Choose `auto` if allowed to be both.
138 Defaults to 'auto'.
139 overwrite (bool, optional):
140 Whether overwrite the existing file or folder.
141 Defaults to True.
142
143 Raises:
144 FileNotFoundError: suffix does not match.
145 FileExistsError: file or folder already exists and `overwrite` is
146 False.
147
148 Returns:
149 None
150 """
151 if path_type.lower() == 'dir':
152 allowed_suffix = []
153 exist_result = check_path_existence(output_path, path_type=path_type)
154 if exist_result == Existence.MissingParent:
155 warnings.warn(
156 f'The parent folder of {tag} does not exist: {output_path},' +
157 f' will make dir {Path(output_path).parent.absolute().__str__()}')
158 os.makedirs(Path(output_path).parent.absolute().__str__(),
159 exist_ok=True)
160
161 elif exist_result == Existence.DirectoryNotExist:
162 os.mkdir(output_path)
163 print(f'Making directory {output_path} for saving results.')
164 elif exist_result == Existence.FileNotExist:
165 suffix_matched = \
166 check_path_suffix(output_path, allowed_suffix=allowed_suffix)
167 if not suffix_matched:
168 raise FileNotFoundError(
169 f'The {tag} should be {", ".join(allowed_suffix)}: '
170 f'{output_path}.')
171 elif exist_result == Existence.FileExist:
172 if not overwrite:
173 raise FileExistsError(
174 f'{output_path} exists (set overwrite = True to overwrite).')
175 else:
176 print(f'Overwriting {output_path}.')
177 elif exist_result == Existence.DirectoryExistEmpty:

Callers

nothing calls this directly

Calls 2

check_path_existenceFunction · 0.85
check_path_suffixFunction · 0.85

Tested by

no test coverage detected