msl.network.service module

Base class for all Services.

class msl.network.service.Service(*, name=None, max_clients=None, ignore_attributes=None)[source]

Bases: Device

Base class for all Services.

New in version 0.4: The name and max_clients keyword argument.

New in version 0.5: The ignore_attributes keyword argument.

New in version 1.0: If a method of the Service returns an object that is not natively JSON serializable, then the returned object can have a callable to_json() method and the value returned by to_json() will be used in the response to the Client.

Parameters:
  • name (str, optional) – The name of the Service as it will appear on the Network Manager. If not specified then the class name is used. You can also specify the name in the start() method.

  • max_clients (int, optional) – The maximum number of Clients that can be linked with this Service. A value \(\leq\) 0 or None means that there is no limit.

  • ignore_attributes (str or list of str, optional) – The names of the attributes to not include in the identity of the Service. See ignore_attributes() for more details.

property max_clients

The maximum number of Clients that can be linked with this Service. A value \(\leq\) 0 means an unlimited number of Clients can be linked.

Type:

int

emit_notification(*args, **kwargs)[source]

Emit a notification to all Clients that are Linked with this Service.

New in version 0.5.

Parameters:
  • args – The arguments to emit.

  • kwargs – The keyword arguments to emit.

emit_notification_threadsafe(*args, **kwargs)[source]

A thread-safe implementation of emit_notification().

When a Service handles a request, it does so in a separate thread than the event loop is running in. Therefore, if a method of the Service class wants to emit a notification while it is handling a request then it must emit the notification in a thread-safe manner.

New in version 1.0.

Parameters:
  • args – The arguments to emit.

  • kwargs – The keyword arguments to emit.

ignore_attributes(*names)[source]

Ignore attributes from being added to the identity of the Service.

There are a few reasons why you may want to call this method:

  • If you see warnings that an object is not JSON serializable or that the signature of an attribute cannot be found when starting the Service and you prefer not to see the warnings.

  • If you do not want an attribute to be made publicly known that it exists. However, a Client can still access the ignored attributes.

Private attributes (i.e., attributes that start with an underscore) are automatically ignored and cannot be accessed from a Client on the network.

If you want to ignore any attributes then you must call ignore_attributes() before calling start().

New in version 0.5.

Parameters:

names – The names of the attributes to not include in the identity of the Service.

start(*, name=None, host='localhost', port=1875, timeout=10, username=None, password=None, password_manager=None, read_limit=None, disable_tls=False, cert_file=None, assert_hostname=True, auto_save=False)[source]

Start the Service.

See connect() for the description of each parameter.

property request

Returns the latest request.

This property is meant to be used by a subclass that may want to know the information about the request while processing it.

Since a request is executed in a separate thread and this property returns the latest request, the subclass should immediately extract the necessary information from the request before the Service receives a new request.

The key-value pairs in the request are:

{
  'args': list,
  'attribute': str,
  'kwargs': dict,
  'service': str (the name of this Service),
  'uid': str,
  'requester': str,
}

New in version 1.1.

Type:

dict

msl.network.service.filter_service_start_kwargs(**kwargs)[source]

From the specified keyword arguments only return those that are valid for start().

New in version 0.4.

Parameters:

kwargs – All keyword arguments that are not part of the method signature for start() are silently ignored and are not included in the output.

Returns:

dict – Valid keyword arguments that can be passed to start().