Skip to content

Commit

Permalink
整理: utility モジュールの削除・移動と詳細追加 (#1228)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
tarepan and Hiroshiba authored May 14, 2024
1 parent 8398406 commit 465d369
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 62 deletions.
23 changes: 22 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import multiprocessing
import os
import sys
import warnings
from io import TextIOWrapper
from pathlib import Path
from typing import TypeVar
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
6 changes: 3 additions & 3 deletions voicevox_engine/app/routers/tts_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
9 changes: 8 additions & 1 deletion voicevox_engine/core/core_initializer.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
19 changes: 18 additions & 1 deletion voicevox_engine/user_dict/user_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""受け入れ不可能な入力値に起因するエラー"""
Expand Down
8 changes: 0 additions & 8 deletions voicevox_engine/utility/core_utility.py

This file was deleted.

19 changes: 0 additions & 19 deletions voicevox_engine/utility/mutex_utility.py

This file was deleted.

13 changes: 9 additions & 4 deletions voicevox_engine/utility/path_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@


def engine_root() -> Path:
if is_development():
"""エンジンのルートディレクトリを指すパスを取得する。"""
if _is_development():
# git レポジトリのルートを指している
root_dir = Path(__file__).parents[2]

# Nuitka/Pyinstallerでビルドされている場合
Expand All @@ -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__が含まれる
Expand All @@ -34,17 +36,20 @@ 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"
return Path(user_data_dir(app_name))


def delete_file(file_path: str) -> None:
"""指定されたファイルを削除する。"""
try:
os.remove(file_path)
except OSError:
Expand Down
23 changes: 0 additions & 23 deletions voicevox_engine/utility/run_utility.py

This file was deleted.

0 comments on commit 465d369

Please sign in to comment.