55from __future__ import annotations
66
77from collections import defaultdict
8- from typing import TYPE_CHECKING , Any , cast , final
8+ from importlib import metadata
9+ from typing import TYPE_CHECKING , cast , final
910
1011from typing_extensions import TypeVar , override
1112
12- from betty import plugin
1313from betty .concurrent import AsynchronizedLock , Ledger
14- from betty .plugin import PluginDefinition
14+ from betty .plugin import PluginDefinition , PluginTypeDefinition
15+ from betty .plugin .collections import (
16+ PluginDefinitions ,
17+ new_plugin_definitions ,
18+ new_plugin_type_definitions ,
19+ )
1520from betty .plugin .manager import PluginManager
16- from betty .plugin .repository .static import StaticPluginRepository
1721
1822if TYPE_CHECKING :
1923 from collections .abc import MutableMapping
2024
2125 from betty .collections import KeyedCollection
2226 from betty .machine_name import MachineName
23- from betty .plugin .repository import PluginRepository
24- from betty .plugin .resolve import ResolvableId
2527 from betty .service .level import ServiceLevel
2628
2729
@@ -39,52 +41,54 @@ class ServiceLevelPluginManager(PluginManager):
3941
4042 def __init__ (self , services : ServiceLevel , / ):
4143 self ._services = services
42- self ._types = plugin .PluginTypeRepository ()
43- self ._plugin_repositories : MutableMapping [
44- type [PluginDefinition ], PluginRepository [Any ] | None
44+ self ._types = new_plugin_type_definitions (
45+ * (
46+ entry_point .load ()
47+ for entry_point in metadata .entry_points (group = "betty.plugin" )
48+ )
49+ )
50+ self ._plugins : MutableMapping [
51+ type [PluginDefinition ], PluginDefinitions | None
4552 ] = defaultdict (None )
4653 self ._ledger = Ledger (AsynchronizedLock .new_threadsafe ())
4754
4855 @override
4956 @property
50- def types (self ) -> plugin . PluginTypeRepository :
57+ def types (self ) -> KeyedCollection [ MachineName , MachineName , PluginTypeDefinition ] :
5158 return self ._types
5259
5360 @override
5461 async def plugins (
5562 self , plugin_type : type [_PluginDefinitionT ] | MachineName , /
56- ) -> KeyedCollection [
57- MachineName , ResolvableId [_PluginDefinitionT ], _PluginDefinitionT
58- ]:
63+ ) -> PluginDefinitions [_PluginDefinitionT ]:
5964 if isinstance (plugin_type , str ):
6065 plugin_type = cast (type [_PluginDefinitionT ], self .types [plugin_type ])
61- repository : PluginRepository [_PluginDefinitionT ] | None
66+ plugins : PluginDefinitions [_PluginDefinitionT ] | None
6267 if plugin_type .type ().discoverer .overridden :
6368 return await self ._new (plugin_type )
64- # If the repository exists already, return it immediately so we avoid acquiring locks.
65- repository = self ._get (plugin_type )
66- if repository :
67- return repository
69+ # If the definitions exist already, return them immediately so we avoid acquiring locks.
70+ plugins = self ._get (plugin_type )
71+ if plugins :
72+ return plugins
6873 async with self ._ledger .ledger (f"{ plugin_type .type ().id } " ):
69- # The repository may have been created since we first checked.
70- repository = self ._get (plugin_type )
71- if repository :
72- return repository
73- repository = await self ._new (plugin_type )
74- self ._plugin_repositories [plugin_type ] = repository
75- return repository
74+ # The definitions may have been created since we first checked.
75+ plugins = self ._get (plugin_type )
76+ if plugins :
77+ return plugins
78+ plugins = await self ._new (plugin_type )
79+ self ._plugins [plugin_type ] = plugins
80+ return plugins
7681
7782 def _get (
7883 self , plugin_type : type [_PluginDefinitionT ]
79- ) -> PluginRepository [_PluginDefinitionT ] | None :
80- if plugin_type not in self ._plugin_repositories :
84+ ) -> PluginDefinitions [_PluginDefinitionT ] | None :
85+ if plugin_type not in self ._plugins :
8186 return None
82- return self ._plugin_repositories [plugin_type ]
87+ return self ._plugins [plugin_type ]
8388
8489 async def _new (
8590 self , plugin_type : type [_PluginDefinitionT ]
86- ) -> PluginRepository [_PluginDefinitionT ]:
87- return StaticPluginRepository (
88- plugin_type ,
89- * await plugin_type .type ().discoverer .discover (services = self ._services ),
91+ ) -> PluginDefinitions [_PluginDefinitionT ]:
92+ return new_plugin_definitions (
93+ * await plugin_type .type ().discoverer .discover (services = self ._services )
9094 )
0 commit comments