-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_manager.py
More file actions
151 lines (118 loc) · 4.4 KB
/
config_manager.py
File metadata and controls
151 lines (118 loc) · 4.4 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
#!/usr/bin/env python3
"""
Config profile manager — list, view, and switch between configuration profiles.
Usage:
python config_manager.py list # List all available profiles
python config_manager.py view <profile> # View a specific profile
python config_manager.py switch <profile> # Switch to a profile
python config_manager.py current # Show the active profile
"""
import os
import json
import sys
from pathlib import Path
PROJECT_ROOT = Path(__file__).parent
CONFIG_DIR = PROJECT_ROOT / "config"
SETTINGS_FILE = CONFIG_DIR / "settings.json"
PROFILES_DIR = CONFIG_DIR / "profiles"
def load_json(path):
"""Load and parse a JSON file."""
with open(path, "r") as f:
return json.load(f)
def save_json(path, data):
"""Save data to a JSON file."""
with open(path, "w") as f:
json.dump(data, f, indent=4)
def list_profiles():
"""List all available profiles."""
if not PROFILES_DIR.exists():
print("❌ Profiles directory not found:", PROFILES_DIR)
return
profiles = sorted([f[:-5] for f in os.listdir(PROFILES_DIR) if f.endswith(".json")])
if not profiles:
print("❌ No profiles found in:", PROFILES_DIR)
return
active_profile = get_active_profile()
print("📋 Available profiles:")
for profile in profiles:
marker = " ✓" if profile == active_profile else ""
profile_file = PROFILES_DIR / f"{profile}.json"
profile_data = load_json(profile_file)
name = profile_data.get("name", profile)
description = profile_data.get("description", "")
print(f" {marker} {profile:15} → {name}")
if description:
print(f" {description}")
def view_profile(profile_name):
"""View the contents of a specific profile."""
profile_file = PROFILES_DIR / f"{profile_name}.json"
if not profile_file.exists():
print(f"❌ Profile '{profile_name}' not found")
list_profiles()
return
data = load_json(profile_file)
print(f"📄 Profile: {profile_name}")
print(json.dumps(data, indent=2))
def switch_profile(profile_name):
"""Switch to a different profile."""
profile_file = PROFILES_DIR / f"{profile_name}.json"
if not profile_file.exists():
print(f"❌ Profile '{profile_name}' not found")
list_profiles()
return
settings = load_json(SETTINGS_FILE)
settings["active_profile"] = profile_name
save_json(SETTINGS_FILE, settings)
profile_data = load_json(profile_file)
print(f"✅ Switched to profile: {profile_name}")
print(f" Name: {profile_data.get('name', 'N/A')}")
print(f" Description: {profile_data.get('description', 'N/A')}")
print(f" Provider: {profile_data.get('model', {}).get('provider', 'N/A')}")
def get_active_profile():
"""Get the currently active profile."""
if not SETTINGS_FILE.exists():
return None
settings = load_json(SETTINGS_FILE)
return settings.get("active_profile", "default")
def show_current():
"""Show the currently active profile."""
active = get_active_profile()
if not active:
print("❌ No active profile configured")
return
profile_file = PROFILES_DIR / f"{active}.json"
if not profile_file.exists():
print(f"❌ Active profile '{active}' not found")
return
data = load_json(profile_file)
print(f"🎯 Active profile: {active}")
print(f" Name: {data.get('name', 'N/A')}")
print(f" Description: {data.get('description', 'N/A')}")
print(f" Model provider: {data.get('model', {}).get('provider', 'N/A')}")
print(f" Model: {data.get('model', {}).get('model', 'N/A')}")
def main():
if len(sys.argv) < 2:
print(__doc__)
return
command = sys.argv[1].lower()
if command == "list":
list_profiles()
elif command == "view":
if len(sys.argv) < 3:
print("Usage: python config_manager.py view <profile>")
list_profiles()
return
view_profile(sys.argv[2])
elif command == "switch":
if len(sys.argv) < 3:
print("Usage: python config_manager.py switch <profile>")
list_profiles()
return
switch_profile(sys.argv[2])
elif command == "current":
show_current()
else:
print(f"❌ Unknown command: {command}")
print(__doc__)
if __name__ == "__main__":
main()