-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
123 lines (93 loc) · 3.2 KB
/
config.py
File metadata and controls
123 lines (93 loc) · 3.2 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
"""Config management for robotmcp-server."""
import json
import os
from pathlib import Path
from typing import Optional
CONFIG_DIR = Path.home() / ".robotmcp-server"
CONFIG_FILE = CONFIG_DIR / "config.json"
class Config:
"""Configuration container."""
def __init__(self, data: dict = None):
self.data = data or {}
@property
def user_id(self) -> Optional[str]:
return self.data.get("user_id")
@property
def email(self) -> Optional[str]:
return self.data.get("email")
@property
def access_token(self) -> Optional[str]:
return self.data.get("access_token")
@property
def refresh_token(self) -> Optional[str]:
return self.data.get("refresh_token")
@property
def robot_name(self) -> Optional[str]:
return self.data.get("robot_name")
@property
def tunnel_token(self) -> Optional[str]:
return self.data.get("tunnel_token")
@property
def tunnel_url(self) -> Optional[str]:
return self.data.get("tunnel_url")
def is_valid(self) -> bool:
"""Check if config has required fields."""
return bool(self.user_id and self.email and self.access_token)
def has_tunnel(self) -> bool:
"""Check if tunnel is configured."""
return bool(self.robot_name and self.tunnel_token)
def load_config() -> Config:
"""Load config from file."""
if not CONFIG_FILE.exists():
return Config()
try:
with open(CONFIG_FILE, "r") as f:
data = json.load(f)
if not isinstance(data, dict):
return Config()
# Ensure all values are strings or None (guard against manual edits)
for key in (
"user_id",
"email",
"access_token",
"refresh_token",
"robot_name",
"tunnel_token",
"tunnel_url",
):
if key in data and data[key] is not None and not isinstance(data[key], str):
data[key] = str(data[key])
return Config(data)
except (json.JSONDecodeError, IOError):
return Config()
def save_config(
user_id: str, email: str, access_token: str, refresh_token: str = None
) -> None:
"""Save config to file."""
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
data = {
"user_id": user_id,
"email": email,
"access_token": access_token,
"refresh_token": refresh_token,
}
with open(CONFIG_FILE, "w") as f:
json.dump(data, f, indent=2)
# Set restrictive permissions (owner read/write only)
os.chmod(CONFIG_FILE, 0o600)
def update_config_tunnel(robot_name: str, tunnel_token: str, tunnel_url: str) -> None:
"""Update existing config with tunnel information."""
config = load_config()
if not config.is_valid():
raise ValueError("Cannot update tunnel: no valid config exists")
data = config.data.copy()
data["robot_name"] = robot_name
data["tunnel_token"] = tunnel_token
data["tunnel_url"] = tunnel_url
with open(CONFIG_FILE, "w") as f:
json.dump(data, f, indent=2)
os.chmod(CONFIG_FILE, 0o600)
def clear_config() -> None:
"""Remove config file."""
if CONFIG_FILE.exists():
CONFIG_FILE.unlink()