-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
141 lines (117 loc) · 4.54 KB
/
Copy pathconfig.py
File metadata and controls
141 lines (117 loc) · 4.54 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
配置管理模块
处理srun的配置文件存储和读取
"""
import json
import base64
from pathlib import Path
class SrunConfig:
"""配置管理类"""
def __init__(self, config_name='.srun'):
self.config_dir = Path.home() / config_name
self.config_file = self.config_dir / 'config.json'
self.ensure_config_dir()
def ensure_config_dir(self):
"""确保配置目录存在"""
self.config_dir.mkdir(exist_ok=True)
# 设置目录权限为仅用户可读写
try:
self.config_dir.chmod(0o700)
except:
pass # 忽略权限设置失败(如Windows)
def load_config(self):
"""加载配置文件"""
if not self.config_file.exists():
return {}
try:
with open(self.config_file, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
print(f"警告: 配置文件读取失败: {e}")
return {}
def save_config(self, config):
"""保存配置文件"""
try:
with open(self.config_file, 'w', encoding='utf-8') as f:
json.dump(config, f, indent=2, ensure_ascii=False)
# 设置文件权限为仅用户可读写
try:
self.config_file.chmod(0o600)
except:
pass # 忽略权限设置失败(如Windows)
print(f"配置已保存到: {self.config_file}")
return True
except Exception as e:
print(f"错误: 配置文件保存失败: {e}")
return False
def get_credentials(self):
"""获取存储的凭据"""
config = self.load_config()
username = config.get('username', '')
password = config.get('password', '')
# 简单的base64解码(注意:这不是安全的加密方式)
if password:
try:
password = base64.b64decode(password.encode()).decode()
except Exception as e:
print(f"警告: 密码解码失败: {e}")
password = ''
return username, password
def save_credentials(self, username, password):
"""保存凭据到配置文件"""
config = self.load_config()
config['username'] = username
# 简单的base64编码(注意:这不是安全的加密方式)
try:
config['password'] = base64.b64encode(password.encode()).decode()
except Exception as e:
print(f"错误: 密码编码失败: {e}")
return False
return self.save_config(config)
def get_server_config(self):
"""获取服务器配置"""
config = self.load_config()
return {
'server_url': config.get('server_url', 'http://10.0.0.55'),
'ac_id': config.get('ac_id', '8'),
'timeout': config.get('timeout', 10)
}
def save_server_config(self, server_url=None, ac_id=None, timeout=None):
"""保存服务器配置"""
config = self.load_config()
if server_url is not None:
config['server_url'] = server_url
if ac_id is not None:
config['ac_id'] = str(ac_id)
if timeout is not None:
config['timeout'] = int(timeout)
return self.save_config(config)
def clear_config(self):
"""清除配置文件"""
try:
if self.config_file.exists():
self.config_file.unlink()
print("配置文件已清除")
return True
except Exception as e:
print(f"错误: 无法清除配置文件: {e}")
return False
def show_config(self):
"""显示当前配置(隐藏密码)"""
config = self.load_config()
if not config:
print("未找到配置文件")
return
print("当前配置:")
print(f" 用户名: {config.get('username', '未设置')}")
print(f" 密码: {'已保存' if config.get('password') else '未设置'}")
print(f" 服务器: {config.get('server_url', '默认')}")
print(f" AC ID: {config.get('ac_id', '默认')}")
print(f" 超时: {config.get('timeout', '默认')}秒")
print(f" 配置文件位置: {self.config_file}")
if __name__ == '__main__':
# 测试代码
config = SrunConfig()
config.show_config()