Core
๐ค AI-Generated Content
This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
provide.foundation.hub.core
¶
Attributes¶
T
module-attribute
¶
Core Hub class for component and command management.
This module provides the core Hub functionality for registering and managing components and commands, without Foundation-specific features.
Classes¶
CoreHub
¶
CoreHub(
context: CLIContext | None = None,
component_registry: Registry | None = None,
command_registry: Registry | None = None,
)
Core hub for managing components and commands.
The CoreHub provides basic functionality for: - Registering components and commands - Managing component lifecycle - Creating Click CLI applications
Does not include Foundation-specific initialization.
Initialize the core hub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
CLIContext | None
|
Foundation CLIContext for configuration |
None
|
component_registry
|
Registry | None
|
Custom component registry |
None
|
command_registry
|
Registry | None
|
Custom command registry |
None
|
Source code in provide/foundation/hub/core.py
Functions¶
__enter__
¶
__exit__
¶
add_cli_group
¶
Add an existing Click group to the hub.
This registers all commands from the group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
Group
|
Click Group to add |
required |
Source code in provide/foundation/hub/core.py
add_command
¶
add_command(
func: Callable[..., Any] | Command,
name: str | None = None,
**kwargs: Any,
) -> CommandInfo
Add a CLI command to the hub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any] | Command
|
Command function or Click command |
required |
name
|
str | None
|
Optional name (defaults to function name) |
None
|
**kwargs
|
Any
|
Additional command options |
{}
|
Returns:
| Type | Description |
|---|---|
CommandInfo
|
CommandInfo for the registered command |
Source code in provide/foundation/hub/core.py
add_component
¶
add_component(
component_class: type[Any],
name: str | None = None,
dimension: str = ComponentCategory.COMPONENT.value,
**metadata: Any,
) -> ComponentInfo
Add a component to the hub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component_class
|
type[Any]
|
Component class to register |
required |
name
|
str | None
|
Optional name (defaults to class name) |
None
|
dimension
|
str
|
Registry dimension |
COMPONENT.value
|
**metadata
|
Any
|
Additional metadata |
{}
|
Returns:
| Type | Description |
|---|---|
ComponentInfo
|
ComponentInfo for the registered component |
Raises:
| Type | Description |
|---|---|
AlreadyExistsError
|
If component is already registered |
ValidationError
|
If component class is invalid |
Source code in provide/foundation/hub/core.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
cleanup
¶
Cleanup all components that support cleanup.
Source code in provide/foundation/hub/core.py
clear
¶
Clear registrations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension
|
str | None
|
Optional dimension to clear (None = all) |
None
|
Source code in provide/foundation/hub/core.py
create_cli
¶
Create a Click CLI with all registered commands.
Requires click to be installed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
CLI name |
'cli'
|
version
|
str | None
|
CLI version |
None
|
**kwargs
|
Any
|
Additional Click Group options |
{}
|
Returns:
| Type | Description |
|---|---|
Group
|
Click Group with registered commands |
Source code in provide/foundation/hub/core.py
discover_components
¶
discover_components(
group: str,
dimension: str = ComponentCategory.COMPONENT.value,
) -> dict[str, type[Any]]
Discover and register components from entry points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
str
|
Entry point group name |
required |
dimension
|
str
|
Dimension to register under |
COMPONENT.value
|
Returns:
| Type | Description |
|---|---|
dict[str, type[Any]]
|
Dictionary of discovered components |
Source code in provide/foundation/hub/core.py
get_command
¶
get_component
¶
initialize
¶
Initialize all components that support initialization.
Source code in provide/foundation/hub/core.py
list_components
¶
List component names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension
|
str | None
|
Optional dimension filter |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of component names |
Source code in provide/foundation/hub/core.py
register
¶
Register a dependency by type for dependency injection.
This enables type-based registration which is the foundation of the dependency injection pattern. Use this in your application's composition root (e.g., main.py) to wire up dependencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_hint
|
type[T]
|
Type to register under |
required |
instance
|
T
|
Instance to register |
required |
name
|
str | None
|
Optional name for named registration |
None
|
Example
hub = Hub() hub.register(DatabaseClient, db_instance) hub.register(HTTPClient, http_instance) service = hub.resolve(MyService) # Auto-injects
See Also
- resolve(): Create instances with auto-injected dependencies
- @injectable: Decorator to mark DI-ready classes
Source code in provide/foundation/hub/core.py
resolve
¶
Create an instance with dependency injection.
Inspects the class constructor, resolves dependencies from the registry, and instantiates the class. This is the core of the dependency injection pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
Class to instantiate |
required |
**overrides
|
Any
|
Explicitly provided dependencies (override registry) |
{}
|
Returns:
| Type | Description |
|---|---|
T
|
New instance with dependencies injected |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If required dependency not registered |
ValidationError
|
If instantiation fails |
Example
@injectable class UserService: ... def init(self, db: Database, logger: Logger): ... self.db = db ... self.logger = logger
hub = Hub() hub.register(Database, db_instance) hub.register(Logger, logger_instance) service = hub.resolve(UserService) # Auto-injects db & logger
Pattern
This implements the Dependency Injection pattern with an explicit Composition Root. The Hub acts as a DI Container that: 1. Stores registered dependencies by type 2. Inspects constructor signatures 3. Automatically wires dependencies together
See Also
- register(): Register dependencies by type
- @injectable: Decorator to mark DI-ready classes