Having a fixed set of functions in an API might be inapplicable in some cases, that’s why there is a concept of function managers in pyws. When a server tries to determine a function to call it asks each of its function managers whether it has one. Also, for instance, when WSDL description is generated, a server needs to know full list of its functions, function managers help with this task too.
pyws has only one predefined function manager, namely a fixed one.
A fixed function manager, it has a fixed set of functions.
functions is a list of functions to be registered.
Adds the function to the list of registered functions.
Returns a list of registered functions, the context is ignored.
Returns a function if it is registered, the context is ignored.
Each pyws server has a fixed function manager preinstalled, however there is no restriction on how many and what function managers a server might have.
If you have a pyws.functions.Function object, you can register it to a server using its method add_function:
Registers the function to the server’s default fixed function manager.
However, if you have an ordinary python function, there is an easier way, just use function pyws.functions.register.register:
Creates a registrator that, being called with a function as the only argument, wraps a function with pyws.functions.NativeFunctionAdapter and registers it to the server. Arguments are exactly the same as for pyws.functions.NativeFunctionAdapter except that you can pass an additional keyword argument to designating the name of the server which the function must be registered to:
>>> from pyws.server import Server
>>> server = Server()
>>> from pyws.functions.register import register
>>> @register()
... def say_hello(name):
... return 'Hello, %s' % name
>>> server.get_functions(context=None)[0].name
'say_hello'
>>> another_server = Server(dict(NAME='another_server'))
>>> @register(to='another_server', return_type=int, args=(int, 0))
... def gimme_more(x):
... return x * 2
>>> another_server.get_functions(context=None)[0].name
'gimme_more'