-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVibeType.py
More file actions
120 lines (106 loc) · 3.99 KB
/
Copy pathVibeType.py
File metadata and controls
120 lines (106 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# vibe_type.py
import tkinter as tk
import gui.theme_manager
from gui.tray_app import TrayApplication
from core import hotkey_handler
import subprocess
from core.config_manager import load_config
import os
import sys
import threading
import time
import requests
_api_proc = None
_stdout_thread = None
_stderr_thread = None
def _stream_pipe(prefix, pipe):
try:
for line in iter(pipe.readline, ''):
if not line:
break
print(f"{prefix} {line.rstrip()}")
except Exception:
pass
def maybe_start_api_server():
global _api_proc, _stdout_thread, _stderr_thread
config = load_config()
api_config = config.get('api', {})
if api_config.get('auto_start', False):
api_path = os.path.join(os.path.dirname(__file__), 'api', 'api.py')
project_root = os.path.dirname(__file__)
host = str(api_config.get('host', '0.0.0.0'))
port = int(api_config.get('port', 9031))
print('Auto-starting API server...')
env = os.environ.copy()
# Ensure local imports work and output is unbuffered for real-time logs
env['PYTHONPATH'] = project_root + os.pathsep + env.get('PYTHONPATH', '')
env['PYTHONUNBUFFERED'] = '1'
env['PYTHONIOENCODING'] = 'utf-8'
# Pass host/port to API
env['VIBETYPE_API_HOST'] = host
env['VIBETYPE_API_PORT'] = str(port)
_api_proc = subprocess.Popen(
[sys.executable, api_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
cwd=project_root,
env=env,
bufsize=1
)
print(f'API server started in background with PID {_api_proc.pid}')
# Stream logs to console so we see port/IP info from the API
_stdout_thread = threading.Thread(target=_stream_pipe, args=('API stdout:', _api_proc.stdout), daemon=True)
_stderr_thread = threading.Thread(target=_stream_pipe, args=('API stderr:', _api_proc.stderr), daemon=True)
_stdout_thread.start()
_stderr_thread.start()
# Readiness probe with brief retries
base_url = f"http://127.0.0.1:{port}"
ready_url = f"{base_url}/api/v1/tts/kokoro/languages"
ok = False
for _ in range(10): # up to ~5s
if _api_proc.poll() is not None:
break
try:
r = requests.get(ready_url, timeout=0.5)
if r.status_code == 200:
ok = True
break
except Exception:
pass
time.sleep(0.5)
if _api_proc.poll() is not None:
try:
out, err = _api_proc.communicate(timeout=1)
except Exception:
out, err = '', ''
print(f'API server exited early with code {_api_proc.returncode}')
print('API server stdout:', out)
print('API server stderr:', err)
else:
if ok:
print(f'API server is up: {base_url} (languages endpoint OK) and host={host}')
else:
print('API server is still starting; logs will appear above if there are issues.')
def main():
"""Main function to start VibeType with the correct, stable initialization order."""
print("Starting VibeType...")
# Auto-start API server if enabled in config
maybe_start_api_server()
# 1. Create the single, shared root window.
root = tk.Tk()
# 2. Apply the theme to this specific root window.
gui.theme_manager.apply_theme(root)
# 3. Hide the root window so the app is tray-only.
root.withdraw()
# 4. Create the application instance, passing it the themed root window.
app = TrayApplication(root)
# 5. Start the background hotkey listener.
print("Starting hotkey listener...")
hotkey_handler.start_hotkey_listener()
# 6. Run the main application loop.
print("Starting application main loop...")
app.run()
print("VibeType stopped.")
if __name__ == "__main__":
main()