Extract item template metadata policy
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
try:
|
||||
from .hardcore_action_metadata import normalize_hardcore_action_family
|
||||
from .hardcore_position_config import normalize_hardcore_position_family, normalize_hardcore_position_values
|
||||
except ImportError: # Allows local smoke tests from the repository root.
|
||||
from hardcore_action_metadata import normalize_hardcore_action_family
|
||||
from hardcore_position_config import normalize_hardcore_position_family, normalize_hardcore_position_values
|
||||
|
||||
|
||||
TEMPLATE_METADATA_KEYS = (
|
||||
"action_family",
|
||||
"action_type",
|
||||
"family",
|
||||
"position_family",
|
||||
"position_key",
|
||||
"position_keys",
|
||||
"formatter_hint",
|
||||
)
|
||||
|
||||
|
||||
def template_metadata(item: Any) -> dict[str, Any]:
|
||||
if not isinstance(item, dict):
|
||||
return {}
|
||||
return {key: item[key] for key in TEMPLATE_METADATA_KEYS if key in item}
|
||||
|
||||
|
||||
def template_position_family(metadata: dict[str, Any]) -> str:
|
||||
return normalize_hardcore_position_family(
|
||||
metadata.get("position_family") or metadata.get("family"),
|
||||
"",
|
||||
)
|
||||
|
||||
|
||||
def template_position_keys(metadata: dict[str, Any]) -> list[str]:
|
||||
keys: list[Any] = []
|
||||
if metadata.get("position_keys") is not None:
|
||||
raw_keys = metadata.get("position_keys")
|
||||
keys.extend(raw_keys if isinstance(raw_keys, list) else [raw_keys])
|
||||
if metadata.get("position_key") is not None:
|
||||
keys.append(metadata.get("position_key"))
|
||||
return normalize_hardcore_position_values(keys)
|
||||
|
||||
|
||||
def template_action_family(metadata: dict[str, Any]) -> str:
|
||||
return normalize_hardcore_action_family(metadata.get("action_family") or metadata.get("action_type"), "")
|
||||
|
||||
|
||||
def merge_position_keys(primary: list[str], fallback: list[str]) -> list[str]:
|
||||
merged: list[str] = []
|
||||
for key in [*primary, *fallback]:
|
||||
if key and key not in merged:
|
||||
merged.append(key)
|
||||
return merged
|
||||
|
||||
|
||||
def _position_key_slug(value: Any) -> str:
|
||||
return re.sub(r"[^a-z0-9]+", "_", str(value or "").strip().lower()).strip("_")
|
||||
|
||||
|
||||
def template_metadata_errors(metadata: dict[str, Any]) -> list[str]:
|
||||
errors: list[str] = []
|
||||
raw_action_family = metadata.get("action_family") or metadata.get("action_type")
|
||||
if raw_action_family and not template_action_family(metadata):
|
||||
errors.append(f"unknown action_family/action_type: {raw_action_family}")
|
||||
raw_position_family = metadata.get("position_family") or metadata.get("family")
|
||||
if raw_position_family and not template_position_family(metadata):
|
||||
errors.append(f"unknown position_family/family: {raw_position_family}")
|
||||
raw_position_keys = []
|
||||
if metadata.get("position_keys") is not None:
|
||||
values = metadata.get("position_keys")
|
||||
raw_position_keys.extend(values if isinstance(values, list) else [values])
|
||||
if metadata.get("position_key") is not None:
|
||||
raw_position_keys.append(metadata.get("position_key"))
|
||||
normalized_keys = template_position_keys(metadata)
|
||||
invalid_keys = [
|
||||
str(value)
|
||||
for value in raw_position_keys
|
||||
if str(value or "").strip()
|
||||
and str(value or "").strip() != "any"
|
||||
and _position_key_slug(value) not in normalized_keys
|
||||
]
|
||||
if invalid_keys:
|
||||
errors.append("unknown position key(s): " + ", ".join(invalid_keys))
|
||||
return errors
|
||||
Reference in New Issue
Block a user