-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
64 lines (55 loc) · 2.03 KB
/
Copy pathstorage.py
File metadata and controls
64 lines (55 loc) · 2.03 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
"""Reads and writes the Hangar registry at ~/.hangar/apps.json.
An entry is a plain dict so it maps one-to-one onto the JSON you can
hand-edit. Nothing here knows about menus or processes on purpose:
you can exercise this whole file from a Python prompt.
Entry shape:
{
"name": "MyApp One",
"launch": {"type": "app", "path": "/Applications/MyApp.app"}
OR {"type": "shell", "command": "./run.sh", "working_dir": "~/proj"},
"update": {"command": "git pull", "working_dir": "~/proj"}, # optional
"notes": "the doorbell thing" # optional
}
"""
import json
import os
HANGAR_DIR = os.path.expanduser("~/.hangar")
APPS_FILE = os.path.join(HANGAR_DIR, "apps.json")
def load_apps():
"""Return the list of entries. Missing or empty file means no apps yet."""
if not os.path.exists(APPS_FILE):
return []
with open(APPS_FILE, "r") as f:
text = f.read().strip()
if not text:
return []
return json.loads(text)
def save_apps(apps):
"""Write the whole list back, pretty-printed so it stays readable."""
os.makedirs(HANGAR_DIR, exist_ok=True)
with open(APPS_FILE, "w") as f:
json.dump(apps, f, indent=2)
f.write("\n")
# Run this file directly to sanity-check reading and writing:
# .venv/bin/python3 storage.py
if __name__ == "__main__":
sample = [
{
"name": "Example Bundle",
"launch": {"type": "app", "path": "/System/Applications/Calculator.app"},
"notes": "just to prove load/save works",
},
{
"name": "Example Shell",
"launch": {"type": "shell", "command": "sleep 30", "working_dir": "~"},
"update": {"command": "git pull", "working_dir": "~"},
},
]
save_apps(sample)
print(f"Wrote {len(sample)} entries to {APPS_FILE}")
loaded = load_apps()
print(f"Read back {len(loaded)} entries:")
for entry in loaded:
print(" -", entry["name"])
assert loaded == sample, "round-trip mismatch"
print("Round-trip OK")