Refactor plugin registry into separate files per plugin
This commit is contained in:
+40
-22
@@ -58,33 +58,51 @@ def _load_toml(path: Path) -> dict[str, Any]:
|
||||
return tomllib.load(handle)
|
||||
|
||||
|
||||
def _registry_plugin_from_item(item: dict[str, Any], source: Path) -> RegistryPlugin:
|
||||
dependencies = tuple(
|
||||
Dependency(
|
||||
id=dep["id"],
|
||||
constraint=dep.get("constraint"),
|
||||
required=dep.get("required", True),
|
||||
)
|
||||
for dep in item.get("dependencies", [])
|
||||
)
|
||||
strategy = item.get("install_strategy", "manual")
|
||||
if strategy not in VALID_STRATEGIES:
|
||||
raise ValueError(f"{source}: invalid install_strategy for {item['id']}: {strategy}")
|
||||
return RegistryPlugin(
|
||||
id=item["id"],
|
||||
name=item.get("name", item["id"]),
|
||||
repo=item.get("repo"),
|
||||
asset_patterns=tuple(item.get("asset_patterns", [])),
|
||||
install_strategy=strategy,
|
||||
category=item.get("category"),
|
||||
dependencies=dependencies,
|
||||
)
|
||||
|
||||
|
||||
def _add_registry_plugin(plugins: dict[str, RegistryPlugin], plugin: RegistryPlugin, source: Path) -> None:
|
||||
if plugin.id in plugins:
|
||||
raise ValueError(f"{source}: duplicate plugin id: {plugin.id}")
|
||||
plugins[plugin.id] = plugin
|
||||
|
||||
|
||||
def load_registry(path: Path) -> Registry:
|
||||
if not path.exists():
|
||||
legacy_path = path.with_suffix(".toml")
|
||||
if path.name == "plugins" and legacy_path.exists():
|
||||
return load_registry(legacy_path)
|
||||
return Registry()
|
||||
data = _load_toml(path)
|
||||
plugins: dict[str, RegistryPlugin] = {}
|
||||
if path.is_dir():
|
||||
for item_path in sorted(path.glob("*.toml")):
|
||||
data = _load_toml(item_path)
|
||||
_add_registry_plugin(plugins, _registry_plugin_from_item(data, item_path), item_path)
|
||||
return Registry(plugins)
|
||||
|
||||
data = _load_toml(path)
|
||||
for item in data.get("plugins", []):
|
||||
dependencies = tuple(
|
||||
Dependency(
|
||||
id=dep["id"],
|
||||
constraint=dep.get("constraint"),
|
||||
required=dep.get("required", True),
|
||||
)
|
||||
for dep in item.get("dependencies", [])
|
||||
)
|
||||
strategy = item.get("install_strategy", "manual")
|
||||
if strategy not in VALID_STRATEGIES:
|
||||
raise ValueError(f"{path}: invalid install_strategy for {item['id']}: {strategy}")
|
||||
plugin = RegistryPlugin(
|
||||
id=item["id"],
|
||||
name=item.get("name", item["id"]),
|
||||
repo=item.get("repo"),
|
||||
asset_patterns=tuple(item.get("asset_patterns", [])),
|
||||
install_strategy=strategy,
|
||||
category=item.get("category"),
|
||||
dependencies=dependencies,
|
||||
)
|
||||
plugins[plugin.id] = plugin
|
||||
_add_registry_plugin(plugins, _registry_plugin_from_item(item, path), path)
|
||||
return Registry(plugins)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user