You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.3 KiB

"""
客户端配置文件
"""
import json
import os
class Config:
def __init__(self, config_file='config.json'):
self.config_file = config_file
self.default_config = {
'server_host': '127.0.0.1',
'server_port': 8888,
'buffer_size': 4096,
'timeout': 30,
'auto_login': False,
'username': '',
'password': '',
'save_password': False
}
self.config = self.load_config()
def load_config(self):
"""加载配置文件"""
if os.path.exists(self.config_file):
try:
with open(self.config_file, 'r', encoding='utf-8') as f:
return {**self.default_config, **json.load(f)}
except:
return self.default_config
return self.default_config
def get(self, key, default=None):
"""获取配置项"""
return self.config.get(key, default)
def set(self, key, value):
"""设置配置项"""
self.config[key] = value
def save(self):
"""保存配置"""
with open(self.config_file, 'w', encoding='utf-8') as f:
json.dump(self.config, f, indent=4, ensure_ascii=False)
config = Config()