forked from vancehuds/VanceCoursePro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount_manager.py
More file actions
198 lines (164 loc) · 6.34 KB
/
account_manager.py
File metadata and controls
198 lines (164 loc) · 6.34 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""
Account Manager
Handles multiple student account management for course selection.
"""
import os
import json
import uuid
from dataclasses import dataclass, asdict
from typing import List, Optional
@dataclass
class Account:
"""Represents a student account."""
id: str
name: str
username: str
password: str
def to_dict(self) -> dict:
return asdict(self)
@classmethod
def from_dict(cls, data: dict) -> 'Account':
return cls(
id=data.get('id', str(uuid.uuid4())),
name=data.get('name', ''),
username=data.get('username', ''),
password=data.get('password', '')
)
class AccountManager:
"""Manages multiple student accounts."""
DEFAULT_BASE_URL = "" # Must be configured by user
def __init__(self, config_path: str = None):
if config_path is None:
config_path = os.path.join(os.path.dirname(__file__), "config.json")
self.config_path = config_path
self.accounts: List[Account] = []
self.default_account_id: Optional[str] = None
self.base_url: str = self.DEFAULT_BASE_URL
self._load()
def _load(self):
"""Load accounts from config file."""
if not os.path.exists(self.config_path):
self.accounts = []
self.default_account_id = None
return
try:
with open(self.config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
# Check if it's the new format (has 'accounts' key)
if 'accounts' in config:
self.accounts = [Account.from_dict(acc) for acc in config.get('accounts', [])]
self.default_account_id = config.get('default_account')
self.base_url = config.get('base_url', self.DEFAULT_BASE_URL)
else:
# Legacy format: single account with username/password at root
self._migrate_legacy_config(config)
except Exception as e:
print(f"Error loading config: {e}")
self.accounts = []
self.default_account_id = None
def _migrate_legacy_config(self, legacy_config: dict):
"""Convert old single-account config to new format."""
username = legacy_config.get('username', '')
password = legacy_config.get('password', '')
if username:
account = Account(
id=str(uuid.uuid4()),
name=f"账号1",
username=username,
password=password
)
self.accounts = [account]
self.default_account_id = account.id
# Save in new format
self.save()
else:
self.accounts = []
self.default_account_id = None
def save(self):
"""Save accounts to config file."""
config = {
'accounts': [acc.to_dict() for acc in self.accounts],
'default_account': self.default_account_id,
'base_url': self.base_url
}
with open(self.config_path, 'w', encoding='utf-8') as f:
json.dump(config, f, ensure_ascii=False, indent=2)
def add_account(self, name: str, username: str, password: str) -> Account:
"""Add a new account."""
account = Account(
id=str(uuid.uuid4()),
name=name,
username=username,
password=password
)
self.accounts.append(account)
# Set as default if it's the first account
if len(self.accounts) == 1:
self.default_account_id = account.id
self.save()
return account
def update_account(self, account_id: str, name: str = None,
username: str = None, password: str = None) -> Optional[Account]:
"""Update an existing account."""
account = self.get_account(account_id)
if not account:
return None
if name is not None:
account.name = name
if username is not None:
account.username = username
if password is not None:
account.password = password
self.save()
return account
def remove_account(self, account_id: str) -> bool:
"""Remove an account by ID."""
for i, acc in enumerate(self.accounts):
if acc.id == account_id:
self.accounts.pop(i)
# Update default if we removed the default account
if self.default_account_id == account_id:
self.default_account_id = self.accounts[0].id if self.accounts else None
self.save()
return True
return False
def get_account(self, account_id: str) -> Optional[Account]:
"""Get account by ID."""
for acc in self.accounts:
if acc.id == account_id:
return acc
return None
def get_account_by_username(self, username: str) -> Optional[Account]:
"""Get account by username."""
for acc in self.accounts:
if acc.username == username:
return acc
return None
def get_default_account(self) -> Optional[Account]:
"""Get the default account."""
if self.default_account_id:
return self.get_account(self.default_account_id)
return self.accounts[0] if self.accounts else None
def set_default_account(self, account_id: str) -> bool:
"""Set the default account."""
if self.get_account(account_id):
self.default_account_id = account_id
self.save()
return True
return False
def get_all_accounts(self) -> List[Account]:
"""Get all accounts."""
return self.accounts.copy()
def get_base_url(self) -> str:
"""Get the configured base URL."""
return self.base_url
def set_base_url(self, url: str) -> None:
"""Set the base URL."""
self.base_url = url.rstrip('/') if url else self.DEFAULT_BASE_URL
self.save()
if __name__ == "__main__":
# Simple test
manager = AccountManager()
print(f"Loaded {len(manager.accounts)} accounts")
for acc in manager.accounts:
print(f" - {acc.name}: {acc.username}")