-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_manager.py
More file actions
70 lines (54 loc) · 1.94 KB
/
config_manager.py
File metadata and controls
70 lines (54 loc) · 1.94 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
#!/usr/bin/env python3
import json
import os
from datetime import datetime
from typing import Optional
class ConfigManager:
def __init__(self, config_dir: str = "config"):
"""
Initialize configuration manager.
Args:
config_dir: Directory containing configuration files
"""
self.config_dir = config_dir
self.profiles_dir = os.path.join(config_dir, "profiles")
self.current_config_file = os.path.join(config_dir, "active_profile.json")
def set_active_profile(self, profile_name: str) -> None:
"""
Set the active profile in configuration.
Args:
profile_name: Name of profile to activate
"""
os.makedirs(self.config_dir, exist_ok=True)
data = {
"profile": profile_name,
"switched_at": datetime.now().isoformat(),
}
with open(self.current_config_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
def get_active_profile(self) -> Optional[str]:
"""
Retrieve the currently active profile name.
Returns:
Profile name or None if not set
"""
if not os.path.exists(self.current_config_file):
return None
with open(self.current_config_file, "r", encoding="utf-8") as f:
data = json.load(f)
return data.get("profile")
def list_available_profiles(self) -> list:
"""
List all available profile files.
Returns:
List of profile names
"""
if not os.path.isdir(self.profiles_dir):
return []
profiles = []
for file_name in os.listdir(self.profiles_dir):
if file_name.endswith(".yaml"):
profiles.append(os.path.splitext(file_name)[0])
return sorted(profiles)
def profile_exists(self, profile_name: str) -> bool:
return profile_name in self.list_available_profiles()