-
Notifications
You must be signed in to change notification settings - Fork 0
[06/10] refactor: centralize packaged reference data #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c604d3b
refactor: centralize packaged reference data
IceCodeNew a145dd6
fix: isolate cached reference data
IceCodeNew 4f4fb4d
perf: copy only selected reference values
IceCodeNew 391070c
perf: avoid copying string tuple resources
IceCodeNew 9844c5b
test: avoid patching imported deepcopy
IceCodeNew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| """Validated access to packaged JSON resources.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from copy import deepcopy | ||
| from functools import cache | ||
| from importlib import resources | ||
| from pathlib import PurePath | ||
| from typing import Any | ||
|
|
||
| from .. import data | ||
|
|
||
|
|
||
| class ReferenceDataError(RuntimeError): | ||
| """Raised when packaged domain reference data is missing or malformed.""" | ||
|
|
||
|
|
||
| def _validate_reference_data_filename(filename: str) -> None: | ||
| if PurePath(filename).name != filename or not filename.endswith(".json"): | ||
| raise ReferenceDataError("Reference data filename must identify one JSON file") | ||
|
|
||
|
|
||
| def load_reference_data(filename: str) -> dict[str, object]: | ||
| """Load and validate one packaged JSON reference-data object.""" | ||
| _validate_reference_data_filename(filename) | ||
| return deepcopy(_load_reference_data(filename)) | ||
|
|
||
|
|
||
| @cache | ||
| def _load_reference_data(filename: str) -> dict[str, object]: | ||
| try: | ||
| text = resources.files(data).joinpath(filename).read_text(encoding="utf-8") | ||
| value = json.loads(text) | ||
| except (FileNotFoundError, OSError, json.JSONDecodeError) as exc: | ||
| raise ReferenceDataError(f"Unable to load reference data: {filename}") from exc | ||
| if not isinstance(value, dict): | ||
| raise ReferenceDataError(f"Reference data root must be an object: {filename}") | ||
| return value | ||
|
|
||
|
|
||
| def _cached_reference_value(filename: str, *path: str) -> Any: | ||
| _validate_reference_data_filename(filename) | ||
| value: Any = _load_reference_data(filename) | ||
| try: | ||
| for key in path: | ||
| value = value[key] | ||
| except (KeyError, TypeError) as exc: | ||
| joined_path = ".".join(path) | ||
| raise ReferenceDataError(f"Missing reference data field: {filename}:{joined_path}") from exc | ||
| return value | ||
|
|
||
|
|
||
| def reference_value(filename: str, *path: str) -> Any: | ||
| """Read an isolated nested value from a packaged reference-data file.""" | ||
| return deepcopy(_cached_reference_value(filename, *path)) | ||
|
|
||
|
|
||
| def reference_string(filename: str, *path: str) -> str: | ||
| """Read a non-empty string from packaged reference data.""" | ||
| value = reference_value(filename, *path) | ||
| if not isinstance(value, str) or not value.strip(): | ||
| joined_path = ".".join(path) | ||
| raise ReferenceDataError(f"Reference data field must be a non-empty string: {filename}:{joined_path}") | ||
| return value | ||
|
|
||
|
|
||
| def reference_string_tuple(filename: str, *path: str) -> tuple[str, ...]: | ||
| """Read a non-empty string sequence from packaged reference data.""" | ||
| value = _cached_reference_value(filename, *path) | ||
| if not isinstance(value, list) or not value or not all(isinstance(item, str) and item.strip() for item in value): | ||
| joined_path = ".".join(path) | ||
| raise ReferenceDataError(f"Reference data field must be a non-empty string list: {filename}:{joined_path}") | ||
| return tuple(value) | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.