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
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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
__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
__is_excluded(dotted_name, patterns)
classmethod
Test a dotted module path against the caller's exclude patterns.
Uses :func:fnmatch.fnmatchcase rather than :func:fnmatch.fnmatch; the latter
applies os.path.normcase, making matches case-insensitive on Windows. Module
names are case-sensitive, so matching must not vary by platform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dotted_name
|
str
|
Full dotted path of the module or package under consideration. |
required |
patterns
|
tuple[str, ...]
|
Glob patterns supplied by the caller. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when any pattern matches. |
Source code in sweet_tea/registry.py
__is_namespace_package(name, path)
classmethod
Decide whether a directory lacking __init__ should be treated as a package.
__init__.py used to act as a de-facto opt-in marker, so treating every bare
directory as a package risks descending into directories that are not packages
at all. These filters restore that boundary without making users opt in to
namespace support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Directory name. |
required |
path
|
str
|
Full path to the directory. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the directory is a plausible namespace package. |
Source code in sweet_tea/registry.py
__iter_package_children(pkg_dir)
classmethod
List the importable children of a package directory.
Extends :func:pkgutil.iter_modules, which reports a subdirectory only when it
contains an __init__. Implicit namespace packages (PEP 420) have no
__init__ and are therefore invisible to it — not reported as a package and
not reported as a module — so their contents were silently skipped during
registry filling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pkg_dir
|
str
|
Directory to scan. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, bool]]
|
Sorted (name, is_a_package) pairs, with namespace packages included. |
Source code in sweet_tea/registry.py
entries()
classmethod
fill_registry(path=None, module=None, library='', label='', exclude=None)
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.
Both regular packages and implicit namespace packages (PEP 420) are traversed;
no flag is needed to opt in to the latter. Directories that cannot be imported
are ignored — see :meth:__is_namespace_package for the exact rules.
Directories that are importable but should not be registered — tests,
fixtures, examples — cannot be detected automatically, since they are
legitimate Python. Name them via exclude:
.. code-block:: python
Registry.fill_registry(exclude=["*.tests", "*.examples"])
Excluding a package prunes its whole subtree, so a single pattern is enough to drop everything beneath it.
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. |
''
|
exclude
|
Iterable[str] | None
|
Glob patterns matched case-sensitively against the full dotted
module path ( |
None
|
Source code in sweet_tea/registry.py
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 | |
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='', exclude=None)
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.
Both regular packages and implicit namespace packages (PEP 420) are traversed;
no flag is needed to opt in to the latter. Directories that cannot be imported
are ignored — see :meth:__is_namespace_package for the exact rules.
Directories that are importable but should not be registered — tests,
fixtures, examples — cannot be detected automatically, since they are
legitimate Python. Name them via exclude:
.. code-block:: python
Registry.fill_registry(exclude=["*.tests", "*.examples"])
Excluding a package prunes its whole subtree, so a single pattern is enough to drop everything beneath it.
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. |
''
|
exclude
|
Iterable[str] | None
|
Glob patterns matched case-sensitively against the full dotted
module path ( |
None
|
Source code in sweet_tea/registry.py
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 | |