From 434a65ffb2beaddd0eae43a97fa0269625652171 Mon Sep 17 00:00:00 2001 From: tarepan Date: Wed, 19 Jun 2024 23:40:23 +0900 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86:=20`EngineManifestJson`=20?= =?UTF-8?q?=E3=82=92=20`dataclass`=20=E3=81=B8=E5=A4=89=E6=9B=B4=20(#1394)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: 内部用構造体 `EngineManifestJson` を dataclass へ変更 * fix: lint * add: dataclass の凍結を追加 --- voicevox_engine/engine_manifest.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/voicevox_engine/engine_manifest.py b/voicevox_engine/engine_manifest.py index 0ea321be3..9307416e5 100644 --- a/voicevox_engine/engine_manifest.py +++ b/voicevox_engine/engine_manifest.py @@ -7,14 +7,16 @@ import json from base64 import b64encode +from dataclasses import asdict, dataclass from pathlib import Path from typing import TypeAlias -from pydantic import BaseModel, Field +from pydantic import BaseModel, Field, TypeAdapter from pydantic.json_schema import SkipJsonSchema -class FeatureSupportJson(BaseModel): +@dataclass(frozen=True) +class FeatureSupportJson: """`engine_manifest.json` の機能サポート状況""" type: str @@ -22,7 +24,8 @@ class FeatureSupportJson(BaseModel): name: str -class SupportedFeaturesJson(BaseModel): +@dataclass(frozen=True) +class SupportedFeaturesJson: """`engine_manifest.json` のサポート機能一覧""" adjust_mora_pitch: FeatureSupportJson @@ -37,7 +40,8 @@ class SupportedFeaturesJson(BaseModel): manage_library: FeatureSupportJson -class EngineManifestJson(BaseModel): +@dataclass(frozen=True) +class EngineManifestJson: """`engine_manifest.json` のコンテンツ""" manifest_version: str @@ -57,6 +61,9 @@ class EngineManifestJson(BaseModel): supported_features: SupportedFeaturesJson +_manifest_json_adapter = TypeAdapter(EngineManifestJson) + + class UpdateInfo(BaseModel): """ エンジンのアップデート情報 @@ -135,7 +142,7 @@ def load_manifest(manifest_path: Path) -> EngineManifest: """エンジンマニフェストを指定ファイルから読み込む。""" root_dir = manifest_path.parent - manifest = EngineManifestJson.model_validate_json(manifest_path.read_bytes()) + manifest = _manifest_json_adapter.validate_json(manifest_path.read_bytes()) return EngineManifest( manifest_version=manifest.manifest_version, name=manifest.name, @@ -161,6 +168,6 @@ def load_manifest(manifest_path: Path) -> EngineManifest: ], supported_features={ key: item["value"] - for key, item in manifest.supported_features.model_dump().items() + for key, item in asdict(manifest.supported_features).items() }, )