From 465d3698a326ff61167e34879b2f9656c08f4b84 Mon Sep 17 00:00:00 2001 From: tarepan Date: Tue, 14 May 2024 17:08:56 +0900 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86:=20utility=20=E3=83=A2?= =?UTF-8?q?=E3=82=B8=E3=83=A5=E3=83=BC=E3=83=AB=E3=81=AE=E5=89=8A=E9=99=A4?= =?UTF-8?q?=E3=83=BB=E7=A7=BB=E5=8B=95=E3=81=A8=E8=A9=B3=E7=B4=B0=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=20(#1228)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: `run_utility` を統合して廃止 * fix: lint * refactor: `core_utility` を統合して廃止 * refactor: 波形接続を `tts_pipeline` へ移動 * fix: 空ファイルを削除 * refactor: `mutex_utility` を統合して廃止 * refactor: docstring とパス実体コメントを追加 * Update voicevox_engine/utility/path_utility.py * fix: テストモジュールをリネーム --------- Co-authored-by: Hiroshiba --- run.py | 23 ++++++++++++++++++- ...re_utility.py => test_core_initializer.py} | 2 +- .../test_connect_base64_waves.py | 2 +- voicevox_engine/app/routers/tts_pipeline.py | 6 ++--- voicevox_engine/core/core_initializer.py | 9 +++++++- .../connect_base64_waves.py | 0 voicevox_engine/user_dict/user_dict.py | 19 ++++++++++++++- voicevox_engine/utility/core_utility.py | 8 ------- voicevox_engine/utility/mutex_utility.py | 19 --------------- voicevox_engine/utility/path_utility.py | 13 +++++++---- voicevox_engine/utility/run_utility.py | 23 ------------------- 11 files changed, 62 insertions(+), 62 deletions(-) rename test/{test_core_utility.py => test_core_initializer.py} (89%) rename test/{ => tts_pipeline}/test_connect_base64_waves.py (98%) rename voicevox_engine/{utility => tts_pipeline}/connect_base64_waves.py (100%) delete mode 100644 voicevox_engine/utility/core_utility.py delete mode 100644 voicevox_engine/utility/mutex_utility.py delete mode 100644 voicevox_engine/utility/run_utility.py diff --git a/run.py b/run.py index 66d93513c..99a6a5669 100644 --- a/run.py +++ b/run.py @@ -4,6 +4,7 @@ import multiprocessing import os import sys +import warnings from io import TextIOWrapper from pathlib import Path from typing import TypeVar @@ -20,7 +21,27 @@ from voicevox_engine.user_dict.user_dict import UserDictionary from voicevox_engine.utility.core_version_utility import get_latest_version from voicevox_engine.utility.path_utility import engine_root -from voicevox_engine.utility.run_utility import decide_boolean_from_env + + +def decide_boolean_from_env(env_name: str) -> bool: + """ + 環境変数からbool値を返す。 + + * 環境変数が"1"ならTrueを返す + * 環境変数が"0"か空白か存在しないならFalseを返す + * それ以外はwarningを出してFalseを返す + """ + env = os.getenv(env_name, default="") + if env == "1": + return True + elif env == "" or env == "0": + return False + else: + warnings.warn( + f"Invalid environment variable value: {env_name}={env}", + stacklevel=1, + ) + return False def set_output_log_utf8() -> None: diff --git a/test/test_core_utility.py b/test/test_core_initializer.py similarity index 89% rename from test/test_core_utility.py rename to test/test_core_initializer.py index f1df8f24d..972e3ff5b 100644 --- a/test/test_core_utility.py +++ b/test/test_core_initializer.py @@ -1,7 +1,7 @@ from unittest import TestCase from unittest.mock import patch -from voicevox_engine.utility.core_utility import get_half_logical_cores +from voicevox_engine.core.core_initializer import get_half_logical_cores class TestHalfLogicalCores(TestCase): diff --git a/test/test_connect_base64_waves.py b/test/tts_pipeline/test_connect_base64_waves.py similarity index 98% rename from test/test_connect_base64_waves.py rename to test/tts_pipeline/test_connect_base64_waves.py index 481428811..837282f7b 100644 --- a/test/test_connect_base64_waves.py +++ b/test/tts_pipeline/test_connect_base64_waves.py @@ -8,7 +8,7 @@ from numpy.typing import NDArray from soxr import resample -from voicevox_engine.utility.connect_base64_waves import ( +from voicevox_engine.tts_pipeline.connect_base64_waves import ( ConnectBase64WavesException, connect_base64_waves, ) diff --git a/voicevox_engine/app/routers/tts_pipeline.py b/voicevox_engine/app/routers/tts_pipeline.py index 8803db3df..e2d5c8de9 100644 --- a/voicevox_engine/app/routers/tts_pipeline.py +++ b/voicevox_engine/app/routers/tts_pipeline.py @@ -22,12 +22,12 @@ ) from voicevox_engine.preset.PresetError import PresetInputError, PresetInternalError from voicevox_engine.preset.PresetManager import PresetManager -from voicevox_engine.tts_pipeline.kana_converter import create_kana, parse_kana -from voicevox_engine.tts_pipeline.tts_engine import TTSEngine -from voicevox_engine.utility.connect_base64_waves import ( +from voicevox_engine.tts_pipeline.connect_base64_waves import ( ConnectBase64WavesException, connect_base64_waves, ) +from voicevox_engine.tts_pipeline.kana_converter import create_kana, parse_kana +from voicevox_engine.tts_pipeline.tts_engine import TTSEngine from voicevox_engine.utility.path_utility import delete_file diff --git a/voicevox_engine/core/core_initializer.py b/voicevox_engine/core/core_initializer.py index a6d0294b2..461709f7d 100644 --- a/voicevox_engine/core/core_initializer.py +++ b/voicevox_engine/core/core_initializer.py @@ -1,15 +1,22 @@ import json +import os import sys from pathlib import Path from ..tts_pipeline.tts_engine import CoreAdapter -from ..utility.core_utility import get_half_logical_cores from ..utility.path_utility import engine_root, get_save_dir from .core_wrapper import CoreWrapper, load_runtime_lib MOCK_VER = "0.0.0" +def get_half_logical_cores() -> int: + logical_cores = os.cpu_count() + if logical_cores is None: + return 0 + return logical_cores // 2 + + def initialize_cores( use_gpu: bool, voicelib_dirs: list[Path] | None = None, diff --git a/voicevox_engine/utility/connect_base64_waves.py b/voicevox_engine/tts_pipeline/connect_base64_waves.py similarity index 100% rename from voicevox_engine/utility/connect_base64_waves.py rename to voicevox_engine/tts_pipeline/connect_base64_waves.py diff --git a/voicevox_engine/user_dict/user_dict.py b/voicevox_engine/user_dict/user_dict.py index 63bcd4e61..b9b724519 100644 --- a/voicevox_engine/user_dict/user_dict.py +++ b/voicevox_engine/user_dict/user_dict.py @@ -2,17 +2,34 @@ import sys import threading import traceback +from collections.abc import Callable from pathlib import Path +from typing import Any, TypeVar from uuid import UUID, uuid4 import numpy as np import pyopenjtalk from ..model import UserDictWord, WordTypes -from ..utility.mutex_utility import mutex_wrapper from ..utility.path_utility import engine_root, get_save_dir from .part_of_speech_data import MAX_PRIORITY, MIN_PRIORITY, part_of_speech_data +F = TypeVar("F", bound=Callable[..., Any]) + + +def mutex_wrapper(lock: threading.Lock) -> Callable[[F], F]: + def wrap(f: F) -> F: + def func(*args: Any, **kw: Any) -> Any: + lock.acquire() + try: + return f(*args, **kw) + finally: + lock.release() + + return func # type: ignore + + return wrap + class UserDictInputError(Exception): """受け入れ不可能な入力値に起因するエラー""" diff --git a/voicevox_engine/utility/core_utility.py b/voicevox_engine/utility/core_utility.py deleted file mode 100644 index 993fa9ebf..000000000 --- a/voicevox_engine/utility/core_utility.py +++ /dev/null @@ -1,8 +0,0 @@ -import os - - -def get_half_logical_cores() -> int: - logical_cores = os.cpu_count() - if logical_cores is None: - return 0 - return logical_cores // 2 diff --git a/voicevox_engine/utility/mutex_utility.py b/voicevox_engine/utility/mutex_utility.py deleted file mode 100644 index 280e1e0e7..000000000 --- a/voicevox_engine/utility/mutex_utility.py +++ /dev/null @@ -1,19 +0,0 @@ -import threading -from collections.abc import Callable -from typing import Any, TypeVar - -F = TypeVar("F", bound=Callable[..., Any]) - - -def mutex_wrapper(lock: threading.Lock) -> Callable[[F], F]: - def wrap(f: F) -> F: - def func(*args: Any, **kw: Any) -> Any: - lock.acquire() - try: - return f(*args, **kw) - finally: - lock.release() - - return func # type: ignore - - return wrap diff --git a/voicevox_engine/utility/path_utility.py b/voicevox_engine/utility/path_utility.py index 0091cd52a..24619c51b 100644 --- a/voicevox_engine/utility/path_utility.py +++ b/voicevox_engine/utility/path_utility.py @@ -7,7 +7,9 @@ def engine_root() -> Path: - if is_development(): + """エンジンのルートディレクトリを指すパスを取得する。""" + if _is_development(): + # git レポジトリのルートを指している root_dir = Path(__file__).parents[2] # Nuitka/Pyinstallerでビルドされている場合 @@ -17,9 +19,9 @@ def engine_root() -> Path: return root_dir.resolve(strict=True) -def is_development() -> bool: +def _is_development() -> bool: """ - 開発版かどうか判定する関数 + 動作環境が開発版であるか否かを返す。 Nuitka/Pyinstallerでコンパイルされていない場合は開発環境とする。 """ # nuitkaビルドをした際はグローバルに__compiled__が含まれる @@ -34,10 +36,12 @@ def is_development() -> bool: def get_save_dir() -> Path: + """ファイルの保存先ディレクトリを指すパスを取得する。""" + # FIXME: ファイル保存場所をエンジン固有のIDが入ったものにする # FIXME: Windowsは`voicevox-engine/voicevox-engine`ディレクトリに保存されているので # `VOICEVOX/voicevox-engine`に変更する - if is_development(): + if _is_development(): app_name = "voicevox-engine-dev" else: app_name = "voicevox-engine" @@ -45,6 +49,7 @@ def get_save_dir() -> Path: def delete_file(file_path: str) -> None: + """指定されたファイルを削除する。""" try: os.remove(file_path) except OSError: diff --git a/voicevox_engine/utility/run_utility.py b/voicevox_engine/utility/run_utility.py deleted file mode 100644 index c61891b9f..000000000 --- a/voicevox_engine/utility/run_utility.py +++ /dev/null @@ -1,23 +0,0 @@ -import os -import warnings - - -def decide_boolean_from_env(env_name: str) -> bool: - """ - 環境変数からbool値を返す。 - - * 環境変数が"1"ならTrueを返す - * 環境変数が"0"か空白か存在しないならFalseを返す - * それ以外はwarningを出してFalseを返す - """ - env = os.getenv(env_name, default="") - if env == "1": - return True - elif env == "" or env == "0": - return False - else: - warnings.warn( - f"Invalid environment variable value: {env_name}={env}", - stacklevel=1, - ) - return False