Cleanup develop code and dont output trailing commas for JSON

This commit is contained in:
Kovid Goyal 2023-01-24 18:07:08 +05:30
parent a58baba395
commit 4d23f9649a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 25 additions and 6 deletions

View File

@ -205,20 +205,24 @@ public:
return serialize_string_for_json(s, out); return serialize_string_for_json(s, out);
case DT_LIST: { case DT_LIST: {
out << '['; out << '[';
bool first = true;
for (auto const &i : list) { for (auto const &i : list) {
if (!first) out << ", ";
first = false;
i.serialize(out); i.serialize(out);
out << ", ";
} }
out << ']'; out << ']';
break; break;
} }
case DT_OBJECT: { case DT_OBJECT: {
out << '{'; out << '{';
bool first = true;
for (const auto& [key, value]: object) { for (const auto& [key, value]: object) {
if (!first) out << ", ";
first = false;
serialize_string_for_json(key, out); serialize_string_for_json(key, out);
out << ": "; out << ": ";
value.serialize(out); value.serialize(out);
out << ", ";
} }
out << '}'; out << '}';
break; break;

View File

@ -2,22 +2,34 @@
# License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net>
import json
import sys import sys
import time import time
from contextlib import closing from contextlib import closing
from queue import Queue
from threading import Thread from threading import Thread
from calibre.utils.ipc.simple_worker import start_pipe_worker from calibre.utils.ipc.simple_worker import start_pipe_worker
def decode_msg(line: bytes) -> dict:
parts = line.strip().split(b' ', 2)
msg_id, msg_type, ans = int(parts[0]), parts[1].decode(), json.loads(parts[2])
ans['related_to'] = msg_id
ans['payload_type'] = msg_type
return ans
def develop_speech(text='Lucca brazzi sleeps with the fishes'): def develop_speech(text='Lucca brazzi sleeps with the fishes'):
p = start_pipe_worker('from calibre_extensions.winspeech import run_main_loop; run_main_loop()') p = start_pipe_worker('from calibre_extensions.winspeech import run_main_loop; run_main_loop()')
print('\x1b[32mSpeaking', text, '\x1b[39m', flush=True) print('\x1b[32mSpeaking', text, '\x1b[39m', flush=True) # ]]
q = Queue()
def echo_output(p): def echo_output(p):
for line in p.stdout: for line in p.stdout:
sys.stdout.buffer.write(b'\x1b[33m' + line + b'\x1b[39m') sys.stdout.buffer.write(b'\x1b[33m' + line + b'\x1b[39m') # ]]
sys.stdout.buffer.flush() sys.stdout.buffer.flush()
q.put(decode_msg(line))
def send(*a): def send(*a):
cmd = ' '.join(map(str, a)) + '\n' cmd = ' '.join(map(str, a)) + '\n'
@ -29,10 +41,13 @@ def develop_speech(text='Lucca brazzi sleeps with the fishes'):
try: try:
send('1 echo Synthesizer started') send('1 echo Synthesizer started')
send('2 speak text inline', text) send('2 speak text inline', text)
time.sleep(6) while True:
m = q.get()
if m['payload_type'] == 'media_state_changed' and m['state'] == 'ended':
break
send('3 echo Synthesizer exiting') send('3 echo Synthesizer exiting')
send('exit') send('exit')
time.sleep(1) p.wait(1)
finally: finally:
if p.poll() is None: if p.poll() is None:
p.kill() p.kill()