|
7 | 7 | from hyperbrowser.type_utils import is_plain_string |
8 | 8 |
|
9 | 9 |
|
10 | | -def _validate_error_message_text(message_value: str, *, field_name: str) -> None: |
11 | | - if not is_plain_string(message_value): |
12 | | - raise HyperbrowserError(f"{field_name} must be a string") |
13 | | - try: |
14 | | - normalized_message = message_value.strip() |
15 | | - if not is_plain_string(normalized_message): |
16 | | - raise TypeError(f"normalized {field_name} must be a string") |
17 | | - is_empty = len(normalized_message) == 0 |
18 | | - except HyperbrowserError: |
19 | | - raise |
20 | | - except Exception as exc: |
21 | | - raise HyperbrowserError( |
22 | | - f"Failed to normalize {field_name}", |
23 | | - original_error=exc, |
24 | | - ) from exc |
25 | | - if is_empty: |
26 | | - raise HyperbrowserError(f"{field_name} must not be empty") |
27 | | - try: |
28 | | - contains_control_character = any( |
29 | | - ord(character) < 32 or ord(character) == 127 for character in message_value |
30 | | - ) |
31 | | - except HyperbrowserError: |
32 | | - raise |
33 | | - except Exception as exc: |
34 | | - raise HyperbrowserError( |
35 | | - f"Failed to validate {field_name} characters", |
36 | | - original_error=exc, |
37 | | - ) from exc |
38 | | - if contains_control_character: |
39 | | - raise HyperbrowserError(f"{field_name} must not contain control characters") |
40 | | - |
41 | | - |
42 | | -def ensure_existing_file_path( |
43 | | - file_path: Union[str, PathLike[str]], |
44 | | - *, |
45 | | - missing_file_message: str, |
46 | | - not_file_message: str, |
47 | | -) -> str: |
48 | | - _validate_error_message_text( |
49 | | - missing_file_message, |
50 | | - field_name="missing_file_message", |
51 | | - ) |
52 | | - _validate_error_message_text( |
53 | | - not_file_message, |
54 | | - field_name="not_file_message", |
55 | | - ) |
| 10 | +def _normalize_file_path_text(file_path: Union[str, PathLike[str]]) -> str: |
56 | 11 | try: |
57 | 12 | normalized_path = os.fspath(file_path) |
58 | 13 | except HyperbrowserError: |
@@ -105,6 +60,56 @@ def ensure_existing_file_path( |
105 | 60 | raise HyperbrowserError("file_path is invalid", original_error=exc) from exc |
106 | 61 | if contains_control_character: |
107 | 62 | raise HyperbrowserError("file_path must not contain control characters") |
| 63 | + return normalized_path |
| 64 | + |
| 65 | + |
| 66 | +def _validate_error_message_text(message_value: str, *, field_name: str) -> None: |
| 67 | + if not is_plain_string(message_value): |
| 68 | + raise HyperbrowserError(f"{field_name} must be a string") |
| 69 | + try: |
| 70 | + normalized_message = message_value.strip() |
| 71 | + if not is_plain_string(normalized_message): |
| 72 | + raise TypeError(f"normalized {field_name} must be a string") |
| 73 | + is_empty = len(normalized_message) == 0 |
| 74 | + except HyperbrowserError: |
| 75 | + raise |
| 76 | + except Exception as exc: |
| 77 | + raise HyperbrowserError( |
| 78 | + f"Failed to normalize {field_name}", |
| 79 | + original_error=exc, |
| 80 | + ) from exc |
| 81 | + if is_empty: |
| 82 | + raise HyperbrowserError(f"{field_name} must not be empty") |
| 83 | + try: |
| 84 | + contains_control_character = any( |
| 85 | + ord(character) < 32 or ord(character) == 127 for character in message_value |
| 86 | + ) |
| 87 | + except HyperbrowserError: |
| 88 | + raise |
| 89 | + except Exception as exc: |
| 90 | + raise HyperbrowserError( |
| 91 | + f"Failed to validate {field_name} characters", |
| 92 | + original_error=exc, |
| 93 | + ) from exc |
| 94 | + if contains_control_character: |
| 95 | + raise HyperbrowserError(f"{field_name} must not contain control characters") |
| 96 | + |
| 97 | + |
| 98 | +def ensure_existing_file_path( |
| 99 | + file_path: Union[str, PathLike[str]], |
| 100 | + *, |
| 101 | + missing_file_message: str, |
| 102 | + not_file_message: str, |
| 103 | +) -> str: |
| 104 | + _validate_error_message_text( |
| 105 | + missing_file_message, |
| 106 | + field_name="missing_file_message", |
| 107 | + ) |
| 108 | + _validate_error_message_text( |
| 109 | + not_file_message, |
| 110 | + field_name="not_file_message", |
| 111 | + ) |
| 112 | + normalized_path = _normalize_file_path_text(file_path) |
108 | 113 | try: |
109 | 114 | path_exists = bool(os.path.exists(normalized_path)) |
110 | 115 | except HyperbrowserError: |
@@ -138,19 +143,7 @@ def open_binary_file( |
138 | 143 | open_error_message, |
139 | 144 | field_name="open_error_message", |
140 | 145 | ) |
141 | | - try: |
142 | | - normalized_path = os.fspath(file_path) |
143 | | - except HyperbrowserError: |
144 | | - raise |
145 | | - except TypeError as exc: |
146 | | - raise HyperbrowserError( |
147 | | - "file_path must be a string or os.PathLike object", |
148 | | - original_error=exc, |
149 | | - ) from exc |
150 | | - except Exception as exc: |
151 | | - raise HyperbrowserError("file_path is invalid", original_error=exc) from exc |
152 | | - if not is_plain_string(normalized_path): |
153 | | - raise HyperbrowserError("file_path must resolve to a string path") |
| 146 | + normalized_path = _normalize_file_path_text(file_path) |
154 | 147 | try: |
155 | 148 | with open(normalized_path, "rb") as file_obj: |
156 | 149 | yield file_obj |
|
0 commit comments