4.34. XML-RPC functions

XML-RPC functions are the Python code that is executed at the server side, they are located in the xmlrpc_functions directory in one or more files and/or directories, it's up to you to arrange your directories and files names, the only obligation is to suffix your files with .py. The xmlrpc_functions directory forms a Python module so you can do cross imports in your code.

When you refer to an XML-RPC function (via the Rpc handler) you can use only the function name or the full path to the function. Ex: you have a function named create_order in a file named orders.py inside a directory named sales, You can call your function with Rpc.create_order or Rpc.sales.orders.create_order.

The following functions are available in the global namespace :

Cmd(command, bg=False, split=True)
Run command in a shell. if bg is set to True, the command is executed in the background and this function returns immediately. If split is set to False, the execution output is return as is, the default is to return it as a list of lines. The return value of this function is a tuple of two elements : the return code from the shell and the execution output.
RootCmd(command, bg=False, split=True)
This function is similar to Cmd unless that RootCmd uses the sudo program to execute command under the root account. You must configure sudo properly for this to work.
Error(error_msg='')
This function can be called to stop the execution and displays error_msg in a message box.
Log(log_msg)
Add log_msg to the available log mechanism, stdout for the standalone PyDADL server and apache log files when using PyDADL with mod_python.
ShellEscape(string)
Return a suitable version of string for shell command usage.
SqlEscape(string)
Return a suitable version of string for SQL command usage.
XmlEscape(string)
Return a suitable version of string for XML usage.
Obj2Str(object)
This function returns an ascii representation of object that can be used where binary data must be avoided.
Str2Obj(string)
Converts string back to the original object. This function is used in conjunction with Obj2Str.

The following attributes are available in the global namespace :

Rpc
A loopback handler for executing XML-RPC functions. ex: Rpc.function_name(arg1, arg2, ..., argN). The return value is the result of the execution..
Data
Data is the SQL handler used to access the application's data. This handler is initialized from parameters specified in the data_server tag. The Data handler defines the following functions :
execute(query, values=())
This function executes SQL query and returns the result in a list of dicts, dict's keys maps to column's names. values is a tuple of placeholders used to insert column values. ex: Data.execute("SELECT id, label FROM products WHERE price < %s", (max_price,)).
getLastId()
Returns the last value of column that uses AUTO_INCREMENT after an INSERT statement.
begin()
begins a new transaction.
commit()
Commits the current transaction.
rollback()
Rolls back the current transaction.
getDAO(table)
Creates a DAO object on table.
Appli
Appli is the SQL handler that is connected to the PyDADL's internal database. Normally, you don't have to use this handler in you code.
Debug
A boolean value that indicates if the debug mode is active.
Peer
Peer is an object that holds informations about the connected client, it defines the following attributes :
app_id
The application's id used by peer.
app_name
The application's name used by peer.
user
The peer's username.
flags
The peer's flags (permissions and attributes).
lang
The peer's selected language.
ip
The peer's IP address.
port
The peer's TCP port.

There is an XML-RPC multicall function that can be used to executes multiples functions in one call. The syntax is Rpc.multicall([{'methodName': 'add', 'params': [2, 2]}, {'methodName': 'divide', 'params': [6, 2]}, ...]). The return is a list of all return values, Ex: [4, 3, ...].