52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
import os
|
|
import configparser
|
|
|
|
class TyConfigManager:
|
|
def __init__(self, config_path="settings.ini"):
|
|
self.config_path = config_path
|
|
self.config = configparser.ConfigParser()
|
|
|
|
self.defaults = {
|
|
"Network": {
|
|
"server_ip": "127.0.0.1",
|
|
"server_port": "5555"
|
|
},
|
|
"Crypto": {
|
|
"wallet_file": "tychat.wallet"
|
|
},
|
|
"UI": {
|
|
"theme": "dark",
|
|
"username": "Anonymous"
|
|
}
|
|
}
|
|
self.load_config()
|
|
|
|
def load_config(self):
|
|
if not os.path.exists(self.config_path):
|
|
for section, options in self.defaults.items():
|
|
self.config[section] = options
|
|
self.save_config()
|
|
else:
|
|
self.config.read(self.config_path)
|
|
|
|
def save_config(self):
|
|
with open(self.config_path, "w", encoding="utf-8") as f:
|
|
self.config.write(f)
|
|
|
|
def get_wallet_status(self) -> tuple[bool, str]:
|
|
wallet_path = self.config.get("Crypto", "wallet_file", fallback="tychat.wallet")
|
|
return os.path.exists(wallet_path), wallet_path
|
|
|
|
def get_network_settings(self) -> tuple[str, int]:
|
|
ip = self.config.get("Network", "server_ip", fallback="127.0.0.1")
|
|
port = self.config.getint("Network", "server_port", fallback=5555)
|
|
return ip, port
|
|
|
|
def get_value(self, section: str, option: str, fallback=None) -> str:
|
|
return self.config.get(section, option, fallback=fallback)
|
|
|
|
def set_value(self, section: str, option: str, value: str):
|
|
if not self.config.has_section(section):
|
|
self.config.add_section(section)
|
|
self.config.set(section, option, str(value))
|
|
self.save_config() |