RSBScript in other languages
Normally, RSBScript is used to orchestrate data from operations to expose Simple Services. However it is possible to use other languages to do the same. Here we introduce one of the language connectors that allows you to use the Python language in RSBScript. You can write scripts in Python by specifying the language="python" parameter on the rsb:script keyword. This functionality requires the PythonOps connector, available via download from www.rssbus.com. It does not require a separate installation of Python, because a copy of the Python interpreter is included in PythonOps.
This short RSBScript lists *.rst files:
This Python script replicates the behavior of the script above:<rsb:set attr="mask" value="*.rst"/> <rsb:call op="fileListDir"> <rsb:push/> </rsb:call>
<rsb:script language="python"> input = {"mask":"*.rst"} rsb.call("fileListDir",input) </rsb:script>
The script above first establishes the language as Python by using the language parameter of the rsb:script keyword. This is followed by two lines of Python code: one to create a Python dictionary object with the input parameters, and another to call the operation.
The Python language connector uses the "rsb" object to share values with the RSSBus Engine. The following list shows the methods available in the "rsb" object:
- string rsb:get(name) The get method gets an input attribute. It is used to get the attributes specified in the URL line.
- rsb:set(name, value) The set method sets an attribute in the output item.
- rsb:push([title, description]) The push method pushes the current output item to the response. Normally one pushes several items from the script to create a feed.
- dict rsb:getinput() The getinput method returns a Python dictionary containing all the input attributes.
- list rsb:call(operation[,input[,push| PythonFunction]]) The call method calls an installed operation and returns a list of dictionaries. The list is the entire feed, each dictionary in the list is an item, and keys in the dictionary are attributes in the item.
The optional parameter input is a dictionary that can be used to specify input parameters. If it is not specified, or if it is None, then the input parameters to the Python script are transferred to the called operation.
The optional parameter push is a Boolean parameter used to specify whether the items produced by the called operation are also simultaneously pushed to the response. It is True by default.
You can also invoke a call with a function name as the third parameter. This acts as a callback and the function will be called for each item returned from the call. The Python function is defined as:
def myOnitem(item): #Note you have to explicitly #push the item from the callback rsb.push()
The item argument to the function is a Python dictionary that contains the attributes of the item. Examples of call:
rsb.call("fileListDir") rsb.call("fileListDir",myinput) rsb.call("fileListDir","myinput,False) rsb.call("fileListDir",myinput,True) rsb.call("fileListDir",null,myOnItem) rsb.call("fileListDir",myinput,myOnItem)
Use the methods described above to pipe the results from one operation to another. This example calls fileListDir and sends an email (using smtpSendMessage) for each item returned by it:
<!-- Send an email for each file in the directory. --> <rsb:info desc="Sends an email for each file found in the current directory."> <input name="from" desc="From address" default="demo@rssbus.com"/> <input name="mask" desc="File mask" default="*.*"/> </rsb:info> <rsb:script language="python"> input = {"mask":rsb.get("mask")} mail_from = rsb.get("from") mail_server = "MY_MAIL_SERVER" mail_to = "MY_EMAIL_ID" email_body = ''' The file %(file:name)s was accessed on %(file:atime)s. <p> Other Details<hr> Size : %(file:size)s<br> Creation Time : %(file:ctime)s<br> ''' fileListFeed = rsb.call("fileListDir", input, False) for item in fileListFeed: item["to"] = mail_to item["from"] = mail_from item["server"] = mail_server item["subject"] = item["file:name"] item["html"] = (email_body)%(item) rsb.call("smtpSendMessage",item,True) </rsb:script>