pydio.base - Interface definitions

Interface definitions.

class pydio.base.IFactory

Interface for bound factories.

Bound factories are managed by IInjector objects and are responsible for construction of target object that is later returned by IInjector.inject(). Each factory should wrap one kind of object factory function provided by user (f.e. normal function or a coroutine, but never both).

abstract close()

Close this factory.

When called, underlying instance is cleared and calling get_instance() again will return None. This method may also invoke some additional custom-defined clearing actions (if supported by implementation).

abstract get_instance() Optional[Union[T, Awaitable[T]]]

Create and return target object.

Value returned by this method is later also returned by IInjector.inject() method.

class pydio.base.IInjector

Definition of injector interface.

abstract close() Optional[Awaitable[None]]

Close this injector.

Closing injector invalidates injector and makes it unusable.

It also cleans up internal cache by calling IFactory.close() for each factory being in use by this injector. If this injector has children injectors (created by calling scoped() method) then those are closed as well (recursively).

abstract inject(key: Hashable) Union[T, Awaitable[T]]

Create and return object for given hashable key.

On success, this method returns created object or awaitable pointing to created object. On failure, it raises pydio.exc.InjectorError.

Parameters

key

Identifier of underlying object factory to be used.

This can be either class object (f.e. base class or interface), a hashable (f.e. string or a number), or a special key wrapper from pydio.keys.

Please be aware that same key has to be used in provider during registration of object factory.

abstract scoped(scope: Hashable, env: Optional[Hashable] = None) IInjector

Create scoped injector that is a child of current one.

Scoped injectors can only operate on pydio.base.IUnboundFactory objects with pydio.base.IUnboundFactory.scope attribute being equal to given scope.

Parameters
  • scope – User-defined scope name.

  • env

    User-defined environment name for newly created injector and all its descendants.

    This option is applicable only if none of the ancestors of newly created injector has environment set. Otherwise, setting this will cause ValueError exception.

class pydio.base.IUnboundFactory

Interface for unbound factories.

Unbound factories are created and managed by IUnboundFactoryRegistry objects. The role of this class is to wrap user-specified factory functions that are being registered to providers.

abstract bind(injector: IInjector) IFactory

Create IFactory object to be owned by given injector.

Parameters

injector – The owner of bound factory object to be created

abstract is_awaitable() bool

Return True if this factory produces awaitable IFactory instances or False otherwise.

abstract property scope: Optional[Hashable]

Name of the scope assigned to this factory.

Factories with scopes defined can only be used by injectors with same scope set.

class pydio.base.IUnboundFactoryRegistry

Interface for IUnboundFactory objects registry.

Factory registries are used by IInjector objects to find IUnboundFactory object that matches key that was given to IInjector.inject() call.

abstract get(key: Hashable, env: Optional[Hashable] = None) Optional[IUnboundFactory]

Get IUnboundFactory registered for given key and environment (if given).

If no factory was found, then return None.

Parameters
  • key – See IInjector.inject()

  • env

    Environment name.

    Same key can be reused by multiple environments, but none can have that key duplicated. This is used to provide several different implementations of one key that depend on environment on which application is executed (f.e. different database may be needed in testing, and different in production)

abstract has_awaitables() bool

Return True if this factory registry contains awaitable factories or False otherwise.

Behind the scenes, this will check if there is at least one unbound factory for which IUnboundFactory.is_awaitable() returns True.