diff --git a/README.md b/README.md index 750585b..66a345b 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ async def main(): asyncio.run(main()) ``` +パスを受け取る引数(exe_path, output_path, text_path)には`str`だけでなく`pathlib.Path`(`Path`)も渡せます。 + # License MITライセンス 詳しくは[LICENSE](./LICENSE)を確認ください。 \ No newline at end of file diff --git a/test/test_voicepeak.py b/test/test_voicepeak.py index 92968eb..c5bc65c 100644 --- a/test/test_voicepeak.py +++ b/test/test_voicepeak.py @@ -4,6 +4,7 @@ import asyncio import os +from pathlib import Path from unittest.mock import AsyncMock import pytest @@ -99,3 +100,61 @@ async def test_terminal_error_is_decoded_as_runtime_error(monkeypatch): with pytest.raises(RuntimeError, match="narrator not found"): await client.get_emotion_list("unknown") + + +@pytest.mark.asyncio +async def test_init_accepts_path_for_exe_path(monkeypatch): + monkeypatch.setenv("ProgramFiles", os.path.dirname(__file__)) + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak(exe_path=Path(__file__)) + process = ProcessResult(b"Japanese Female 1\n") + create_subprocess = mock_process(monkeypatch, process) + + assert await client.get_narrator_name_list() == ("Japanese Female 1",) + create_subprocess.assert_awaited_once_with( + __file__, + "--list-narrator", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.asyncio +async def test_say_text_accepts_path_for_output_path(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", output_path=Path("output.wav")) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "-o", + "output.wav", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.asyncio +async def test_say_textfile_accepts_paths(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_textfile(Path("input.txt"), output_path=Path("output.wav")) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-t", + "input.txt", + "-o", + "output.wav", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) diff --git a/voicepeak_wrapper/voicepeak.py b/voicepeak_wrapper/voicepeak.py index 44da2da..260add3 100644 --- a/voicepeak_wrapper/voicepeak.py +++ b/voicepeak_wrapper/voicepeak.py @@ -5,6 +5,9 @@ import asyncio import os from dataclasses import dataclass +from pathlib import Path + +_DEFAULT_EXE_PATH = os.path.join(os.environ["ProgramFiles"], "VOICEPEAK", "voicepeak.exe") @dataclass @@ -16,13 +19,13 @@ class Narrator: class Voicepeak: def __init__( self, - exe_path: str = os.path.join(os.environ["ProgramFiles"], "VOICEPEAK", "voicepeak.exe"), + exe_path: str | Path = _DEFAULT_EXE_PATH, ): """ 標準のインストール先ではない場所にVOICEPEAKインストールした場合はexe_pathを指定してください。 Args: - exe_path (str, optional): voicepeak.exeへのパス。Defaultは標準のインストール先。 + exe_path (str | Path, optional): voicepeak.exeへのパス。Defaultは標準のインストール先。 """ if not os.path.exists(exe_path): @@ -31,7 +34,7 @@ def __init__( async def __async_run(self, args: list[str]) -> str: proc = await asyncio.create_subprocess_exec( - self.__exe_path, + os.fspath(self.__exe_path), *args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, @@ -48,8 +51,8 @@ async def __async_run(self, args: list[str]) -> str: def __make_say_command( self, text: str | None = None, - text_file: str | None = None, - output_path: str | None = None, + text_file: str | Path | None = None, + output_path: str | Path | None = None, narrator: Narrator | str | None = None, emotions: dict[str, int] | None = None, speed: int | None = None, @@ -58,19 +61,21 @@ def __make_say_command( args = [] match text, text_file: - case str(), str(): + case str(), str() | Path(): raise ValueError("textかtext_fileの一方のみ指定してください") case str(), None: args += ["-s", text] - case None, str(): - args += ["-t", text_file] + case None, str() as text_file_path: + args += ["-t", os.fspath(text_file_path)] + case None, Path() as text_file_path: + args += ["-t", os.fspath(text_file_path)] case None, None: raise ValueError("textまたはtext_fileが設定されている必要があります。") case _: raise ValueError("textまたはtext_fileが不正な値です。") if output_path is not None: - args += ["-o", output_path] + args += ["-o", os.fspath(output_path)] match narrator: case Narrator(): @@ -105,7 +110,7 @@ async def say_text( self, text: str, *, - output_path: str | None = None, + output_path: str | Path | None = None, narrator: Narrator | str | None = None, emotions: dict[str, int] | None = None, speed: int | None = None, @@ -117,7 +122,8 @@ async def say_text( Args: text (str): 読み上げるテキスト - output_path (str | None, optional): wavファイル出力先。指定しないとvoicepeak.exeと同じ階層にoutput.wavが生成される。 Defaults to None. + output_path (str | Path | None, optional): wavファイル出力先。 + 指定しないとvoicepeak.exeと同じ階層にoutput.wavが生成される。 Defaults to None. narrator (Narrator | str | None, optional): 読み上げを行うナレータの種類。Narrator型またはstr型の名前で指定する。 Defaults to None. @@ -140,9 +146,9 @@ async def say_text( async def say_textfile( self, - text_path: str, + text_path: str | Path, *, - output_path: str = "./output.wav", + output_path: str | Path = "./output.wav", narrator: Narrator | str | None = None, emotions: dict[str, int] | None = None, speed: int | None = None, @@ -152,9 +158,9 @@ async def say_textfile( テキストファイル内のテキストを読み上げたwavファイルを保存する。 Args: - text_path (str): 読み上げるテキストファイルのパス + text_path (str | Path): 読み上げるテキストファイルのパス - output_path (str , optional): wavファイル出力先。Defaultはoutput.wavが生成される。 + output_path (str | Path, optional): wavファイル出力先。Defaultはoutput.wavが生成される。 narrator (Narrator | str | None, optional): 読み上げを行うナレータの種類。Narrator型またはstr型の名前で指定する。 Defaults to None.