-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_printer.py
More file actions
executable file
·90 lines (70 loc) · 2.46 KB
/
setup_printer.py
File metadata and controls
executable file
·90 lines (70 loc) · 2.46 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
#!/usr/bin/env python3
"""
Setup script to detect and save Brother VC-500W printer IP address to config
"""
import json
import os
import subprocess
import sys
DEFAULT_HOSTNAME = "VC-500W4188.local"
AVAHI_TIMEOUT = 10
def detect_printer():
"""Detect the printer IP address using avahi-resolve
Returns the IP address (e.g., "192.168.1.100") or None if not found.
avahi-resolve output format: "hostname.local\t192.168.1.100"
"""
try:
result = subprocess.run(
["avahi-resolve", "-n", DEFAULT_HOSTNAME, "-4"],
capture_output=True,
text=True,
timeout=AVAHI_TIMEOUT,
)
if result.returncode == 0:
# Parse output: "hostname.local\t192.168.1.100"
# Extract the IP address (second column)
parts = result.stdout.strip().split("\t")
if len(parts) >= 2:
return parts[1]
return None
except (subprocess.TimeoutExpired, FileNotFoundError):
return None
def get_config_path():
"""Get the path to the configuration file"""
config_dir = os.path.expanduser("~/.config/labelprinter")
return os.path.join(config_dir, "config.json")
def load_existing_config(config_file):
"""Load existing configuration or return empty dict"""
if not os.path.exists(config_file):
return {}
try:
with open(config_file, "r") as f:
return json.load(f)
except json.JSONDecodeError:
print(f"Warning: Invalid config file {config_file}, creating new one")
return {}
def save_config(ip_address):
"""Save the IP address to the config file"""
config_file = get_config_path()
os.makedirs(os.path.dirname(config_file), exist_ok=True)
config = load_existing_config(config_file)
config["host"] = ip_address
with open(config_file, "w") as f:
json.dump(config, f, indent=2)
print(f"✅ Saved printer IP address to config: {ip_address}")
def main():
print("🔍 Detecting Brother VC-500W printer...")
ip_address = detect_printer()
if ip_address:
print(f"✅ Found printer at {ip_address}")
save_config(ip_address)
return 0
else:
print("❌ Could not find printer")
print(" Make sure the printer is on and connected to the network")
print(
" You can also manually set the IP address in ~/.config/labelprinter/config.json"
)
return 1
if __name__ == "__main__":
sys.exit(main())