asphalt.core.utils
- class asphalt.core.utils.PluginContainer(namespace, base_class=None)
A convenience class for loading and instantiating plugins through the use of entry points.
- Parameters:
namespace (str) – a setuptools entry points namespace
base_class (type | None) – the base class for plugins of this type (or
None
if the entry points don’t point to classes)
- all()
Load all entry points (if not already loaded) in this namespace and return the resulting objects as a list.
- create_object(type, **constructor_kwargs)
Instantiate a plugin.
The entry points in this namespace must point to subclasses of the
base_class
parameter passed to this container.- Parameters:
type (type | str) – an entry point identifier, a
module:varname
reference to a class, or an actual class objectconstructor_kwargs – keyword arguments passed to the constructor of the plugin class
- Return type:
Any
- Returns:
the plugin instance
- resolve(obj)
Resolve a reference to an entry point or a variable in a module.
If
obj
is amodule:varname
reference to an object,resolve_reference()
is used to resolve it. If it is a string of any other kind, the named entry point is loaded from this container’s namespace. Otherwise,obj
is returned as is.- Parameters:
obj (
Any
) – an entry point identifier, an object reference or an arbitrary object- Return type:
- Returns:
the loaded entry point, resolved object or the unchanged input value
- Raises:
LookupError – if
obj
was a string but the named entry point was not found
- asphalt.core.utils.callable_name(func)
Return the qualified name (e.g. package.module.func) for the given callable.
- Return type:
- asphalt.core.utils.merge_config(original, overrides)
Return a copy of the
original
configuration dictionary, with overrides fromoverrides
applied.This similar to what
dict.update()
does, but when a dictionary is about to be replaced with another dictionary, it instead merges the contents.If a key in
overrides
is a dotted path (ie.foo.bar.baz: value
), it is assumed to be a shorthand forfoo: {bar: {baz: value}}
.- Parameters:
original – a configuration dictionary (or
None
)overrides – a dictionary containing overriding values to the configuration (or
None
)
- Returns:
the merge result
- asphalt.core.utils.qualified_name(obj)
Return the qualified name (e.g. package.module.Type) for the given object.
If
obj
is not a class, the returned name will match its type instead.- Return type:
- asphalt.core.utils.resolve_reference(ref)
Return the object pointed to by
ref
.If
ref
is not a string or does not contain:
, it is returned as is.References must be in the form <modulename>:<varname> where <modulename> is the fully qualified module name and varname is the path to the variable inside that module.
For example, “concurrent.futures:Future” would give you the
Future
class.- Raises:
LookupError – if the reference could not be resolved
- Return type: