Repository navigation
Expand file tree
/
Copy path_registry.py
More file actions
92 lines (80 loc) · 2.57 KB
/
Copy path_registry.py
File metadata and controls
92 lines (80 loc) · 2.57 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
"""Local app registry used by the MCP bridge for app-id routing."""
from __future__ import annotations
import json
import os
import time
from glob import glob
from typing import Any
_CACHE_DIR = os.path.join(
os.environ.get('XDG_CACHE_HOME', os.path.expanduser('~/.cache')),
'python-devtools',
)
_REGISTRY_DIR = os.path.join(_CACHE_DIR, 'registry')
def register_app(
*,
app_id: str,
host: str,
port: int,
readonly: bool,
pid: int | None = None,
) -> str:
"""Write one registry entry and return the file path."""
os.makedirs(_REGISTRY_DIR, exist_ok=True)
now = time.time()
process_id = os.getpid() if pid is None else pid
entry = {
'app_id': str(app_id),
'host': str(host),
'port': int(port),
'readonly': bool(readonly),
'pid': int(process_id),
'started_at': now,
'instance_id': f'{process_id}-{int(now * 1000)}-{int(port)}',
}
path = os.path.join(_REGISTRY_DIR, f"{entry['instance_id']}.json")
tmp = f'{path}.tmp'
with open(tmp, 'w', encoding='utf-8') as f:
json.dump(entry, f, separators=(',', ':'))
os.replace(tmp, path)
return path
def unregister_app(path: str | None) -> None:
"""Remove one registry entry path if present."""
if not path:
return
try:
os.remove(path)
except FileNotFoundError:
pass
def list_registered_apps() -> list[dict[str, Any]]:
"""Return all syntactically valid registry entries."""
entries: list[dict[str, Any]] = []
if not os.path.isdir(_REGISTRY_DIR):
return entries
for path in glob(os.path.join(_REGISTRY_DIR, '*.json')):
try:
with open(path, encoding='utf-8') as f:
raw = json.load(f)
except (FileNotFoundError, OSError, json.JSONDecodeError):
continue
app_id = raw.get('app_id')
host = raw.get('host')
port = raw.get('port')
if not isinstance(app_id, str) or not app_id:
continue
if not isinstance(host, str) or not host:
continue
if not isinstance(port, int):
continue
entries.append(
{
'app_id': app_id,
'host': host,
'port': port,
'readonly': bool(raw.get('readonly', False)),
'pid': int(raw.get('pid', 0)),
'started_at': float(raw.get('started_at', 0.0)),
'instance_id': str(raw.get('instance_id', os.path.basename(path))),
'registry_path': path,
}
)
return entries