Skip to content

Commit

Permalink
registry: Add before init hook
Browse files Browse the repository at this point in the history
  • Loading branch information
lmignon committed Jul 28, 2023
1 parent 30760a6 commit 799999a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions news/before_hook.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add a `before_init_registry` hook method into the `ExtendableRegistryListener` class.
This methods allows you to udpate the list of modules to load into the registry if
you need to do so.
17 changes: 17 additions & 0 deletions src/extendable/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ class ExtendableRegistryListener:
def on_registry_initialized(self, registry: "ExtendableClassesRegistry") -> None:
...

def before_init_registry(
self,
registry: "ExtendableClassesRegistry",
module_matchings: Optional[List[str]] = None,
) -> None:
"""Called before the registry is initialized.
This hook allows you to add your own specific module matching
rules into the provided one. A common use case is when you
define a base Extendable class into a specific python module and
you want to ensure that this base class is always loaded when
the registry is initialized.
"""
...


class ExtendableClassesRegistry:
"""Store all the extendableClasses and allow to retrieve them by name.
Expand Down Expand Up @@ -158,6 +173,8 @@ def init_registry(self, module_matchings: Optional[List[str]] = None) -> None:
The module list accept wildcard expression as last character
"""
module_matchings = module_matchings if module_matchings else ["*"]
for listener in self.listeners:
listener.before_init_registry(self, module_matchings)
with self.build_mode(), ModuleIndex() as idx:
for match in module_matchings:
for module in idx.get_modules(match):
Expand Down

0 comments on commit 799999a

Please sign in to comment.