djblets.webapi.resources.registry¶
Resource registration and lookup.
-
class
ResourcesRegistry
[source]¶ Bases:
object
Manages a registry of instances of API resources.
This handles dynamically loading API resource instances upon request, and registering those resources with models.
When accessing a resource through this class for the first time, it will be imported from the proper file and cached. Subsequent requests will be returned from the cache.
While an optional class, consumers are encouraged to create a subclass of this that they use for all resource instance references and for registering model to resource mappings.
-
resource_search_path
= None[source]¶ A list of Python module paths to search for module instances.
When looking up a module, the class will attempt to load a “<resource_name>_resource” module from that path, with a “<resource_name>” instance from the module.
-
__getattr__
(name)[source]¶ Return a resource instance as an attribute.
If the resource hasn’t yet been loaded into cache, it will be imported, fetched from the module, and cached. Subsequent attribute fetches for this resource will be returned from the cache.
Parameters: name (unicode) – The name of the resource to look up. Returns: The resource instance matching the name. Return type: WebAPIResource
-
-
register_resource_for_model
(model, resource)[source]¶ Register a resource as the official location for a model.
Parameters: - model (Model) – The model associated with the resource.
- resource (WebAPIResource or callable) – Either a WebAPIResource, or a function that takes an instance of
model
and returns a WebAPIResource.
-
unregister_resource_for_model
(model)[source]¶ Remove the official location for a model.
Parameters: model (Model) – The model associated with the resource to remove.
-
get_resource_for_object
(obj)[source]¶ Return the resource for an object.
Parameters: obj – The object whose model has a resource associated. Returns: The resource associated with the object, or None
if not found.Return type: WebAPIResource
-
get_resource_from_name
(name)[source]¶ Return the resource of the specified name.
Parameters: name (unicode) – The name of the resource. Returns: The resource instance, or None
if not found.Return type: WebAPIResource
-
get_resource_from_class
(klass)[source]¶ Return the resource with the specified resource class.
Parameters: klass (class) – The WebAPIResource subclass. Returns: The resource instance, or None
if not found.Return type: WebAPIResource
-
unregister_resource
(resource)[source]¶ Unregister a resource from the caches.
Parameters: resource (WebAPIResource) – The resource instance to unregister.