asphalt.wamp.registry

class asphalt.wamp.registry.Procedure(name, handler, options)

Create new instance of Procedure(name, handler, options)

handler

Alias for field number 1

name

Alias for field number 0

options

Alias for field number 2

class asphalt.wamp.registry.Subscriber(topic, handler, options)

Create new instance of Subscriber(topic, handler, options)

handler

Alias for field number 1

options

Alias for field number 2

topic

Alias for field number 0

class asphalt.wamp.registry.WAMPRegistry(prefix='', *, procedure_defaults=None, subscription_defaults=None)

Hosts a collection of WAMP procedure and subscriber registrations and exception mappings.

The purpose of this class is to ease the task of collecting all of the procedure handlers, event listeners and exception mappings of the application into a single place that the client can then use to register those on the router when a session has been opened. The alternative would be to call register_procedure(), subscribe() and map_exception() after connect(). This would not be very modular, however, since the code the connects the client would have to know about every single handler callbacks in advance.

Variables:
  • procedures (Dict[str, Procedure]) – registered procedure handlers
  • subscriptions (List[Subscription]) – registered event subscribers
  • exceptions (Dict[str, type]) – mapping of WAMP error code to exception class
Parameters:
add_from(registry, prefix='')

Add all the procedures, subscribers and exception mappings from another registry to this one.

If no prefix has been specified, the final name of each added procedure/subscriber endpoint or mapped error will be of the form <root prefix>.<name>. If a prefix has been specified, the name will be <root prefix>.<prefix>.<name>.

Parameters:
  • registry (WAMPRegistry) – a WAMP registry
  • prefix (str) – prefix to add to names of all procedure endpoints
Return type:

None

add_procedure(handler, name=None, options=None)

Add a procedure handler.

The callable will receive a CallContext instance as its first argument. The other positional and keyword arguments will be the arguments the caller passed to it.

Parameters:
  • handler (Callable) – callable that handles the procedure calls
  • name (Optional[str]) – name of the endpoint to register (relative to registry’s prefix); if None, use the callable’s internal name
  • options (Union[RegisterOptions, Dict[str, Any], None]) – either an Autobahn register options object or a dictionary of keyword arguments to make one
Return type:

Procedure

Returns:

the procedure registration object

Raises:
  • TypeError – if the handler does not accept at least a single positional argument
  • ValueError – if there is already a handler registered for this endpoint
add_subscriber(handler, topic, options=None)

Decorator that registers a callable to receive events on the given topic.

The callable will receive an EventContext instance as its first argument and must return an awaitable. The other positional and keyword arguments will be the arguments the publisher passed to the publish() method.

The topic may contain wildcards (a..b).

Parameters:
  • handler (Callable) – callable that receives matching events
  • topic (str) – the topic to listen to
  • options (Union[SubscribeOptions, Dict[str, Any], None]) – either an Autobahn subscribe options object or a dictionary of keyword arguments to make one
Return type:

Subscriber

Returns:

the subscription registration object

Raises:

TypeError – if the handler is not a coroutine or does not accept at least a single positional argument

exception(error)

Decorator version of map_exception().

Parameters:error (str) – the WAMP error code (e.g. foo.bar.baz)
Return type:Callable
map_exception(exc_type, code)

Map a Python exception class to a WAMP error code.

Parameters:
  • code (str) – the WAMP error code (e.g. foo.bar.baz)
  • exc_type (Type[BaseException]) – an exception class
Raises:

TypeError – if exc_type does not inherit from :class`BaseException`

Return type:

None

procedure(name=None, options=None)

Decorator version of add_procedure().

Can be used as @procedure(...) or simply @procedure.

If name has not been specified, the function name of the handler is used.

Parameters:
  • name (Union[str, Callable, None]) – name of the endpoint to register (relative to registry’s prefix)
  • options (Union[RegisterOptions, Dict[str, Any], None]) – either an Autobahn register options object or a dictionary of keyword arguments to make one
Return type:

Callable

subscriber(topic, options=None)

Decorator version of add_subscriber().

Parameters:
  • topic (str) – the topic to listen to
  • options (Union[SubscribeOptions, Dict[str, Any], None]) – either an Autobahn subscribe options object or a dictionary of keyword arguments to make one
Return type:Callable