Repository navigation
refactor(gt-i18n): registration interface #18
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| pypi/gt-i18n: patch | ||
| --- | ||
|
|
||
| refactor: string registration interface |
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
23 changes: 17 additions & 6 deletions
23
packages/gt-i18n/src/gt_i18n/translation_functions/_extract_variables.py
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 |
|---|---|---|
| @@ -1,16 +1,27 @@ | ||
| """Filter $-prefixed GT keys from options, returning user variables.""" | ||
| """Filter GT-reserved keys from options, returning user variables.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| _GT_RESERVED_KEYS = frozenset( | ||
| { | ||
| "_context", | ||
| "_id", | ||
| "_max_chars", | ||
| "__hash", | ||
| "__source", | ||
| "__fallback", | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def extract_variables(options: dict[str, object]) -> dict[str, object]: | ||
| """Return only user interpolation variables (non-$ keys). | ||
| """Return only user interpolation variables. | ||
|
|
||
| Args: | ||
| options: The full kwargs dict which may contain ``$context``, | ||
| ``$id``, ``$max_chars``, etc. | ||
| options: The full kwargs dict which may contain GT-reserved keys | ||
| like ``_context``, ``_id``, ``_max_chars``, etc. | ||
|
|
||
| Returns: | ||
| A new dict with only the user-provided variable keys. | ||
| A new dict with GT-reserved keys removed. | ||
| """ | ||
| return {k: v for k, v in options.items() if not k.startswith("$")} | ||
| return {k: v for k, v in options.items() if k not in _GT_RESERVED_KEYS} |
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,6 @@ | ||
| """Make the tests directory importable so helpers.py can be used.""" | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| sys.path.insert(0, str(Path(__file__).parent)) |
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,37 @@ | ||
| """Shared test helpers for converting JS-style fixture keys to Python-style.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import base64 | ||
| import json | ||
| from typing import Any | ||
|
|
||
| # Mapping from JS $-prefixed keys to Python _-prefixed keys | ||
| JS_TO_PY_KEY_MAP: dict[str, str] = { | ||
| "$context": "_context", | ||
| "$id": "_id", | ||
| "$max_chars": "_max_chars", | ||
| "$_source": "__source", | ||
| "$_hash": "__hash", | ||
| } | ||
|
|
||
|
|
||
| def convert_keys(d: dict[str, Any]) -> dict[str, Any]: | ||
| """Convert JS-style $-prefixed keys to Python-style _-prefixed keys.""" | ||
| return {JS_TO_PY_KEY_MAP.get(k, k): v for k, v in d.items()} | ||
|
|
||
|
|
||
| def convert_encoded_string(encoded: str) -> str: | ||
| """Re-encode a JS-style encoded message with Python-style keys. | ||
|
|
||
| Takes 'message:base64payload', decodes the payload, converts | ||
| $-prefixed keys to _-prefixed, and re-encodes. | ||
| """ | ||
| idx = encoded.rfind(":") | ||
| if idx == -1: | ||
| return encoded | ||
| msg_part = encoded[:idx] | ||
| payload = json.loads(base64.b64decode(encoded[idx + 1 :]).decode()) | ||
| converted = convert_keys(payload) | ||
| new_b64 = base64.b64encode(json.dumps(converted, separators=(",", ":")).encode()).decode() | ||
| return f"{msg_part}:{new_b64}" |
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
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.