Skip to content

Commit 7af01cb

Browse files
Adopt shared display normalization in parsers
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 85b4c22 commit 7af01cb

File tree

3 files changed

+18
-47
lines changed

3 files changed

+18
-47
lines changed

hyperbrowser/client/managers/extension_utils.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from collections.abc import Mapping
22
from typing import Any, List
33

4+
from hyperbrowser.display_utils import normalize_display_text
45
from hyperbrowser.exceptions import HyperbrowserError
56
from hyperbrowser.models.extension import ExtensionResponse
67
from .list_parsing_utils import parse_mapping_list_items
78

89
_MAX_DISPLAYED_MISSING_KEYS = 20
910
_MAX_DISPLAYED_MISSING_KEY_LENGTH = 120
10-
_TRUNCATED_KEY_DISPLAY_SUFFIX = "... (truncated)"
1111

1212

1313
def _get_type_name(value: Any) -> str:
@@ -29,24 +29,15 @@ def _format_key_display(value: object) -> str:
2929
normalized_key = _safe_stringify_key(value)
3030
if type(normalized_key) is not str:
3131
raise TypeError("normalized key display must be a string")
32-
normalized_key = "".join(
33-
"?" if ord(character) < 32 or ord(character) == 127 else character
34-
for character in normalized_key
35-
).strip()
36-
if type(normalized_key) is not str:
37-
raise TypeError("normalized key display must be a string")
3832
except Exception:
3933
return "<unreadable key>"
34+
normalized_key = normalize_display_text(
35+
normalized_key,
36+
max_length=_MAX_DISPLAYED_MISSING_KEY_LENGTH,
37+
)
4038
if not normalized_key:
4139
return "<blank key>"
42-
if len(normalized_key) <= _MAX_DISPLAYED_MISSING_KEY_LENGTH:
43-
return normalized_key
44-
available_key_length = _MAX_DISPLAYED_MISSING_KEY_LENGTH - len(
45-
_TRUNCATED_KEY_DISPLAY_SUFFIX
46-
)
47-
if available_key_length <= 0:
48-
return _TRUNCATED_KEY_DISPLAY_SUFFIX
49-
return f"{normalized_key[:available_key_length]}{_TRUNCATED_KEY_DISPLAY_SUFFIX}"
40+
return normalized_key
5041

5142

5243
def _summarize_mapping_keys(mapping: Mapping[object, object]) -> str:

hyperbrowser/client/managers/session_utils.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
from typing import Any, List, Type, TypeVar
22

3+
from hyperbrowser.display_utils import normalize_display_text
34
from hyperbrowser.exceptions import HyperbrowserError
45
from hyperbrowser.models.session import SessionRecording
56
from .list_parsing_utils import parse_mapping_list_items
67
from .response_utils import parse_response_model
78

89
T = TypeVar("T")
910
_MAX_KEY_DISPLAY_LENGTH = 120
10-
_TRUNCATED_KEY_DISPLAY_SUFFIX = "... (truncated)"
1111

1212

1313
def _format_recording_key_display(key: str) -> str:
14-
try:
15-
normalized_key = "".join(
16-
"?" if ord(character) < 32 or ord(character) == 127 else character
17-
for character in key
18-
).strip()
19-
if type(normalized_key) is not str:
20-
raise TypeError("normalized recording key display must be a string")
21-
except Exception:
22-
return "<unreadable key>"
14+
normalized_key = normalize_display_text(
15+
key,
16+
max_length=_MAX_KEY_DISPLAY_LENGTH,
17+
)
2318
if not normalized_key:
2419
return "<blank key>"
25-
if len(normalized_key) <= _MAX_KEY_DISPLAY_LENGTH:
26-
return normalized_key
27-
available_length = _MAX_KEY_DISPLAY_LENGTH - len(_TRUNCATED_KEY_DISPLAY_SUFFIX)
28-
if available_length <= 0:
29-
return _TRUNCATED_KEY_DISPLAY_SUFFIX
30-
return f"{normalized_key[:available_length]}{_TRUNCATED_KEY_DISPLAY_SUFFIX}"
20+
return normalized_key
3121

3222

3323
def parse_session_response_model(

hyperbrowser/tools/__init__.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections.abc import Mapping as MappingABC
44
from typing import Any, Callable, Dict, Mapping
55

6+
from hyperbrowser.display_utils import normalize_display_text
67
from hyperbrowser.exceptions import HyperbrowserError
78
from hyperbrowser.models.agents.browser_use import StartBrowserUseTaskParams
89
from hyperbrowser.models.crawl import StartCrawlJobParams
@@ -26,7 +27,6 @@
2627
)
2728

2829
_MAX_KEY_DISPLAY_LENGTH = 120
29-
_TRUNCATED_KEY_DISPLAY_SUFFIX = "... (truncated)"
3030
_NON_OBJECT_CRAWL_PAGE_TYPES = (
3131
str,
3232
bytes,
@@ -53,23 +53,13 @@ def _has_declared_attribute(
5353

5454

5555
def _format_tool_param_key_for_error(key: str) -> str:
56-
try:
57-
normalized_key = "".join(
58-
"?" if ord(character) < 32 or ord(character) == 127 else character
59-
for character in key
60-
).strip()
61-
if type(normalized_key) is not str:
62-
raise TypeError("normalized tool key display must be a string")
63-
except Exception:
64-
return "<unreadable key>"
56+
normalized_key = normalize_display_text(
57+
key,
58+
max_length=_MAX_KEY_DISPLAY_LENGTH,
59+
)
6560
if not normalized_key:
6661
return "<blank key>"
67-
if len(normalized_key) <= _MAX_KEY_DISPLAY_LENGTH:
68-
return normalized_key
69-
available_length = _MAX_KEY_DISPLAY_LENGTH - len(_TRUNCATED_KEY_DISPLAY_SUFFIX)
70-
if available_length <= 0:
71-
return _TRUNCATED_KEY_DISPLAY_SUFFIX
72-
return f"{normalized_key[:available_length]}{_TRUNCATED_KEY_DISPLAY_SUFFIX}"
62+
return normalized_key
7363

7464

7565
def _copy_mapping_values_by_keys(

0 commit comments

Comments
 (0)