Skip to content

Commit

Permalink
整理: SettingLoader リネーム (#1060)
Browse files Browse the repository at this point in the history
* refactor: `SettingLoader` リネーム

* fix: typo

* fix: メソッド名
  • Loading branch information
tarepan authored Feb 21, 2024
1 parent 709527b commit 346e1f7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
4 changes: 2 additions & 2 deletions build_util/make_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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, SettingLoader
from voicevox_engine.setting.SettingLoader import USER_SETTING_PATH, SettingHandler
from voicevox_engine.tts_pipeline.tts_engine import CoreAdapter
from voicevox_engine.utility.path_utility import engine_root

Expand Down Expand Up @@ -41,7 +41,7 @@ def generate_api_docs_html(schema: str) -> str:
tts_engines={"mock": MockTTSEngine()},
cores={"mock": CoreAdapter(mock_core)},
latest_core_version="mock",
setting_loader=SettingLoader(USER_SETTING_PATH),
setting_loader=SettingHandler(USER_SETTING_PATH),
preset_manager=PresetManager( # FIXME: impl MockPresetManager
preset_path=engine_root() / "presets.yaml",
),
Expand Down
12 changes: 6 additions & 6 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
from voicevox_engine.preset.PresetError import PresetError
from voicevox_engine.preset.PresetManager import PresetManager
from voicevox_engine.setting.Setting import CorsPolicyMode, Setting
from voicevox_engine.setting.SettingLoader import USER_SETTING_PATH, SettingLoader
from voicevox_engine.setting.SettingLoader import USER_SETTING_PATH, SettingHandler
from voicevox_engine.tts_pipeline.kana_converter import create_kana, parse_kana
from voicevox_engine.tts_pipeline.tts_engine import (
TTSEngine,
Expand Down Expand Up @@ -135,7 +135,7 @@ def generate_app(
tts_engines: dict[str, TTSEngine],
cores: dict[str, CoreAdapter],
latest_core_version: str,
setting_loader: SettingLoader,
setting_loader: SettingHandler,
preset_manager: PresetManager,
cancellable_engine: CancellableEngine | None = None,
root_dir: Optional[Path] = None,
Expand Down Expand Up @@ -1310,7 +1310,7 @@ def setting_get(request: Request) -> Response:
"""
設定ページを返します。
"""
settings = setting_loader.load_setting_file()
settings = setting_loader.load()

brand_name = engine_manifest_data.brand_name
cors_policy_mode = settings.cors_policy_mode
Expand Down Expand Up @@ -1348,7 +1348,7 @@ def setting_post(
)

# 更新した設定へ上書き
setting_loader.dump_setting_file(settings)
setting_loader.save(settings)

return Response(status_code=204)

Expand Down Expand Up @@ -1552,9 +1552,9 @@ def main() -> None:
if root_dir is None:
root_dir = engine_root()

setting_loader = SettingLoader(args.setting_file)
setting_loader = SettingHandler(args.setting_file)

settings = setting_loader.load_setting_file()
settings = setting_loader.load()

cors_policy_mode: CorsPolicyMode | None = args.cors_policy_mode
if cors_policy_mode is None:
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from voicevox_engine.core.core_initializer import initialize_cores
from voicevox_engine.preset.PresetManager import PresetManager
from voicevox_engine.setting.SettingLoader import SettingLoader
from voicevox_engine.setting.SettingLoader import SettingHandler
from voicevox_engine.tts_pipeline.tts_engine import make_tts_engines_from_cores
from voicevox_engine.utility.core_version_utility import get_latest_core_version

Expand All @@ -17,7 +17,7 @@ def app_params():
cores = initialize_cores(use_gpu=False, enable_mock=True)
tts_engines = make_tts_engines_from_cores(cores)
latest_core_version = get_latest_core_version(versions=list(tts_engines.keys()))
setting_loader = SettingLoader(Path("./not_exist.yaml"))
setting_loader = SettingHandler(Path("./not_exist.yaml"))
preset_manager = PresetManager( # FIXME: impl MockPresetManager
preset_path=Path("./presets.yaml"),
)
Expand Down
24 changes: 12 additions & 12 deletions test/setting/test_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest import TestCase

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


class TestSettingLoader(TestCase):
Expand All @@ -12,41 +12,41 @@ def setUp(self):
self.tmp_dir_path = Path(self.tmp_dir.name)

def test_loading_1(self):
setting_loader = SettingLoader(Path("not_exist.yaml"))
settings = setting_loader.load_setting_file()
setting_loader = SettingHandler(Path("not_exist.yaml"))
settings = setting_loader.load()

self.assertEqual(
settings.dict(),
{"allow_origin": None, "cors_policy_mode": CorsPolicyMode.localapps},
)

def test_loading_2(self):
setting_loader = SettingLoader(
setting_loader = SettingHandler(
setting_file_path=Path("test/setting/setting-test-load-1.yaml")
)
settings = setting_loader.load_setting_file()
settings = setting_loader.load()

self.assertEqual(
settings.dict(),
{"allow_origin": None, "cors_policy_mode": CorsPolicyMode.localapps},
)

def test_loading_3(self):
setting_loader = SettingLoader(
setting_loader = SettingHandler(
setting_file_path=Path("test/setting/setting-test-load-2.yaml")
)
settings = setting_loader.load_setting_file()
settings = setting_loader.load()

self.assertEqual(
settings.dict(),
{"allow_origin": None, "cors_policy_mode": "all"},
)

def test_loading_4(self):
setting_loader = SettingLoader(
setting_loader = SettingHandler(
setting_file_path=Path("test/setting/setting-test-load-3.yaml")
)
settings = setting_loader.load_setting_file()
settings = setting_loader.load()

self.assertEqual(
settings.dict(),
Expand All @@ -57,15 +57,15 @@ def test_loading_4(self):
)

def test_dump(self):
setting_loader = SettingLoader(
setting_loader = SettingHandler(
setting_file_path=Path(self.tmp_dir_path / "setting-test-dump.yaml")
)
settings = Setting(cors_policy_mode=CorsPolicyMode.localapps)
setting_loader.dump_setting_file(settings)
setting_loader.save(settings)

self.assertTrue(setting_loader.setting_file_path.is_file())
self.assertEqual(
setting_loader.load_setting_file().dict(),
setting_loader.load().dict(),
{"allow_origin": None, "cors_policy_mode": CorsPolicyMode.localapps},
)

Expand Down
9 changes: 5 additions & 4 deletions voicevox_engine/setting/SettingLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
USER_SETTING_PATH: Path = get_save_dir() / "setting.yml"


class SettingLoader:
class SettingHandler:
def __init__(self, setting_file_path: Path) -> None:
"""
設定ファイルの管理
Expand All @@ -19,8 +19,8 @@ def __init__(self, setting_file_path: Path) -> None:
"""
self.setting_file_path = setting_file_path

def load_setting_file(self) -> Setting:
# 設定値の読み込み
def load(self) -> Setting:
"""設定値をファイルから読み込む。"""
if not self.setting_file_path.is_file():
# 設定ファイルが存在しないためデフォルト値を取得
setting = {"allow_origin": None, "cors_policy_mode": "localapps"}
Expand All @@ -34,7 +34,8 @@ def load_setting_file(self) -> Setting:
allow_origin=setting["allow_origin"],
)

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

with open(self.setting_file_path, mode="w", encoding="utf-8") as f:
Expand Down

0 comments on commit 346e1f7

Please sign in to comment.