Function manager

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.

class pyws.functions.managers.FunctionManager
get_one(context, name)

Returns a function by its name if it is accessible in the context. If it is not accessible or does not exist, raises pyws.errors.FunctionNotFound. Read more about context in chaper Context and about functions in chapter Function.

get_all(context)

Returns a list of functions accessible in the context. Read more about context in chaper Context and about functiona in chapter Function.

Fixed function manager

pyws has only one predefined function manager, namely a fixed one.

class pyws.functions.managers.FixedFunctionManager(*functions)

A fixed function manager, it has a fixed set of functions.

__init__(*functions)

functions is a list of functions to be registered.

add_function(function)

Adds the function to the list of registered functions.

get_one(context, name)

Returns a function if it is registered, the context is ignored.

get_all(context)

Returns a list of registered functions, 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.

Registering a function

If you have a pyws.functions.Function object, you can register it to a server using its method add_function:

Server.add_function(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:

pyws.functions.register.register(*args, **kwargs)

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'

Table Of Contents

Previous topic

Function

Next topic

Integration tests & examples

This Page