-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.py
More file actions
71 lines (55 loc) · 1.7 KB
/
config.py
File metadata and controls
71 lines (55 loc) · 1.7 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
"""Config management — TOML file + env var + flag resolution."""
import os
import tomllib
from pathlib import Path
import tomli_w
def get_config_dir() -> Path:
xdg = os.environ.get("XDG_CONFIG_HOME")
if xdg:
base = Path(xdg)
elif os.name == "nt":
base = Path(os.environ.get("APPDATA", Path.home() / "AppData" / "Roaming"))
else:
base = Path.home() / ".config"
return base / "pumpfun-cli"
def get_config_path() -> Path:
return get_config_dir() / "config.toml"
def load_config() -> dict:
path = get_config_path()
if not path.exists():
return {}
with open(path, "rb") as f:
return tomllib.load(f)
def save_config_value(key: str, value: str):
config = load_config()
config[key] = value
path = get_config_path()
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "wb") as f:
tomli_w.dump(config, f)
def delete_config_value(key: str):
config = load_config()
if key not in config:
raise KeyError(key)
del config[key]
path = get_config_path()
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "wb") as f:
tomli_w.dump(config, f)
ENV_MAP = {
"rpc": "PUMPFUN_RPC",
"keyfile": "PUMPFUN_KEYFILE",
"priority_fee": "PUMPFUN_PRIORITY_FEE",
"compute_units": "PUMPFUN_COMPUTE_UNITS",
}
KNOWN_KEYS: frozenset[str] = frozenset(ENV_MAP)
def resolve_value(key: str, flag: str | None = None) -> str | None:
if flag is not None:
return flag
env_name = ENV_MAP.get(key)
if env_name:
env_val = os.environ.get(env_name)
if env_val is not None:
return env_val
config = load_config()
return config.get(key)