|
11 | 11 | from pathlib import Path |
12 | 12 | from typing import Any, Callable |
13 | 13 |
|
| 14 | +from quant_platform_kit.common.strategy_plugin_artifacts import ( |
| 15 | + cache_path_for_remote_artifact, |
| 16 | + download_gcs_object, |
| 17 | + materialize_local_or_gcs_artifact, |
| 18 | + parse_gcs_uri, |
| 19 | +) |
| 20 | + |
14 | 21 | PLUGIN_CRISIS_RESPONSE_SHADOW = "crisis_response_shadow" |
15 | 22 | PLUGIN_MARKET_REGIME_CONTROL = "market_regime_control" |
16 | 23 | PLUGIN_MACRO_RISK_GOVERNOR = "macro_risk_governor" |
@@ -1237,42 +1244,23 @@ def _sanitize_key_part(value: Any) -> str: |
1237 | 1244 |
|
1238 | 1245 |
|
1239 | 1246 | def _materialize_artifact_path(reference: str, *, client_factory: Any = None) -> tuple[Path, dict[str, str | None]]: |
1240 | | - raw_reference = _required_string(reference, field_name="reference") |
1241 | | - if not raw_reference.startswith("gs://"): |
1242 | | - return Path(raw_reference).expanduser(), {"source_uri": None, "local_path": raw_reference} |
1243 | | - |
1244 | | - local_path = _cache_path_for_remote_artifact(raw_reference) |
1245 | | - _download_gcs_object(raw_reference, local_path, client_factory=client_factory) |
1246 | | - return local_path, {"source_uri": raw_reference, "local_path": str(local_path)} |
| 1247 | + return materialize_local_or_gcs_artifact( |
| 1248 | + reference, |
| 1249 | + cache_dir=DEFAULT_PLUGIN_ARTIFACT_CACHE_DIR, |
| 1250 | + client_factory=client_factory, |
| 1251 | + ) |
1247 | 1252 |
|
1248 | 1253 |
|
1249 | 1254 | def _download_gcs_object(uri: str, destination: Path, *, client_factory: Any = None) -> None: |
1250 | | - if client_factory is None: |
1251 | | - try: |
1252 | | - from google.cloud import storage # type: ignore |
1253 | | - except ImportError as exc: |
1254 | | - raise RuntimeError("google-cloud-storage is required for GCS strategy plugin artifacts") from exc |
1255 | | - client_factory = storage.Client |
1256 | | - bucket_name, object_name = _parse_gcs_uri(uri) |
1257 | | - destination.parent.mkdir(parents=True, exist_ok=True) |
1258 | | - client = client_factory() |
1259 | | - client.bucket(bucket_name).blob(object_name).download_to_filename(str(destination)) |
| 1255 | + download_gcs_object(uri, destination, client_factory=client_factory) |
1260 | 1256 |
|
1261 | 1257 |
|
1262 | 1258 | def _parse_gcs_uri(uri: str) -> tuple[str, str]: |
1263 | | - raw_uri = str(uri or "").strip() |
1264 | | - if not raw_uri.startswith("gs://"): |
1265 | | - raise ValueError(f"Unsupported GCS URI: {raw_uri}") |
1266 | | - bucket_name, _, object_name = raw_uri[5:].partition("/") |
1267 | | - if not bucket_name or not object_name: |
1268 | | - raise ValueError(f"Invalid GCS URI: {raw_uri}") |
1269 | | - return bucket_name, object_name |
| 1259 | + return parse_gcs_uri(uri) |
1270 | 1260 |
|
1271 | 1261 |
|
1272 | 1262 | def _cache_path_for_remote_artifact(reference: str) -> Path: |
1273 | | - digest = hashlib.sha256(reference.encode("utf-8")).hexdigest()[:16] |
1274 | | - leaf_name = Path(reference).name or "latest_signal.json" |
1275 | | - return DEFAULT_PLUGIN_ARTIFACT_CACHE_DIR / digest / leaf_name |
| 1263 | + return cache_path_for_remote_artifact(reference, cache_dir=DEFAULT_PLUGIN_ARTIFACT_CACHE_DIR) |
1276 | 1264 |
|
1277 | 1265 |
|
1278 | 1266 | def _as_bool(value: Any, *, default: bool = False) -> bool: |
|
0 commit comments