|
6 | 6 | import ast
|
7 | 7 | import json
|
8 | 8 | import logging
|
9 |
| -from typing import Any, Generator, Iterable, Mapping, cast |
| 9 | +from typing import TYPE_CHECKING, Any, Generator, Iterable, Mapping, cast |
10 | 10 |
|
11 | 11 | import boto3
|
12 | 12 | import numpy as np
|
|
17 | 17 | from awswrangler._utils import parse_path
|
18 | 18 | from awswrangler.opensearch._utils import _get_distribution, _get_version_major, _is_serverless
|
19 | 19 |
|
20 |
| -progressbar = _utils.import_optional_dependency("progressbar") |
21 |
| -opensearchpy = _utils.import_optional_dependency("opensearchpy") |
22 |
| -if opensearchpy: |
23 |
| - from jsonpath_ng import parse |
24 |
| - from jsonpath_ng.exceptions import JsonPathParserError |
| 20 | +if TYPE_CHECKING: |
| 21 | + try: |
| 22 | + import jsonpath_ng |
| 23 | + except ImportError: |
| 24 | + pass |
| 25 | +else: |
| 26 | + jsonpath_ng = _utils.import_optional_dependency("jsonpath_ng") |
| 27 | + |
| 28 | + |
| 29 | +if TYPE_CHECKING: |
| 30 | + try: |
| 31 | + import opensearchpy |
| 32 | + except ImportError: |
| 33 | + pass |
| 34 | +else: |
| 35 | + opensearchpy = _utils.import_optional_dependency("opensearchpy") |
| 36 | + |
| 37 | +if TYPE_CHECKING: |
| 38 | + try: |
| 39 | + import progressbar |
| 40 | + except ImportError: |
| 41 | + pass |
| 42 | +else: |
| 43 | + progressbar = _utils.import_optional_dependency("progressbar") |
| 44 | + |
25 | 45 |
|
26 | 46 | _logger: logging.Logger = logging.getLogger(__name__)
|
27 | 47 |
|
@@ -95,9 +115,12 @@ def _file_line_generator(path: str, is_json: bool = False) -> Generator[Any, Non
|
95 | 115 | yield line.strip()
|
96 | 116 |
|
97 | 117 |
|
| 118 | +@_utils.check_optional_dependency(jsonpath_ng, "jsonpath_ng") |
98 | 119 | def _get_documents_w_json_path(documents: list[Mapping[str, Any]], json_path: str) -> list[Any]:
|
| 120 | + from jsonpath_ng.exceptions import JsonPathParserError |
| 121 | + |
99 | 122 | try:
|
100 |
| - jsonpath_expression = parse(json_path) |
| 123 | + jsonpath_expression = jsonpath_ng.parse(json_path) |
101 | 124 | except JsonPathParserError as e:
|
102 | 125 | _logger.error("invalid json_path: %s", json_path)
|
103 | 126 | raise e
|
|
0 commit comments