Factory
framework3.base.base_factory
¶
__all__ = ['BaseFactory']
module-attribute
¶
BaseFactory
¶
Bases: Generic[TypePlugable]
A generic factory class for managing and creating pluggable components.
This class provides a flexible way to register, retrieve, and manage different types of components (plugins) in the framework.
Key Features
- Dynamic registration and retrieval of components
- Support for attribute-style and dictionary-style access
- Iteration over registered components
- Rich printing of available components
Usage
To create a new factory for a specific type of component, inherit from this class and specify the type of components it will manage. For example:
from framework3.base.base_factory import BaseFactory
from framework3.base.base_plugin import BasePlugin
class MyComponentFactory(BaseFactory[BasePlugin]):
pass
factory = MyComponentFactory()
factory['ComponentA'] = ComponentA
factory['ComponentB'] = ComponentB
component_a = factory['ComponentA']()
component_b = factory['ComponentB']()
Attributes:
Name | Type | Description |
---|---|---|
_foundry |
Dict[str, Type[TypePlugable]]
|
Internal dictionary to store registered components. |
Methods:
Name | Description |
---|---|
__getattr__ |
str) -> Type[TypePlugable]: Retrieve a component by attribute access. |
__setattr__ |
str, value: Type[TypePlugable]) -> None: Set a component by attribute assignment. |
__setitem__ |
str, value: Type[TypePlugable]) -> None: Set a component using dictionary-like syntax. |
__getitem__ |
str, default: Type[TypePlugable] | None = None) -> Type[TypePlugable]: Retrieve a component using dictionary-like syntax. |
__iter__ |
Provide an iterator over the registered components. |
__contains__ |
str) -> bool: Check if a component is registered in the factory. |
get |
str, default: Type[TypePlugable] | None = None) -> Type[TypePlugable]: Retrieve a component by name. |
print_available_components |
Print a list of all available components in the factory. |
Note
This class uses Generic[TypePlugable] to allow type hinting for the specific type of components managed by the factory.
Source code in framework3/base/base_factory.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
|
__contains__(item)
¶
Check if a component is registered in the factory.
This method allows the use of the 'in' operator to check for component existence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item
|
str
|
The name of the component to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the component is registered, False otherwise. |
Source code in framework3/base/base_factory.py
__getattr__(name)
¶
Retrieve a component by attribute access.
This method allows components to be accessed as if they were attributes of the factory instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the component to retrieve. |
required |
Returns:
Type | Description |
---|---|
Type[TypePlugable]
|
Type[TypePlugable]: The requested component class. |
Raises:
Type | Description |
---|---|
AttributeError
|
If the component is not found in the factory. |
Source code in framework3/base/base_factory.py
__getitem__(name, default=None)
¶
Retrieve a component using dictionary-like syntax.
This method allows components to be accessed using dictionary-style item retrieval.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the component to retrieve. |
required |
default
|
Type[TypePlugable] | None
|
Default value if component is not found. |
None
|
Returns:
Type | Description |
---|---|
Type[TypePlugable]
|
Type[TypePlugable]: The requested component class or the default value. |
Raises:
Type | Description |
---|---|
AttributeError
|
If the component is not found and no default is provided. |
Source code in framework3/base/base_factory.py
__init__()
¶
__iter__()
¶
Provide an iterator over the registered components.
This method allows iteration over the (name, component) pairs in the factory.
Returns:
Type | Description |
---|---|
Iterator[Tuple[str, Type[TypePlugable]]]
|
Iterator[Tuple[str, Type[TypePlugable]]]: An iterator of (name, component) pairs. |
Example
Source code in framework3/base/base_factory.py
__setattr__(name, value)
¶
Set a component by attribute assignment.
This method allows components to be registered as if they were attributes of the factory instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name to assign to the component. |
required |
value
|
Type[TypePlugable]
|
The component class to register. |
required |
Source code in framework3/base/base_factory.py
__setitem__(name, value)
¶
Set a component using dictionary-like syntax.
This method allows components to be registered using dictionary-style item assignment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name to assign to the component. |
required |
value
|
Type[TypePlugable]
|
The component class to register. |
required |
Source code in framework3/base/base_factory.py
get(name, default=None)
¶
Retrieve a component by name.
This method provides a way to safely retrieve components with an optional default value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the component to retrieve. |
required |
default
|
Type[TypePlugable] | None
|
Default value if component is not found. |
None
|
Returns:
Type | Description |
---|---|
Type[TypePlugable]
|
Type[TypePlugable]: The requested component class or the default value. |
Raises:
Type | Description |
---|---|
AttributeError
|
If the component is not found and no default is provided. |
Example
Source code in framework3/base/base_factory.py
print_available_components()
¶
Print a list of all available components in the factory.
This method uses rich formatting to display the components in a visually appealing way.