-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpm_config.py
More file actions
54 lines (40 loc) · 1.47 KB
/
Copy pathpm_config.py
File metadata and controls
54 lines (40 loc) · 1.47 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
"""
pm_config.py - Configuration Management
Handles loading/saving the user configuration file (~/.puppetmaster_config.json).
"""
import json
import sys
from pathlib import Path
# Store in home directory so it survives puppetmaster directory deletions
CONFIG_FILE = Path.home() / ".puppetmaster_config.json"
def load_config():
"""Load saved configuration (output directories, etc.)"""
try:
if CONFIG_FILE.exists():
return json.loads(CONFIG_FILE.read_text())
except (json.JSONDecodeError, OSError):
pass
return {"output_dirs": []}
def save_config(config):
"""Save configuration to disk"""
try:
CONFIG_FILE.write_text(json.dumps(config, indent=2))
return True
except OSError as e:
print(f"\n\033[31mWarning: Failed to save config: {e}\033[0m", file=sys.stderr)
return False
def remember_output_dir(path):
"""Remember an output directory for later retrieval"""
config = load_config()
abs_path = str(Path(path).resolve())
# Add to front of list (most recent first), avoid duplicates
if abs_path in config["output_dirs"]:
config["output_dirs"].remove(abs_path)
config["output_dirs"].insert(0, abs_path)
# Keep only last 20 directories
config["output_dirs"] = config["output_dirs"][:20]
save_config(config)
def get_remembered_output_dirs():
"""Get list of previously used output directories"""
config = load_config()
return config.get("output_dirs", [])