-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from asfadmin/rew/pr-6293-cmr-storage
PR-6293 Add storage for querying CMR
- Loading branch information
Showing
19 changed files
with
882 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,73 @@ | ||
import dataclasses | ||
from dataclasses import dataclass, field | ||
from typing import Any | ||
|
||
from mandible import jsonpath | ||
|
||
from .exception import ContextValueError | ||
|
||
|
||
@dataclass | ||
class Context: | ||
files: list[dict[str, Any]] = field(default_factory=list) | ||
meta: list[str, Any] = field(default_factory=dict) | ||
meta: dict[str, Any] = field(default_factory=dict) | ||
|
||
|
||
@dataclass | ||
class ContextValue: | ||
"""A marker that should be replaced by a value from the Context""" | ||
|
||
path: str | ||
|
||
|
||
def replace_context_values( | ||
obj: Any, | ||
context: Context, | ||
) -> Any: | ||
return _replace_context_values(obj, dataclasses.asdict(context)) | ||
|
||
|
||
def _replace_context_values(obj: Any, context_dict: dict) -> Any: | ||
if isinstance(obj, ContextValue): | ||
try: | ||
result = jsonpath.get(context_dict, obj.path) | ||
except Exception as e: | ||
raise ContextValueError( | ||
f"jsonpath error for path {repr(obj.path)}: {e}", | ||
) from e | ||
|
||
if not result: | ||
raise ContextValueError( | ||
f"context missing value for path {repr(obj.path)}", | ||
) | ||
if len(result) > 1: | ||
raise ContextValueError( | ||
f"context path {repr(obj.path)} returned more than " | ||
f"one value", | ||
) | ||
|
||
return result[0] | ||
|
||
if isinstance(obj, dict): | ||
return { | ||
k: _replace_context_values(v, context_dict) | ||
for k, v in obj.items() | ||
} | ||
|
||
if isinstance(obj, list): | ||
return [_replace_context_values(v, context_dict) for v in obj] | ||
|
||
if dataclasses.is_dataclass(obj) and not isinstance(obj, type): | ||
replaced = dataclasses.replace( | ||
obj, | ||
**{ | ||
field_obj.name: _replace_context_values( | ||
getattr(obj, field_obj.name), | ||
context_dict, | ||
) | ||
for field_obj in dataclasses.fields(obj) | ||
}, | ||
) | ||
return replaced | ||
|
||
return obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.