-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
72 lines (59 loc) · 2 KB
/
Copy path__init__.py
File metadata and controls
72 lines (59 loc) · 2 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
"""Language registry: plugin registration and language resolution."""
from __future__ import annotations
import inspect
from collections.abc import Callable
from pathlib import Path
from typing import TypeVar
from languages._framework import (
discovery,
registry_state,
resolution,
runtime,
)
from languages._framework.base.types import LangConfig
from languages._framework.contract_validation import validate_lang_contract
from languages._framework.policy import REQUIRED_DIRS, REQUIRED_FILES
from languages._framework.resolution import (
auto_detect_lang,
available_langs,
get_lang,
make_lang_config,
)
from languages._framework.structure_validation import validate_lang_structure
T = TypeVar("T")
def register_lang(name: str) -> Callable[[T], T]:
"""Decorator to register a language config class.
Validates structure, instantiates the class, validates the contract,
and stores the *instance* in the registry.
"""
def decorator(cls: T) -> T:
module = inspect.getmodule(cls)
if module and hasattr(module, "__file__"):
validate_lang_structure(Path(module.__file__).parent, name)
if isinstance(cls, type) and issubclass(cls, LangConfig):
cfg = make_lang_config(name, cls) # instantiate + validate
registry_state.register(name, cfg) # store instance
else:
registry_state.register(name, cls) # test doubles
return cls
return decorator
def register_generic_lang(name: str, cfg: LangConfig) -> None:
"""Register a pre-built language plugin instance (no package structure required)."""
validate_lang_contract(name, cfg)
registry_state.register(name, cfg)
__all__ = [
"REQUIRED_FILES",
"REQUIRED_DIRS",
"register_lang",
"register_generic_lang",
"get_lang",
"available_langs",
"auto_detect_lang",
"make_lang_config",
"validate_lang_structure",
"validate_lang_contract",
"discovery",
"registry_state",
"resolution",
"runtime",
]