Karrigell Documentation

Version 2.2.5 13 05 06

Français

9. Karrigell Services

9.1 Syntax

"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

To achieve this, the functions in a Karrigell service match a URL : the function foo() in the script dummy.ks is called by the URL dummy.ks/foo

To call the method foo(arg1,arg2) the URL must be like dummy.ks/foo?arg1=val1&arg2=val2 or called by a form with fields arg1 and 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

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

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, form fields, custom exceptions, functions for authentication, session handling etc. are the same as in Python scripts

Here is an example of a simple Karrigell Service :

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

9.2 Smart urls

For some uses it may be useful to access Karrigell Services with urls that specify additional parameters. For instance, the url htpp://path/service.ks/function/foo/bar?name=smith would call the function with 'foo' and 'bar' as parameters

They can be found in the script in an attribute of THIS called subpath. In the example above, it would be the list ['foo','bar']

def function(name):
   print 'subpath ',THIS.subpath
   print name

A problem with these "smart urls" is that if you have to write a link, or use Include() or redirect to a relative url, you must give the right url to the browser, that is, prepend the right number of '../' before the target url

This number is defined by the length of the subpath attribute : if you want to write a link in the function above you'd have to write

def function(name):
   print 'subpath ',THIS.subpath
   print name
   print '<a href=%slogin>Login</a>' %('../'*len(THIS.subpath))
or with a shortcut attribute of THIS called up :

def function(name):
   print 'subpath ',THIS.subpath
   print name
   print '<a href=%slogin>Login</a>' %THIS.up