Skip to content

Commit

Permalink
整理: EngineManifestJsondataclass へ変更 (#1394)
Browse files Browse the repository at this point in the history
* refactor: 内部用構造体 `EngineManifestJson` を dataclass へ変更

* fix: lint

* add: dataclass の凍結を追加
  • Loading branch information
tarepan authored Jun 19, 2024
1 parent 00fb774 commit 434a65f
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions voicevox_engine/engine_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@

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
value: bool
name: str


class SupportedFeaturesJson(BaseModel):
@dataclass(frozen=True)
class SupportedFeaturesJson:
"""`engine_manifest.json` のサポート機能一覧"""

adjust_mora_pitch: FeatureSupportJson
Expand All @@ -37,7 +40,8 @@ class SupportedFeaturesJson(BaseModel):
manage_library: FeatureSupportJson


class EngineManifestJson(BaseModel):
@dataclass(frozen=True)
class EngineManifestJson:
"""`engine_manifest.json` のコンテンツ"""

manifest_version: str
Expand All @@ -57,6 +61,9 @@ class EngineManifestJson(BaseModel):
supported_features: SupportedFeaturesJson


_manifest_json_adapter = TypeAdapter(EngineManifestJson)


class UpdateInfo(BaseModel):
"""
エンジンのアップデート情報
Expand Down Expand Up @@ -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,
Expand All @@ -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()
},
)

0 comments on commit 434a65f

Please sign in to comment.