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

整理: モジュール SettingSettingLoader を統合 #1264

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion build_util/make_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from voicevox_engine.dev.core.mock import MockCoreWrapper
from voicevox_engine.dev.tts_engine.mock import MockTTSEngine
from voicevox_engine.preset.PresetManager import PresetManager
from voicevox_engine.setting.SettingLoader import USER_SETTING_PATH, SettingHandler
from voicevox_engine.setting.Setting import USER_SETTING_PATH, SettingHandler
from voicevox_engine.tts_pipeline.tts_engine import TTSEngineManager
from voicevox_engine.user_dict.user_dict import UserDictionary
from voicevox_engine.utility.path_utility import engine_root
Expand Down
7 changes: 5 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
from voicevox_engine.cancellable_engine import CancellableEngine
from voicevox_engine.core.core_initializer import initialize_cores
from voicevox_engine.preset.PresetManager import PresetManager
from voicevox_engine.setting.Setting import CorsPolicyMode
from voicevox_engine.setting.SettingLoader import USER_SETTING_PATH, SettingHandler
from voicevox_engine.setting.Setting import (
USER_SETTING_PATH,
CorsPolicyMode,
SettingHandler,
)
from voicevox_engine.tts_pipeline.tts_engine import make_tts_engines_from_cores
from voicevox_engine.user_dict.user_dict import UserDictionary
from voicevox_engine.utility.path_utility import engine_root
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from voicevox_engine.app.application import generate_app
from voicevox_engine.core.core_initializer import initialize_cores
from voicevox_engine.preset.PresetManager import PresetManager
from voicevox_engine.setting.SettingLoader import SettingHandler
from voicevox_engine.setting.Setting import SettingHandler
from voicevox_engine.tts_pipeline.tts_engine import make_tts_engines_from_cores
from voicevox_engine.user_dict.user_dict import UserDictionary

Expand Down
3 changes: 1 addition & 2 deletions test/setting/test_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

from pydantic import ValidationError

from voicevox_engine.setting.Setting import CorsPolicyMode, Setting
from voicevox_engine.setting.SettingLoader import SettingHandler
from voicevox_engine.setting.Setting import CorsPolicyMode, Setting, SettingHandler


class TestSettingLoader(TestCase):
Expand Down
3 changes: 1 addition & 2 deletions voicevox_engine/app/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
from voicevox_engine.library_manager import LibraryManager
from voicevox_engine.metas.MetasStore import MetasStore
from voicevox_engine.preset.PresetManager import PresetManager
from voicevox_engine.setting.Setting import CorsPolicyMode
from voicevox_engine.setting.SettingLoader import SettingHandler
from voicevox_engine.setting.Setting import CorsPolicyMode, SettingHandler
from voicevox_engine.tts_pipeline.tts_engine import TTSEngineManager
from voicevox_engine.user_dict.user_dict import UserDictionary
from voicevox_engine.utility.path_utility import engine_root, get_save_dir
Expand Down
3 changes: 1 addition & 2 deletions voicevox_engine/app/routers/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from fastapi.templating import Jinja2Templates

from voicevox_engine.engine_manifest.EngineManifest import EngineManifest
from voicevox_engine.setting.Setting import CorsPolicyMode, Setting
from voicevox_engine.setting.SettingLoader import SettingHandler
from voicevox_engine.setting.Setting import CorsPolicyMode, Setting, SettingHandler
from voicevox_engine.utility.path_utility import engine_root

from ..dependencies import check_disabled_mutable_api
Expand Down
44 changes: 44 additions & 0 deletions voicevox_engine/setting/Setting.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from enum import Enum
from pathlib import Path

import yaml
from pydantic import BaseModel, Field

from ..utility.path_utility import get_save_dir


class CorsPolicyMode(str, Enum):
"""
Expand All @@ -19,3 +23,43 @@ class Setting(BaseModel):

cors_policy_mode: CorsPolicyMode = Field(title="リソース共有ポリシー")
allow_origin: str | None = Field(title="許可するオリジン")


USER_SETTING_PATH: Path = get_save_dir() / "setting.yml"


class SettingHandler:
def __init__(self, setting_file_path: Path) -> None:
"""
設定ファイルの管理
Parameters
----------
setting_file_path : Path
設定ファイルのパス。存在しない場合はデフォルト値を設定。
"""
self.setting_file_path = setting_file_path

def load(self) -> Setting:
"""設定値をファイルから読み込む。"""
if not self.setting_file_path.is_file():
# 設定ファイルが存在しないためデフォルト値を取得
setting = {"allow_origin": None, "cors_policy_mode": "localapps"}
else:
# 指定された設定ファイルから値を取得
# FIXME: 型チェックと例外処理を追加する
setting = yaml.safe_load(self.setting_file_path.read_text(encoding="utf-8"))

return Setting(
cors_policy_mode=setting["cors_policy_mode"],
allow_origin=setting["allow_origin"],
)

def save(self, settings: Setting) -> None:
"""設定値をファイルへ書き込む。"""
settings_dict = settings.dict()

if isinstance(settings_dict["cors_policy_mode"], Enum):
settings_dict["cors_policy_mode"] = settings_dict["cors_policy_mode"].value

with open(self.setting_file_path, mode="w", encoding="utf-8") as f:
yaml.safe_dump(settings_dict, f)
46 changes: 0 additions & 46 deletions voicevox_engine/setting/SettingLoader.py

This file was deleted.