7.2. Karrigell Services
7.2.1 Definition
"Karrigell Services" are Python scripts which can handle several URLs, so that a complete service with different HTML pages can be created with just one script. They are the most recommended way of writing scripts with Karrigell, for ease of programming (access to user value is more straightforward) and maintenance (the logic of the whole application is defined in one script)
To achieve this, each function defined at the module level in a Karrigell service
matches a URL : the function foo()
in the script dummy.ks
is called by the URL dummy.ks/foo
This makes passing values from one page to another very straightforward : if a page has a link like
<A HREF="script.ks/foo?bar=99">
the script will handle the value by defining is as it argument :
def foo(bar): print bar
Form values are handled in a similar way :
<FORM ACTION="script.ks/foo"> <INPUT NAME="bar"> <INPUT TYPE="submit" VALUE="Ok">
The same script as above will manage the value entered by the user :
def foo(bar): print bar
The names defined as arguments to the function must be the same as those defined in the link or the form ; the function can also use default values like in ordinary Python scripts, in case no value was sent by the browser :
def foo(arg1,arg2=None): print "First argument",arg1 if arg2 is not None: print "Second argument",arg2
If no function is specified, Karrigell searches for a function called
index()
with no argument
Note that for security and readability reasons, only the functions explicitely defined in the ks script, and whose definition starts at the column 0 in the source code, can be called
7.2.2 Building applications
To "jump" from one function to another, just specify the function name in a link or a form action :
def index(): print '<a href="foo?name=bar">go to foo</a>' def foo(name): print '<IMG SRC="../picture.jpg">' print name
Notice the first line in the foo()
function : because of URL resolution
methods, the relative URL for files or scripts in the same directory as a ks script
must be prefixed by "../"
All the HTTP environment, custom exceptions, functions for authentication, session handling etc. are the same as in Python scripts
7.2.3 "Private" functions
If you need to define functions inside the script but don't want them to be called by a url, prefix them by an underscore (_)
def _private(value): """Private function - can't be called from the outside""" return value+1
7.2.4 Example
Here is an example of a simple Karrigell Service, using session management and HTTP redirection :
so = Session() if not hasattr(so, 'x'): so.x = 0 def index(): print "x = %s" %so.x print '<br><a href="increment">Increment</a>' print '<br><a href="decrement">Decrement</a>' print '<br><a href="reset">Reset</a>' def increment(): so.x = _private(so.x) raise HTTP_REDIRECTION,"index" def decrement(): so.x -= 1 raise HTTP_REDIRECTION,"index" def reset(): so.x = 0 raise HTTP_REDIRECTION,"index" def _private(x): """The function name begins with _ : internal function, can't be call by a url""" return x+1