python-clipboard-speaker

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

commit ef4ab2d3396004ad0b7467a138b52fd9a95aaab8
parent 38687fed6c4c87ebe4b7827d8f65083104104058
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Fri, 26 Nov 2021 01:15:03 +0200

Python shenanigans and commented out shell code. Also changelog.

Diffstat:
MCHANGELOG.md | 6++++++
Mclipboard-speaker | 7+++++++
Mclipboard-speaker-kill | 4+++-
Aclipboard-speaker-kill.py | 14++++++++++++++
Mclipboard-speaker.py | 39++++++++++++++++-----------------------
5 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 2021-11-26 + +More Python script experimentations. I've started with a shell threeliner using +ready made packages and I ended up reimplementing them badly. +Something something UNIX philosophy. + ## 2021-11-25 Trying to remove the daemonize dependency, but I don't know how to kill the diff --git a/clipboard-speaker b/clipboard-speaker @@ -6,9 +6,16 @@ pid_file_path="$clipboard_speaker_directory/pid" fifo_path="$clipboard_speaker_directory/fifo" speak_ng_path="$(which speak-ng)" + mkdir -p "$clipboard_speaker_directory" mkfifo --mode=600 "$fifo_path" +# pgrep speak-ng >/dev/null || speak-ng -f "$fifo_path" +# pgrep speak-ng >/dev/null || { +# echo $$ > "$pid_file_path" +# speak-ng -f "$fifo_path" +# } + xsel -p | tr '\n' ' ' > "$fifo_path" & daemonize -v -l "$lock_file_path" -p "$pid_file_path" "$speak_ng_path" -f "$fifo_path" diff --git a/clipboard-speaker-kill b/clipboard-speaker-kill @@ -3,4 +3,6 @@ clipboard_speaker_directory="$HOME/.clipboard-speaker/" pid_file_path="$clipboard_speaker_directory/pid" -kill "$(cat "$pid_file_path")" +[ -f "$pid_file_path" ] && kill "$(cat "$pid_file_path")" + +rm "$pid_file_path" diff --git a/clipboard-speaker-kill.py b/clipboard-speaker-kill.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 + +import os +import pathlib +import signal + +CLIPBOARD_SPEAKER_PATH = pathlib.Path.home() / ".clipboard-speaker" +PID_FILE_PATH = CLIPBOARD_SPEAKER_PATH / "pid" + +if __name__ == "__main__": + with PID_FILE_PATH.open("r") as pid_file: + pid = int(pid_file.read()) + os.remove(PID_FILE_PATH) + os.kill(pid, signal.SIGTERM) diff --git a/clipboard-speaker.py b/clipboard-speaker.py @@ -1,33 +1,26 @@ #!/usr/bin/env python3 -import signal import os from pathlib import Path from subprocess import Popen, PIPE CLIPBOARD_SPEAKER_PATH = Path.home() / ".clipboard-speaker" -FIFO_FILE_PATH = CLIPBOARD_SPEAKER_PATH / "fifo" PID_FILE_PATH = CLIPBOARD_SPEAKER_PATH / "pid" -LOCK_FILE_PATH = CLIPBOARD_SPEAKER_PATH / "lock" if __name__ == "__main__": - clipboard_speaker_pid = os.getpid() - with open(PID_FILE_PATH, 'w') as pid_file: - pid_file.write(str(clipboard_speaker_pid)) - #with open(FIFO_FILE_PATH, "w") as fifo: - # xsel_process = Popen(["xsel", "-p"], stdout=fifo) - xsel_process = Popen(["xsel", "-p"], stdout=PIPE) - speak_ng_process = Popen( - [ - "speak-ng", - '--stdin' - ], - stdin=xsel_process.stdout, - stdout=PIPE , - ) - def kill(*a): - print('kill:', a) - speak_ng_process.terminate() - signal.signal(signal.SIGHUP, kill) - signal.signal(signal.SIGTERM, kill) - speak_ng_process.wait() + if not PID_FILE_PATH.exists(): + xsel_process = Popen(["xsel", "-p"], stdout=PIPE) + + speak_ng_process = Popen( + ["speak-ng", "--stdin"], + stdin=xsel_process.stdout, + stdout=PIPE, + ) + + with PID_FILE_PATH.open("w") as pid_file: + pid_file.write(str(speak_ng_process.pid)) + + try: + speak_ng_process.wait() + except KeyboardInterrupt as e: + os.remove(PID_FILE_PATH)