Skip to content

Commit

Permalink
fix: Improve OpenSearch import errors (#2939)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonLuttenberger authored Aug 22, 2024
1 parent 8de2695 commit 064d375
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions awswrangler/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"pyodbc": "sqlserver",
"gremlin_python": "gremlin",
"opensearchpy": "opensearch",
"jsonpath_ng": "opensearch",
"oracledb": "oracle",
}

Expand Down
10 changes: 8 additions & 2 deletions awswrangler/opensearch/_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@

from __future__ import annotations

from typing import Any, Collection, Mapping
from typing import TYPE_CHECKING, Any, Collection, Mapping

import awswrangler.pandas as pd
from awswrangler import _utils, exceptions
from awswrangler.opensearch._utils import _get_distribution, _is_serverless

opensearchpy = _utils.import_optional_dependency("opensearchpy")
if TYPE_CHECKING:
try:
import opensearchpy
except ImportError:
pass
else:
opensearchpy = _utils.import_optional_dependency("opensearchpy")


def _resolve_fields(row: Mapping[str, Any]) -> Mapping[str, Any]:
Expand Down
37 changes: 30 additions & 7 deletions awswrangler/opensearch/_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import ast
import json
import logging
from typing import Any, Generator, Iterable, Mapping, cast
from typing import TYPE_CHECKING, Any, Generator, Iterable, Mapping, cast

import boto3
import numpy as np
Expand All @@ -17,11 +17,31 @@
from awswrangler._utils import parse_path
from awswrangler.opensearch._utils import _get_distribution, _get_version_major, _is_serverless

progressbar = _utils.import_optional_dependency("progressbar")
opensearchpy = _utils.import_optional_dependency("opensearchpy")
if opensearchpy:
from jsonpath_ng import parse
from jsonpath_ng.exceptions import JsonPathParserError
if TYPE_CHECKING:
try:
import jsonpath_ng
except ImportError:
pass
else:
jsonpath_ng = _utils.import_optional_dependency("jsonpath_ng")


if TYPE_CHECKING:
try:
import opensearchpy
except ImportError:
pass
else:
opensearchpy = _utils.import_optional_dependency("opensearchpy")

if TYPE_CHECKING:
try:
import progressbar
except ImportError:
pass
else:
progressbar = _utils.import_optional_dependency("progressbar")


_logger: logging.Logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -95,9 +115,12 @@ def _file_line_generator(path: str, is_json: bool = False) -> Generator[Any, Non
yield line.strip()


@_utils.check_optional_dependency(jsonpath_ng, "jsonpath_ng")
def _get_documents_w_json_path(documents: list[Mapping[str, Any]], json_path: str) -> list[Any]:
from jsonpath_ng.exceptions import JsonPathParserError

try:
jsonpath_expression = parse(json_path)
jsonpath_expression = jsonpath_ng.parse(json_path)
except JsonPathParserError as e:
_logger.error("invalid json_path: %s", json_path)
raise e
Expand Down

0 comments on commit 064d375

Please sign in to comment.