-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
211 lines (172 loc) · 5.84 KB
/
main.py
File metadata and controls
211 lines (172 loc) · 5.84 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
"""
Speedcube Training Explorer - Main Entry Point
Simplified launcher that starts the web interface
"""
import sys
import webbrowser
import time
import subprocess
import socket
from pathlib import Path
# Add src to path
sys.path.insert(0, str(Path(__file__).parent / 'src' / 'python'))
def print_banner():
"""Print welcome banner"""
print("\n" + "="*60)
print(" SPEEDCUBE TRAINING EXPLORER")
print("="*60)
print()
def check_port_available(port=5000):
"""Check if port is available"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('localhost', port))
sock.close()
return result != 0
def check_database():
"""Quick database check and initialization if needed"""
try:
from db_manager import DatabaseManager
db = DatabaseManager()
with db.get_connection() as conn:
cursor = conn.cursor()
# Check if tables exist
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = [row[0] for row in cursor.fetchall()]
if not tables or 'cubes' not in tables:
print("⚠️ Database needs initialization...")
db.create_schema()
print("✅ Database initialized!")
else:
print(f"✅ Database ready ({len(tables)} tables)")
return True
except Exception as e:
print(f"❌ Database error: {e}")
print("\nTry running:")
print(" python -m src.python.db_manager")
return False
def launch_web_app():
"""Launch the web application"""
print_banner()
print("🚀 Starting Speedcube Training Explorer...")
print()
# Check database
if not check_database():
print("\n❌ Cannot start without a valid database")
input("\nPress Enter to exit...")
return
print()
# Check if port is already in use
if not check_port_available(5000):
print("⚠️ Port 5000 is already in use!")
print("The app might already be running.")
response = input("\nOpen browser anyway? (y/n): ").lower()
if response == 'y':
webbrowser.open('http://localhost:5000')
return
# Start the server
print("⏳ Starting web server...")
try:
# Start server in background
process = subprocess.Popen(
[sys.executable, 'website_server.py'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
print("⏳ Waiting for server to start...")
time.sleep(2)
# Check if server started successfully
if process.poll() is None:
print("✅ Server started!")
print("🌐 Opening browser at http://localhost:5000")
print()
print("Press Ctrl+C to stop the server")
print()
# Open browser
webbrowser.open('http://localhost:5000')
# Wait for user to stop
try:
process.wait()
except KeyboardInterrupt:
print("\n\n👋 Shutting down server...")
process.terminate()
process.wait()
else:
print("❌ Server failed to start")
stdout, stderr = process.communicate()
if stderr:
print(f"Error: {stderr.decode()}")
except Exception as e:
print(f"❌ Error starting server: {e}")
print("\nTry running manually:")
print(" python website_server.py")
def init_database():
"""Initialize or reset database"""
print_banner()
print("DATABASE INITIALIZATION")
print("-" * 60)
print()
print("⚠️ This will create/reset the database.")
print("All existing data will be lost!")
print()
confirm = input("Continue? (type 'yes' to confirm): ").strip()
if confirm.lower() == 'yes':
try:
from db_manager import DatabaseManager
print("\n🔄 Initializing database...")
db = DatabaseManager()
db.create_schema()
db.get_table_info()
print("\n✅ Database initialized successfully!")
except Exception as e:
print(f"\n❌ Error: {e}")
else:
print("\n❌ Cancelled")
def show_help():
"""Show help information"""
print_banner()
print("USAGE")
print("-" * 60)
print()
print("Start the web interface:")
print(" python main.py")
print(" python main.py --web")
print()
print("Initialize/reset database:")
print(" python main.py --init-db")
print()
print("Show this help:")
print(" python main.py --help")
print()
print("-" * 60)
print()
print("Once the web interface is open:")
print(" • Use the Timer for live solving")
print(" • Import CSTimer data from the Import tab")
print(" • View sessions and statistics")
print(" • Manage your cube inventory")
print(" • Analyze progress with interactive charts")
print()
def main():
"""Main entry point"""
# Handle command line arguments
if len(sys.argv) > 1:
arg = sys.argv[1].lower()
if arg in ['--help', '-h', 'help']:
show_help()
elif arg in ['--init-db', '--init', 'init']:
init_database()
elif arg in ['--web', 'web']:
launch_web_app()
else:
print(f"Unknown argument: {sys.argv[1]}")
print("Use --help for usage information")
return
# No arguments - launch web interface
launch_web_app()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n👋 Interrupted. Goodbye!")
sys.exit(0)