Registry API
sweet_tea.registry.Registry
Global registry for class definitions that can be instantiated via factories.
This registry automatically discovers and registers classes from packages, supporting optional dependencies that may not be installed. Classes with missing dependencies are skipped with a warning rather than failing registration.
The registry supports typed lookups for abstract factories, allowing filtering by inheritance hierarchy.
Thread-safe: All registry operations are synchronized to prevent race conditions in multi-threaded environments.
Source code in sweet_tea/registry.py
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 | |
__add_entry_to_registry(label, library, name_of_package)
classmethod
Import a module and register all classes defined in it.
Handles optional dependencies by issuing warnings for ImportError/ModuleNotFoundError and continuing, while raising SweetTeaError for other exceptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
Optional label for categorizing classes. |
required |
library
|
str
|
Name of the library the classes belong to. |
required |
name_of_package
|
str
|
Full module name to import and scan. |
required |
Raises:
| Type | Description |
|---|---|
SweetTeaError
|
For non-import related errors during module processing. |
Source code in sweet_tea/registry.py
entries()
classmethod
fill_registry(path=None, module=None, library='', label='')
classmethod
Recursively scan and register classes from packages starting from the given path.
This method automatically discovers all classes in the package hierarchy, supporting optional dependencies by gracefully skipping modules that fail to import due to missing packages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | None
|
Package path where modules are located. If None, uses the caller's module path. |
None
|
module
|
str | None
|
Name of the root module. If None, inferred from path. |
None
|
library
|
str
|
Name of the library for categorization. |
''
|
label
|
str
|
Optional label for categorizing classes. |
''
|
Source code in sweet_tea/registry.py
register(key, class_def, library='', label='')
classmethod
Register a class with the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Name used to reference the class for instantiation. |
required |
class_def
|
type
|
The class type to register. |
required |
library
|
str
|
Name of the library the class belongs to. |
''
|
label
|
str
|
Optional label for categorizing classes (e.g., for different environments). |
''
|
Source code in sweet_tea/registry.py
typed_entries(lookup_type=Any)
classmethod
Get entries that are subclasses of the specified type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lookup_type
|
Any
|
The base class to filter by. Defaults to Any. |
Any
|
Returns:
| Type | Description |
|---|---|
list[Entry]
|
List of entries where the class_def is a subclass of lookup_type. |
Source code in sweet_tea/registry.py
Methods
register()
sweet_tea.registry.Registry.register(key, class_def, library='', label='')
classmethod
Register a class with the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Name used to reference the class for instantiation. |
required |
class_def
|
type
|
The class type to register. |
required |
library
|
str
|
Name of the library the class belongs to. |
''
|
label
|
str
|
Optional label for categorizing classes (e.g., for different environments). |
''
|
Source code in sweet_tea/registry.py
entries()
sweet_tea.registry.Registry.entries()
classmethod
typed_entries()
sweet_tea.registry.Registry.typed_entries(lookup_type=Any)
classmethod
Get entries that are subclasses of the specified type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lookup_type
|
Any
|
The base class to filter by. Defaults to Any. |
Any
|
Returns:
| Type | Description |
|---|---|
list[Entry]
|
List of entries where the class_def is a subclass of lookup_type. |
Source code in sweet_tea/registry.py
fill_registry()
sweet_tea.registry.Registry.fill_registry(path=None, module=None, library='', label='')
classmethod
Recursively scan and register classes from packages starting from the given path.
This method automatically discovers all classes in the package hierarchy, supporting optional dependencies by gracefully skipping modules that fail to import due to missing packages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | None
|
Package path where modules are located. If None, uses the caller's module path. |
None
|
module
|
str | None
|
Name of the root module. If None, inferred from path. |
None
|
library
|
str
|
Name of the library for categorization. |
''
|
label
|
str
|
Optional label for categorizing classes. |
''
|