From a240a097839426693fa4daab90e98c8363e2de4f Mon Sep 17 00:00:00 2001 From: lohrrrr Date: Wed, 3 Jun 2026 12:53:43 +0000 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20TyConfigManager=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D1=83=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D1=83?= =?UTF-8?q?=D1=80=D0=B0=D1=86=D0=B8=D0=B5=D0=B9=20=D0=BF=D1=80=D0=B8=D0=BB?= =?UTF-8?q?=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tyconfig.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 lib/tyconfig.py diff --git a/lib/tyconfig.py b/lib/tyconfig.py new file mode 100644 index 0000000..a289b6c --- /dev/null +++ b/lib/tyconfig.py @@ -0,0 +1,52 @@ +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() \ No newline at end of file