cgcardona / muse public
registry.py python
110 lines 3.7 KB
9ee9c39c refactor: rename music→midi domain, strip all 5-dim backward compat Gabriel Cardona <gabriel@tellurstori.com> 1d ago
1 """Plugin registry — maps domain names to :class:`~muse.domain.MuseDomainPlugin` instances.
2
3 Every CLI command that operates on domain state calls :func:`resolve_plugin`
4 once to obtain the active plugin for the current repository. Adding support
5 for a new domain requires only two changes:
6
7 1. Implement :class:`~muse.domain.MuseDomainPlugin` in a new module under
8 ``muse/plugins/<domain>/plugin.py``.
9 2. Register the plugin instance in ``_REGISTRY`` below.
10
11 The domain for a repository is stored in ``.muse/repo.json`` under the key
12 ``"domain"``. Repositories created before this key was introduced default to
13 ``'midi'``.
14 """
15 from __future__ import annotations
16
17 import json
18 import pathlib
19
20 from muse.core.errors import MuseCLIError
21 from muse.core.schema import DomainSchema
22 from muse.domain import MuseDomainPlugin
23 from muse.plugins.code.plugin import CodePlugin
24 from muse.plugins.midi.plugin import MidiPlugin
25 from muse.plugins.scaffold.plugin import ScaffoldPlugin
26
27 _REGISTRY: dict[str, MuseDomainPlugin] = {
28 "code": CodePlugin(),
29 "midi": MidiPlugin(),
30 "scaffold": ScaffoldPlugin(),
31 }
32
33 _DEFAULT_DOMAIN = "midi"
34
35
36 def _read_domain(root: pathlib.Path) -> str:
37 """Return the domain name stored in ``.muse/repo.json``.
38
39 Falls back to ``'midi'`` for repos that pre-date the ``domain`` field.
40 """
41 repo_json = root / ".muse" / "repo.json"
42 try:
43 data = json.loads(repo_json.read_text())
44 domain = data.get("domain")
45 return str(domain) if domain else _DEFAULT_DOMAIN
46 except (OSError, json.JSONDecodeError):
47 return _DEFAULT_DOMAIN
48
49
50 def resolve_plugin(root: pathlib.Path) -> MuseDomainPlugin:
51 """Return the active domain plugin for the repository at *root*.
52
53 Reads the ``"domain"`` key from ``.muse/repo.json`` and looks it up in
54 the plugin registry. Raises :class:`~muse.core.errors.MuseCLIError` if
55 the domain is not registered.
56
57 Args:
58 root: Repository root directory (contains ``.muse/``).
59
60 Returns:
61 The :class:`~muse.domain.MuseDomainPlugin` instance for this repo.
62
63 Raises:
64 MuseCLIError: When the domain stored in ``repo.json`` is not in the
65 registry. This is a configuration error — either the plugin was
66 not installed or ``repo.json`` was edited manually.
67 """
68 domain = _read_domain(root)
69 plugin = _REGISTRY.get(domain)
70 if plugin is None:
71 registered = ", ".join(sorted(_REGISTRY))
72 raise MuseCLIError(
73 f"Unknown domain {domain!r}. Registered domains: {registered}"
74 )
75 return plugin
76
77
78 def read_domain(root: pathlib.Path) -> str:
79 """Return the domain name for the repository at *root*.
80
81 This is the same lookup used internally by :func:`resolve_plugin`.
82 Use it when you need the domain string to construct a
83 :class:`~muse.domain.SnapshotManifest` for a stored manifest.
84 """
85 return _read_domain(root)
86
87
88 def registered_domains() -> list[str]:
89 """Return the sorted list of registered domain names."""
90 return sorted(_REGISTRY)
91
92
93 def schema_for(domain: str) -> DomainSchema | None:
94 """Return the ``DomainSchema`` for *domain*, or ``None`` if not registered.
95
96 Allows the CLI and merge engine to look up a domain's schema without
97 holding a plugin instance. Returns ``None`` rather than raising so callers
98 can decide whether an unknown domain is an error or a soft miss.
99
100 Args:
101 domain: Domain name string (e.g. ``'midi'``).
102
103 Returns:
104 The :class:`~muse.core.schema.DomainSchema` declared by the plugin,
105 or ``None`` if *domain* is not in the registry.
106 """
107 plugin = _REGISTRY.get(domain)
108 if plugin is None:
109 return None
110 return plugin.schema()