The python accessor executes Python language scripts.
It is based on Jython
.
The operator specifies the resource to be executed as a program.
This may contain zero or more classes plus
some script code outside the context of any class which will be executed when the module is invoked.
The script may either be a plain/text or an XML document with the script specified as the
text of the root element. This later form is useful when embedding python snippets inside declarative
languages.
When using XML it is strongly advisable to enclose your script inside a CDATA section, for example:
<script><![CDATA[ ...your script .. ]]></script>
Script Context
When executed, each script is provided an instance
of the INKFConvenienceHelper
class as the local
variable context.
The context variable has two purposes.
It supports introspection of the request which initiated the script
and it allows sub-requests to be issued.
For more details see the NKF Reference Guide.
Once a response object is created:
response=context.createResponseFrom(...);
it will be returned via the context when the script exits.
Parameters
Parameters can be passed to a script using named arguments.
Argument values are available within the script through the
context
using the this:param URI address space convention.
For example if the python accessor is requested with...
active:python+operator@myscript.py+myargument@data.xml
The myargument parameter can be obtained inside the script
using the context and the argument URI "this:param:myargument".
context.source("this:param:myargument")
Python Libraries
There are many useful Python libraries.
It can also be useful to structure your own code using multiple modules.
The python accessor allows you to import Python modules into your Python scripts.
Traditionally Python has been oriented around a filesystem and all imports were
resolved against the fileystem either absolutely or relatively.
Because NetKernel has a modular address space abstraction this accessor
resolves imports not from the filesystem but from NetKernel modules.
The principles are the same, modules are resolved either absolutely or
against the current working URI of the "main" module.
Module import specificiations are converted to URIs which are resolved against the
address space of the request for execution of the module.
For example, the import statement:
from test.library.area import AreaClass
executed inside the module addressed by
ffcpl:/scripts/myModule.py
would be resolved against the
following URIs with the first successful one used:
ffcpl:/scripts/test/library/area.py
(relative to main)
ffcpl:/test/library/area.py
(absolute)
Java Libraries
The Jython implementation has the ability to access native Java classes. Java classes
are often needed when constructing requests and issuing results in NKF.
Java classes are imported with the following python syntax:
import sys
sys.add_package("org.ten60.netkernel.layer1.representation")
sys.add_package("com.ten60.netkernel.urii.aspect")
Exception Handling
Python contains good exception handling however during development unexpected and unhandled
exceptions will occur. The Python accessor maps python exceptions into the NetKernel domain
so they integrate well into the composite stack trace that NetKernel presents. Specifically
you can actually see the Python stack trace with python-module names, functions and line numbers.
Example
Here's an example of a Python script for performing an XSLT, logging the result and returning the result as the response.
# Create and issue XSLT request
sr=context.createSubRequest("active:xslt)
sr.addArgument("operand","data1.xml")
sr.addArgument("operator","style1.xsl")
rep=context.issueSubRequest(sr)
# Log result
sr=context.createSubRequest()
sr.setURI("active:log")
sr.addArgument("operand",rep)
context.issueSubRequest(sr)
# Create response, set metadata and exit
resp=context.createResponseFrom(rep)
resp.setMimeType("text/plain")
resp.setExpired()