Check whether the suffix of the path is allowed. Args: path_str (str): Path to check. allowed_suffix (List[str], optional): What extension names are allowed. Offer a list like ['.jpg', ',jpeg']. When it's [], all will be received.
(path_str: str,
allowed_suffix: Union[str, List[str]] = '')
| 11 | |
| 12 | |
| 13 | def check_path_suffix(path_str: str, |
| 14 | allowed_suffix: Union[str, List[str]] = '') -> bool: |
| 15 | """Check whether the suffix of the path is allowed. |
| 16 | |
| 17 | Args: |
| 18 | path_str (str): |
| 19 | Path to check. |
| 20 | allowed_suffix (List[str], optional): |
| 21 | What extension names are allowed. |
| 22 | Offer a list like ['.jpg', ',jpeg']. |
| 23 | When it's [], all will be received. |
| 24 | Use [''] then directory is allowed. |
| 25 | Defaults to []. |
| 26 | |
| 27 | Returns: |
| 28 | bool: |
| 29 | True: suffix test passed |
| 30 | False: suffix test failed |
| 31 | """ |
| 32 | if isinstance(allowed_suffix, str): |
| 33 | allowed_suffix = [allowed_suffix] |
| 34 | pathinfo = Path(path_str) |
| 35 | suffix = pathinfo.suffix.lower() |
| 36 | if len(allowed_suffix) == 0: |
| 37 | return True |
| 38 | if pathinfo.is_dir(): |
| 39 | if '' in allowed_suffix: |
| 40 | return True |
| 41 | else: |
| 42 | return False |
| 43 | else: |
| 44 | for index, tmp_suffix in enumerate(allowed_suffix): |
| 45 | if not tmp_suffix.startswith('.'): |
| 46 | tmp_suffix = '.' + tmp_suffix |
| 47 | allowed_suffix[index] = tmp_suffix.lower() |
| 48 | if suffix in allowed_suffix: |
| 49 | return True |
| 50 | else: |
| 51 | return False |
| 52 | |
| 53 | |
| 54 | class Existence(Enum): |
no outgoing calls
no test coverage detected