-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
152 lines (120 loc) · 4.06 KB
/
main.py
File metadata and controls
152 lines (120 loc) · 4.06 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
#!/usr/bin/env python3
"""
AutomaPrint Server - REST API PDF Print Server
A simple REST API server that receives PDF files and prints them to a local printer.
Usage:
python main.py - Run GUI (default)
python main.py gui - Run GUI
python main.py gui auto_start - Run GUI minimized with auto-start
python main.py server - Run server only (no GUI)
python main.py test - Run test client
python main.py --help - Show help
"""
import sys
import signal
def signal_handler(signum, frame):
"""Handle Ctrl+C"""
print("\n[STOP] Shutting down...")
sys.exit(0)
def run_server_only():
"""Run server without GUI"""
from automaprint.server import PrintServer
print("AutomaPrint Server - REST API PDF Print Server")
print("=" * 46)
print()
server = PrintServer()
if not server.config.get("printer_name"):
print("No printer configured!")
print()
printers = server.list_printers()
if not printers:
print("[!] No printers found!")
return
print("Available printers:")
for i, printer in enumerate(printers, 1):
print(f" {i}. {printer}")
while True:
try:
selection = input(f"\nSelect printer (1-{len(printers)}): ")
idx = int(selection) - 1
if 0 <= idx < len(printers):
selected = printers[idx]
break
except ValueError:
pass
print("[!] Invalid selection")
port_input = input("Port (default 8080): ").strip()
port = int(port_input) if port_input else 8080
server.save_config(printer_name=selected, port=port)
print("[OK] Configuration saved")
print(f"Starting server...")
print(f"Printer: {server.config['printer_name']}")
print(f"Port: {server.config['port']}")
print("Press Ctrl+C to stop")
print()
try:
server.start()
except KeyboardInterrupt:
print("\n[STOP] Server stopped")
def run_test_client():
"""Run interactive test client"""
from automaprint import test_client
print("AutomaPrint Server - Test Client")
print("=" * 33)
print()
host = input("Server host (localhost): ").strip() or "localhost"
port_input = input("Server port (8080): ").strip()
port = int(port_input) if port_input else 8080
print()
print("1. Test connection")
print("2. Send PDF file")
print()
choice = input("Choice (1-2): ").strip()
if choice == "1":
test_client.test_connection(host, port)
elif choice == "2":
pdf_path = input("PDF file path: ").strip()
test_client.send_pdf_file(host, port, pdf_path)
else:
print("Invalid choice")
def show_help():
"""Show help"""
print(__doc__)
print()
print("REST API Endpoints:")
print(" GET /health - Health check")
print(" POST /print - Print PDF file (body = PDF data)")
print()
print("Example (curl):")
print(' curl -X POST -H "Content-Type: application/pdf" \\')
print(' --data-binary @document.pdf http://localhost:8080/print')
print()
def main():
"""Main entry point"""
signal.signal(signal.SIGINT, signal_handler)
if len(sys.argv) > 1:
command = sys.argv[1].lower()
if command in ['--help', '-h', 'help']:
show_help()
elif command == 'gui':
auto_start = 'auto_start' in sys.argv
from automaprint.gui import run_gui
run_gui(auto_start)
elif command == 'server':
run_server_only()
elif command == 'test':
run_test_client()
else:
print(f"Unknown command: {command}")
show_help()
else:
# Default to GUI
try:
from automaprint.gui import run_gui
run_gui()
except ImportError as e:
print(f"GUI dependencies not available: {e}")
print("Running in server mode...")
run_server_only()
if __name__ == "__main__":
main()