Skip to content
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

整理: EngineManifestJsondataclass へ変更 #1394

Merged
merged 4 commits into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
},
)