ircthing/foobar/kiss.py

75 lines
1.9 KiB
Python
Raw Permalink Normal View History

2024-02-11 09:05:43 +02:00
import queue
2024-02-11 12:33:13 +02:00
import os
2024-02-11 09:05:43 +02:00
import time
import threading
2024-02-11 12:33:13 +02:00
import re
2024-02-11 09:05:43 +02:00
def main():
print("Hello There")
que = queue.Queue()
event = threading.Event()
2024-02-11 09:31:37 +02:00
config = {'output': '/tmp/test_out', 'input': '/tmp/test_in'}
2024-02-11 09:05:43 +02:00
#input(que)
2024-02-11 12:33:13 +02:00
input_thread = threading.Thread(target=handle_input, args=(que, event, config))
2024-02-11 09:05:43 +02:00
input_thread.daemon = True
input_thread.start()
#output(que)
2024-02-11 12:33:13 +02:00
output_thread = threading.Thread(target=handle_output, args=(que, event, config))
2024-02-11 09:05:43 +02:00
output_thread.daemon = True
output_thread.start()
2024-02-11 12:33:13 +02:00
countdown(config, event)
def countdown(config, event):
print(f"Speak to: {config['input']}")
print(f"Output is going to be: {config['output']}")
num = 0
while not event.is_set():
print(f"wait till full: {num}/~", end='\r')
time.sleep(0.5)
num = num + 1
2024-02-11 09:05:43 +02:00
print("\nDone!")
2024-02-11 09:31:37 +02:00
def handle_input(que, event, config):
file = config['input']
2024-02-11 12:33:13 +02:00
try:
os.mkfifo(file)
except FileExistsError:
pass
with open(file, 'r') as in_fifo:
while not event.is_set():
fifo_read = in_fifo.readline().rsplit('\n')
if not fifo_read:
continue
message = fifo_read
que.put(message)
time.sleep(1)
2024-02-11 09:05:43 +02:00
2024-02-11 09:31:37 +02:00
def handle_output(que, event, config):
2024-02-11 12:33:13 +02:00
file = config['output']
2024-02-11 09:05:43 +02:00
with open(file, 'a') as out_file:
2024-02-11 09:24:45 +02:00
while not event.is_set():
2024-02-11 12:33:13 +02:00
try:
message = que.get()
except queue.Empty:
continue
if message == "":
continue
elif 'EXIT' in message:
event.set()
out_file.write(f"{time.time()} | Bye!\r\n")
out_file.flush()
else:
out_file.write(f"{time.time()} | {message[0]}\r\n")
out_file.flush()
time.sleep(1)
2024-02-11 09:05:43 +02:00
if __name__ == "__main__":
main()
print("Bye now!")
exit(0)