|
10 | 10 | _DEFAULT_OPEN_ERROR_MESSAGE_PREFIX = "Failed to open file at path" |
11 | 11 |
|
12 | 12 |
|
| 13 | +def _normalize_error_prefix(prefix: object, *, default_prefix: str) -> str: |
| 14 | + normalized_default_prefix = default_prefix |
| 15 | + if not is_plain_string(normalized_default_prefix): |
| 16 | + normalized_default_prefix = _DEFAULT_OPEN_ERROR_MESSAGE_PREFIX |
| 17 | + else: |
| 18 | + try: |
| 19 | + stripped_default_prefix = normalized_default_prefix.strip() |
| 20 | + except Exception: |
| 21 | + stripped_default_prefix = _DEFAULT_OPEN_ERROR_MESSAGE_PREFIX |
| 22 | + if not is_plain_string(stripped_default_prefix) or not stripped_default_prefix: |
| 23 | + normalized_default_prefix = _DEFAULT_OPEN_ERROR_MESSAGE_PREFIX |
| 24 | + else: |
| 25 | + normalized_default_prefix = stripped_default_prefix |
| 26 | + if not is_plain_string(prefix): |
| 27 | + return normalized_default_prefix |
| 28 | + try: |
| 29 | + sanitized_prefix = "".join( |
| 30 | + "?" if ord(character) < 32 or ord(character) == 127 else character |
| 31 | + for character in prefix |
| 32 | + ) |
| 33 | + stripped_prefix = sanitized_prefix.strip() |
| 34 | + except Exception: |
| 35 | + stripped_prefix = normalized_default_prefix |
| 36 | + if not is_plain_string(stripped_prefix) or not stripped_prefix: |
| 37 | + return normalized_default_prefix |
| 38 | + return stripped_prefix |
| 39 | + |
| 40 | + |
13 | 41 | def format_file_path_for_error( |
14 | 42 | file_path: object, |
15 | 43 | *, |
@@ -44,27 +72,25 @@ def format_file_path_for_error( |
44 | 72 | return f"{sanitized_path[:truncated_length]}..." |
45 | 73 |
|
46 | 74 |
|
47 | | -def build_open_file_error_message(file_path: object, *, prefix: str) -> str: |
48 | | - normalized_prefix = prefix |
49 | | - if not is_plain_string(normalized_prefix): |
50 | | - normalized_prefix = _DEFAULT_OPEN_ERROR_MESSAGE_PREFIX |
51 | | - else: |
52 | | - try: |
53 | | - sanitized_prefix = "".join( |
54 | | - "?" if ord(character) < 32 or ord(character) == 127 else character |
55 | | - for character in normalized_prefix |
56 | | - ) |
57 | | - stripped_prefix = sanitized_prefix.strip() |
58 | | - except Exception: |
59 | | - stripped_prefix = _DEFAULT_OPEN_ERROR_MESSAGE_PREFIX |
60 | | - if not is_plain_string(stripped_prefix) or not stripped_prefix: |
61 | | - normalized_prefix = _DEFAULT_OPEN_ERROR_MESSAGE_PREFIX |
62 | | - else: |
63 | | - normalized_prefix = stripped_prefix |
| 75 | +def build_file_path_error_message( |
| 76 | + file_path: object, |
| 77 | + *, |
| 78 | + prefix: str, |
| 79 | + default_prefix: str, |
| 80 | +) -> str: |
| 81 | + normalized_prefix = _normalize_error_prefix(prefix, default_prefix=default_prefix) |
64 | 82 | file_path_display = format_file_path_for_error(file_path) |
65 | 83 | return f"{normalized_prefix}: {file_path_display}" |
66 | 84 |
|
67 | 85 |
|
| 86 | +def build_open_file_error_message(file_path: object, *, prefix: str) -> str: |
| 87 | + return build_file_path_error_message( |
| 88 | + file_path, |
| 89 | + prefix=prefix, |
| 90 | + default_prefix=_DEFAULT_OPEN_ERROR_MESSAGE_PREFIX, |
| 91 | + ) |
| 92 | + |
| 93 | + |
68 | 94 | def _normalize_file_path_text(file_path: Union[str, PathLike[str]]) -> str: |
69 | 95 | try: |
70 | 96 | normalized_path = os.fspath(file_path) |
|
0 commit comments