|
| 1 | +""" |
| 2 | +title: Language plugin interface. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from abc import ABC, abstractmethod |
| 8 | +from typing import Dict, Optional, Type |
| 9 | + |
| 10 | +from douki._base.config import BaseConfig |
| 11 | + |
| 12 | + |
| 13 | +class BaseLanguage(ABC): |
| 14 | + """ |
| 15 | + title: Abstract base class for a language plugin. |
| 16 | + summary: >- |
| 17 | + Plugins subclass this to bind their language-specific extractor, sync |
| 18 | + logic, and configuration. |
| 19 | + """ |
| 20 | + |
| 21 | + @property |
| 22 | + @abstractmethod |
| 23 | + def name(self) -> str: |
| 24 | + """ |
| 25 | + title: The name of the language (e.g. 'python'). |
| 26 | + returns: |
| 27 | + type: str |
| 28 | + """ |
| 29 | + ... # pragma: no cover |
| 30 | + |
| 31 | + @property |
| 32 | + @abstractmethod |
| 33 | + def config(self) -> BaseConfig: |
| 34 | + """ |
| 35 | + title: Configuration loader for this language. |
| 36 | + returns: |
| 37 | + type: BaseConfig |
| 38 | + """ |
| 39 | + ... # pragma: no cover |
| 40 | + |
| 41 | + @abstractmethod |
| 42 | + def sync_source( |
| 43 | + self, source: str, *, migrate: Optional[str] = None |
| 44 | + ) -> str: |
| 45 | + """ |
| 46 | + title: Synchronize docstrings in the given source code. |
| 47 | + parameters: |
| 48 | + source: |
| 49 | + type: str |
| 50 | + migrate: |
| 51 | + type: Optional[str] |
| 52 | + optional: true |
| 53 | + returns: |
| 54 | + type: str |
| 55 | + """ |
| 56 | + ... # pragma: no cover |
| 57 | + |
| 58 | + |
| 59 | +# --------------------------------------------------------------------------- |
| 60 | +# Registry |
| 61 | +# --------------------------------------------------------------------------- |
| 62 | + |
| 63 | +_REGISTRY: Dict[str, Type[BaseLanguage]] = {} |
| 64 | + |
| 65 | + |
| 66 | +def register_language(lang_class: Type[BaseLanguage]) -> None: |
| 67 | + """ |
| 68 | + title: Register a language plugin class. |
| 69 | + parameters: |
| 70 | + lang_class: |
| 71 | + type: Type[BaseLanguage] |
| 72 | + """ |
| 73 | + # Create a temporary instance just to get its name |
| 74 | + instance = lang_class() |
| 75 | + _REGISTRY[instance.name] = lang_class |
| 76 | + |
| 77 | + |
| 78 | +def get_language(name: str) -> BaseLanguage: |
| 79 | + """ |
| 80 | + title: Get an initialized language plugin by name. |
| 81 | + parameters: |
| 82 | + name: |
| 83 | + type: str |
| 84 | + returns: |
| 85 | + type: BaseLanguage |
| 86 | + """ |
| 87 | + if name not in _REGISTRY: |
| 88 | + raise ValueError(f"Language '{name}' is not registered.") |
| 89 | + return _REGISTRY[name]() |
| 90 | + |
| 91 | + |
| 92 | +def get_registered_language_names() -> list[str]: |
| 93 | + """ |
| 94 | + title: Return a list of registered language plugin names. |
| 95 | + returns: |
| 96 | + type: list[str] |
| 97 | + """ |
| 98 | + return list(_REGISTRY.keys()) |
0 commit comments