Providers & Services

A generic framework for service providers, recommended to be used whenever there are multiple ways of accomplishing a task or multiple sources can offer the required data.

xl.providers.MANAGER

Singleton instance of the ProviderManager

xl.providers.register(servicename, provider, target=None)

Registers a provider for a service. The provider object is used by consumers of the service.

Services can be targeted for a specific use. For example, if you have a widget that uses a service ‘foo’, if your object can perform a service only for a specific type of widget, then target would be set to the widget type.

If you had a service that could perform ‘foo’ for all widgets, then target would be set to None, and all widgets could use your service.

It is intended that most services should set target to None, with some narrow exceptions.

Parameters:
  • servicename (string) – the name of the service [string]
  • provider (object) – the object that is the provider [object]
  • target (object) – a specific target for the service [object]
xl.providers.unregister(servicename, provider, target=None)

Unregisters a provider.

Parameters:
  • servicename (string) – the name of the service
  • provider (object) – the provider to be removed
  • target (object) – a specific target for the service [object]
xl.providers.get(servicename, target=None)

Returns a list of providers for the specified servicename.

This will return providers targeted for a specific target AND providers not targeted towards any particular target.

Parameters:
  • servicename (string) – the service name to get providers for
  • target (object) – the target of the service
Returns:

list of providers

Return type:

list of objects

xl.providers.get_provider(servicename, providername, target=None)

Returns a single identified provider

This will return a provider either targeted for the specific target or a provider not targeted towards any particular target.

Parameters:
  • servicename (string) – The service name to get the provider for
  • providername (string) – The provider name to identify the provider
  • target (object) – the target of the service
Returns:

a provider or None

Return type:

object

class xl.providers.ProviderManager

The overall manager for services and providers for them

class xl.providers.ProviderHandler(servicename, target=None, simple_init=False)

Base class to handle providers for one specific service including notification about (un)registration

Target is the object that the service is being performed for. Often, if the service is truly global and it doesn’t make sense to target a service at a particular consumer, it can be None.

Parameters:
  • servicename (string) – the name of the service to handle
  • target (string) – the target for a provided service. Generally, this will be the object that uses the service
  • simple_init – call on_provider_added for every element already registered on instantiation.