python-clipboard-speaker

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 84872cef5046815dbc537f7ed760a8af14a871fa
parent 560254cc5e1c197f8ce5dd62f8118a4a098382ea
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Fri, 26 Nov 2021 22:23:19 +0200

Switch (again) to FIFO for continuous feeding of text.

Diffstat:
Mclipboard-speaker.py | 26+++++++++++++++++++++-----
1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/clipboard-speaker.py b/clipboard-speaker.py @@ -8,6 +8,7 @@ DEFAULT_WORDS_PER_MINUTE = "175" CLIPBOARD_SPEAKER_PATH = Path.home() / ".clipboard-speaker" PID_FILE_PATH = CLIPBOARD_SPEAKER_PATH / "pid" +FIFO_FILE_PATH = CLIPBOARD_SPEAKER_PATH / "fifo" WORDS_PER_MINUTE_PATH = CLIPBOARD_SPEAKER_PATH / "words-per-minute" @@ -26,15 +27,30 @@ def get_words_per_minute() -> str: if __name__ == "__main__": words_per_minute = get_words_per_minute() + try: + os.mkfifo(FIFO_FILE_PATH, mode=0o600) + except FileExistsError: + pass - if not PID_FILE_PATH.exists(): - xsel_process = Popen(["xsel", "-p"], stdout=PIPE) + # https://stackoverflow.com/questions/63132778/how-to-use-fifo-named-pipe-as-stdin-in-popen-python + fifo_read_file = os.open(FIFO_FILE_PATH, os.O_RDONLY | os.O_NONBLOCK) + fifo_write_file = os.open(FIFO_FILE_PATH, os.O_WRONLY) + + Popen( + ["xsel", "-p"], + stdout=fifo_write_file, + ) + os.close(fifo_write_file) + if not PID_FILE_PATH.exists(): speak_ng_process = Popen( - ["speak-ng", f"-s {words_per_minute}", "--stdin"], - stdin=xsel_process.stdout, - stdout=PIPE, + [ + "speak-ng", + f"-s {words_per_minute}", + ], + stdin=fifo_read_file, ) + os.close(fifo_read_file) with PID_FILE_PATH.open("w") as pid_file: pid_file.write(str(speak_ng_process.pid))