mini_arcade.utils.implementation_registry

Typed, safe, and subclass-isolated implementation registry.

Attributes

Classes

ImplementationRegistry

A registry of classes implementing a specific interface.

Module Contents

mini_arcade.utils.implementation_registry.ImplementationType
class mini_arcade.utils.implementation_registry.ImplementationRegistry[source]

Bases: Generic[ImplementationType]

A registry of classes implementing a specific interface.

Subclasses must set implementation_base to the ABC (or base class) that all implementations derive from.

Variables:
  • implementation_base – ClassVar[type]: The base class for registered implementations.

  • _registry – ClassVar[MutableMapping[str, Type[ImplementationType]]]: Mapping of names to implementation classes.

  • _lock – ClassVar[threading.RLock]: Lock for thread-safe operations.

implementation_base: ClassVar[type]
classmethod __init_subclass__(**kwargs)[source]
classmethod register(name: str, impl_class: Type[ImplementationType], *, replace: bool = False)[source]

Register an implementation implementation class.

Parameters:
  • name (str) – Name to register the implementation under.

  • impl_class (Type[ImplementationType]) – Implementation class to register.

  • replace (bool) – Whether to replace an existing registration.

Raises:
  • TypeError – If impl_class does not subclass implementation_base.

  • KeyError – If name is already registered and replace is False

classmethod unregister(name: str)[source]

Unregister an implementation implementation by name.

Parameters:

name (str) – Name of the implementation to unregister.

classmethod implementation(name: str | None = None, *, replace: bool = False) Callable[[Type[ImplementationType]], Type[ImplementationType]][source]

Decorator to register an implementation implementation class.

Parameters:
  • name (Optional[str]) – Name to register the implementation under.

  • replace (bool) – Whether to replace an existing registration.

classmethod get(name: str) Type[ImplementationType][source]

Get the implementation implementation class by name.

Parameters:

name (str) – Name of the implementation to retrieve.

Returns:

Implementation class.

Return type:

Type[ImplementationType]

Raises:

KeyError – If the name is not registered.

classmethod try_get(name: str) Type[ImplementationType] | None[source]

Try to get the implementation implementation class by name.

Parameters:

name (str) – Name of the implementation to retrieve.

Returns:

Implementation class or None if not found.

Return type:

Optional[Type[ImplementationType]]

classmethod contains(name: str) bool[source]

Check if an implementation implementation is registered by name.

Parameters:

name (str) – Name of the implementation to check.

Returns:

True if registered, False otherwise.

Return type:

bool

classmethod all() Mapping[str, Type[ImplementationType]][source]

Get a mapping of all registered implementation implementations.

Returns:

Mapping of names to implementation classes.

Return type:

Mapping[str, Type[ImplementationType]]

classmethod names() list[str][source]

Get a list of all registered implementation implementation names.

Returns:

List of implementation names.

Return type:

list[str]

classmethod find_contains(needle: str) list[Type[ImplementationType]][source]

Find all implementation implementations whose names contain the given substring (case-insensitive).

Parameters:

needle (str) – Substring to search for.

Returns:

List of matching implementation classes.

Return type:

list[Type[ImplementationType]]

classmethod find_regex(pattern: str) list[Type[ImplementationType]][source]

Find all implementation implementations whose names match the given regex pattern (case-insensitive).

Parameters:

pattern (str) – Regex pattern to search for.

Returns:

List of matching implementation classes.

Return type:

list[Type[ImplementationType]]

classmethod instantiate(name: str, *args, **kwargs) ImplementationType[source]

Instantiate an implementation implementation by name.

Parameters:

name (str) – Name of the implementation to instantiate.

Returns:

Instance of the implementation.

Return type:

ImplementationType

classmethod clear()[source]

Clear all registered implementation implementations.

classmethod __iter__() Iterator[tuple[str, Type[ImplementationType]]][source]
classmethod __len__() int[source]