From ebb81762ef3ccc3f3d031b273845dc1c92dd0f46 Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Sat, 8 Aug 2026 00:11:55 +0900 Subject: [PATCH 1/3] Support pathlib.Path inputs --- README.md | 2 ++ test/test_voicepeak.py | 59 ++++++++++++++++++++++++++++++++++ voicepeak_wrapper/voicepeak.py | 38 +++++++++++++--------- 3 files changed, 84 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 750585b..a977486 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`も渡せます。 + # 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..77c56f3 100644 --- a/voicepeak_wrapper/voicepeak.py +++ b/voicepeak_wrapper/voicepeak.py @@ -5,6 +5,11 @@ import asyncio import os from dataclasses import dataclass +from typing import TypeAlias + +_StrPath: TypeAlias = str | os.PathLike[str] + +_DEFAULT_EXE_PATH = os.path.join(os.environ["ProgramFiles"], "VOICEPEAK", "voicepeak.exe") @dataclass @@ -16,13 +21,13 @@ class Narrator: class Voicepeak: def __init__( self, - exe_path: str = os.path.join(os.environ["ProgramFiles"], "VOICEPEAK", "voicepeak.exe"), + exe_path: _StrPath = _DEFAULT_EXE_PATH, ): """ 標準のインストール先ではない場所にVOICEPEAKインストールした場合はexe_pathを指定してください。 Args: - exe_path (str, optional): voicepeak.exeへのパス。Defaultは標準のインストール先。 + exe_path (str | os.PathLike[str], optional): voicepeak.exeへのパス。Defaultは標準のインストール先。 """ if not os.path.exists(exe_path): @@ -31,7 +36,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 +53,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: _StrPath | None = None, + output_path: _StrPath | None = None, narrator: Narrator | str | None = None, emotions: dict[str, int] | None = None, speed: int | None = None, @@ -58,19 +63,21 @@ def __make_say_command( args = [] match text, text_file: - case str(), str(): + case str(), str() | os.PathLike(): 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, os.PathLike() 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 +112,7 @@ async def say_text( self, text: str, *, - output_path: str | None = None, + output_path: _StrPath | None = None, narrator: Narrator | str | None = None, emotions: dict[str, int] | None = None, speed: int | None = None, @@ -117,7 +124,8 @@ async def say_text( Args: text (str): 読み上げるテキスト - output_path (str | None, optional): wavファイル出力先。指定しないとvoicepeak.exeと同じ階層にoutput.wavが生成される。 Defaults to None. + output_path (str | os.PathLike[str] | None, optional): wavファイル出力先。 + 指定しないとvoicepeak.exeと同じ階層にoutput.wavが生成される。 Defaults to None. narrator (Narrator | str | None, optional): 読み上げを行うナレータの種類。Narrator型またはstr型の名前で指定する。 Defaults to None. @@ -140,9 +148,9 @@ async def say_text( async def say_textfile( self, - text_path: str, + text_path: _StrPath, *, - output_path: str = "./output.wav", + output_path: _StrPath = "./output.wav", narrator: Narrator | str | None = None, emotions: dict[str, int] | None = None, speed: int | None = None, @@ -152,9 +160,9 @@ async def say_textfile( テキストファイル内のテキストを読み上げたwavファイルを保存する。 Args: - text_path (str): 読み上げるテキストファイルのパス + text_path (str | os.PathLike[str]): 読み上げるテキストファイルのパス - output_path (str , optional): wavファイル出力先。Defaultはoutput.wavが生成される。 + output_path (str | os.PathLike[str], optional): wavファイル出力先。Defaultはoutput.wavが生成される。 narrator (Narrator | str | None, optional): 読み上げを行うナレータの種類。Narrator型またはstr型の名前で指定する。 Defaults to None. From acf9135cc0f520610784ca091b723e58b8bfcdfc Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Sun, 9 Aug 2026 15:35:18 +0900 Subject: [PATCH 2/3] Use explicit Path annotations --- voicepeak_wrapper/voicepeak.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/voicepeak_wrapper/voicepeak.py b/voicepeak_wrapper/voicepeak.py index 77c56f3..d5b7397 100644 --- a/voicepeak_wrapper/voicepeak.py +++ b/voicepeak_wrapper/voicepeak.py @@ -5,9 +5,7 @@ import asyncio import os from dataclasses import dataclass -from typing import TypeAlias - -_StrPath: TypeAlias = str | os.PathLike[str] +from pathlib import Path _DEFAULT_EXE_PATH = os.path.join(os.environ["ProgramFiles"], "VOICEPEAK", "voicepeak.exe") @@ -21,13 +19,13 @@ class Narrator: class Voicepeak: def __init__( self, - exe_path: _StrPath = _DEFAULT_EXE_PATH, + exe_path: str | Path = _DEFAULT_EXE_PATH, ): """ 標準のインストール先ではない場所にVOICEPEAKインストールした場合はexe_pathを指定してください。 Args: - exe_path (str | os.PathLike[str], optional): voicepeak.exeへのパス。Defaultは標準のインストール先。 + exe_path (str | pathlib.Path, optional): voicepeak.exeへのパス。Defaultは標準のインストール先。 """ if not os.path.exists(exe_path): @@ -53,8 +51,8 @@ async def __async_run(self, args: list[str]) -> str: def __make_say_command( self, text: str | None = None, - text_file: _StrPath | None = None, - output_path: _StrPath | 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, @@ -63,13 +61,13 @@ def __make_say_command( args = [] match text, text_file: - case str(), str() | os.PathLike(): + case str(), str() | Path(): raise ValueError("textかtext_fileの一方のみ指定してください") case str(), None: args += ["-s", text] case None, str() as text_file_path: args += ["-t", os.fspath(text_file_path)] - case None, os.PathLike() as 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が設定されている必要があります。") @@ -112,7 +110,7 @@ async def say_text( self, text: str, *, - output_path: _StrPath | None = None, + output_path: str | Path | None = None, narrator: Narrator | str | None = None, emotions: dict[str, int] | None = None, speed: int | None = None, @@ -124,7 +122,7 @@ async def say_text( Args: text (str): 読み上げるテキスト - output_path (str | os.PathLike[str] | None, optional): wavファイル出力先。 + output_path (str | pathlib.Path | None, optional): wavファイル出力先。 指定しないとvoicepeak.exeと同じ階層にoutput.wavが生成される。 Defaults to None. narrator (Narrator | str | None, optional): 読み上げを行うナレータの種類。Narrator型またはstr型の名前で指定する。 Defaults to None. @@ -148,9 +146,9 @@ async def say_text( async def say_textfile( self, - text_path: _StrPath, + text_path: str | Path, *, - output_path: _StrPath = "./output.wav", + output_path: str | Path = "./output.wav", narrator: Narrator | str | None = None, emotions: dict[str, int] | None = None, speed: int | None = None, @@ -160,9 +158,9 @@ async def say_textfile( テキストファイル内のテキストを読み上げたwavファイルを保存する。 Args: - text_path (str | os.PathLike[str]): 読み上げるテキストファイルのパス + text_path (str | pathlib.Path): 読み上げるテキストファイルのパス - output_path (str | os.PathLike[str], optional): wavファイル出力先。Defaultはoutput.wavが生成される。 + output_path (str | pathlib.Path, optional): wavファイル出力先。Defaultはoutput.wavが生成される。 narrator (Narrator | str | None, optional): 読み上げを行うナレータの種類。Narrator型またはstr型の名前で指定する。 Defaults to None. From 68a80089652f6c46f7e35613c1326d57276998e6 Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Sun, 9 Aug 2026 15:44:03 +0900 Subject: [PATCH 3/3] Unify Path naming --- README.md | 2 +- voicepeak_wrapper/voicepeak.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a977486..66a345b 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ async def main(): asyncio.run(main()) ``` -パスを受け取る引数(exe_path, output_path, text_path)には`str`だけでなく`pathlib.Path`も渡せます。 +パスを受け取る引数(exe_path, output_path, text_path)には`str`だけでなく`pathlib.Path`(`Path`)も渡せます。 # License MITライセンス diff --git a/voicepeak_wrapper/voicepeak.py b/voicepeak_wrapper/voicepeak.py index d5b7397..260add3 100644 --- a/voicepeak_wrapper/voicepeak.py +++ b/voicepeak_wrapper/voicepeak.py @@ -25,7 +25,7 @@ def __init__( 標準のインストール先ではない場所にVOICEPEAKインストールした場合はexe_pathを指定してください。 Args: - exe_path (str | pathlib.Path, optional): voicepeak.exeへのパス。Defaultは標準のインストール先。 + exe_path (str | Path, optional): voicepeak.exeへのパス。Defaultは標準のインストール先。 """ if not os.path.exists(exe_path): @@ -122,7 +122,7 @@ async def say_text( Args: text (str): 読み上げるテキスト - output_path (str | pathlib.Path | None, optional): wavファイル出力先。 + output_path (str | Path | None, optional): wavファイル出力先。 指定しないとvoicepeak.exeと同じ階層にoutput.wavが生成される。 Defaults to None. narrator (Narrator | str | None, optional): 読み上げを行うナレータの種類。Narrator型またはstr型の名前で指定する。 Defaults to None. @@ -158,9 +158,9 @@ async def say_textfile( テキストファイル内のテキストを読み上げたwavファイルを保存する。 Args: - text_path (str | pathlib.Path): 読み上げるテキストファイルのパス + text_path (str | Path): 読み上げるテキストファイルのパス - output_path (str | pathlib.Path, optional): wavファイル出力先。Defaultはoutput.wavが生成される。 + output_path (str | Path, optional): wavファイル出力先。Defaultはoutput.wavが生成される。 narrator (Narrator | str | None, optional): 読み上げを行うナレータの種類。Narrator型またはstr型の名前で指定する。 Defaults to None.