Add config

This commit is contained in:
Jimmy 2025-01-04 19:44:13 +13:00
parent cc45c919f5
commit 9d35fb5002
1 changed files with 49 additions and 0 deletions

49
config.py Normal file
View File

@ -0,0 +1,49 @@
import json
import os
default_config = {
'color': (255, 0, 0),
'brightness': 1.0,
'color_format': 'RGB',
'servers': [('192.168.0.201', 80)]
}
config_file = 'config.json'
class ConfigHandler:
def load_config(self):
"""Load color, brightness, format, and server settings from config file."""
if os.path.exists(config_file):
with open(config_file, 'r') as f:
return json.load(f)
else:
return default_config
def save_config(self, color, brightness, color_format, servers):
"""Save color, brightness, format, and server settings to config file."""
config = {
'color': color,
'brightness': brightness,
'color_format': color_format,
'servers': servers
}
with open(config_file, 'w') as f:
json.dump(config, f)
print("Configuration saved.")
def add_server(self, ip, port):
"""Add a new server IP and port."""
config = self.load_config()
config['servers'].append((ip, port))
self.save_config(config['color'], config['brightness'], config['color_format'], config['servers'])
print(f"Server {ip}:{port} added.")
def remove_server(self, ip, port):
"""Remove an existing server IP and port."""
config = self.load_config()
if (ip, port) in config['servers']:
config['servers'].remove((ip, port))
self.save_config(config['color'], config['brightness'], config['color_format'], config['servers'])
print(f"Server {ip}:{port} removed.")
else:
print(f"Server {ip}:{port} not found.")