Athana

1.Manual

1.1  Installing Python 

For Athana to work, you need a working Python interpreter (at least python version 2.3). You can download Python from http://www.python.org/download/.

1.2  Installing Athana 

Athana can be downloaded from http://www.athana.org/#download. It only consists of the script athana.py, which you can copy to any convenient location on your harddisk.

1.3  Running Athana 

To start the athana web server, run the following command:

Code listing 1.1

	python athana.py -i /var/www/athana/demo/web.cfg
    

with "web.cfg" being a config file which specifies the setup of your web server (description follows).

Run python athana.py -h for a list of other command line options.

1.4  The web.cfg config file 

The following is a simple example for a web server with only one contexts, /demo/.

Code listing 1.2

base: /var/www/athana/
context: demo/
root: demo/
    file: index.py
	handler: show_startpage
	    pattern: /
	    pattern: /index.*
	handler: process_submit
	    pattern: /submit

The context is the portion of the url under which the application will be visible. In this example, if the web server would be running on the IP 127.0.0.1, you would be able to access the application by going to http://127.0.0.1:8081/demo/. (8081 is the Athana default port. You can change it by passing the -p option to the script)

The root is the local directory where the script files for this context are (relative to the base directory).

The file directive lists individual files inside the directory given with root. It's possible to use subdirectories, i.e. a file directive of the form
file: frontent/index.py
would also be allowed.

The handlerdirective references functions inside the script file given with file- each handler can have one or more patterns associated, so that different kinds of requests can be mapped to a given handler. In the example above, the handler show_startpage would serve all requests to /demo/ and /demo/index.html, /demo/index.jsp, etc.

1.5  script files 

The following is an example for a simple script, containing two handlers:

Code listing 1.3


import athana

def show_startpage(req):
    req.write(
    """
    <html>
    <body>
	<h1>click:</h1>
	<form action="""+req.makeLink("submit")+""" method="post">
	    <input type="text" name="text"/><br/>
	    <input type="submit" name="submit" value="submit"/>
	</form>
    </body>
    </html>
    """)
    return athana.HTTP_OK

def process_submit(req):
    for key,value in req.params.items():
        print key,'=',value
    return athana.HTTP_OK


As you can see, a "handler" is nothing more than a python function which takes a request object, which contains the request parameters and can be used to write a http response.

The return value of the function must be an HTTP code- in this example, HTTP_OK is used to notify the browser that the request was processed correctly.

1.6  Session handling 

Every request object also contains a session object, which is only valid for a specific user and a given time.

The following fragment of code lists all contents of the session, and stores some data in it:

Code listing 1.4


    for key in req.session:
	print key, "=", req.session[key]
    
    req.session["favourite_color"] = "blue"


1.7  Serving files 

Static files (like images, css stylesheets, .js scripts etc.) can be served by athana by adding a section like the following to the config file:

Code listing 1.5


context: /img/
root: /local/filesystem/path/to/images/


Notice that the root path will still be treated relatively to the global base path.

You can also put static files into a zip file, and let athana deliver the contents of that zip file:

Code listing 1.6


context: /img/
root: images.zip


1.8  Page Templates 

Athana has support for TAL Page Templates.

Page templates are especially good for seperating logic and display when following the MVC (Model View Controller) model for web development.

To use a page template, place a .html file with TAL markup into the context directory, and then process it in the handler like this:

Code listing 1.7



class Pizza:
    def __init__(self, size, cheese):
	self.size = size
	self.cheese = cheese
	self.toppings = []
    def addTopping(self, topping):
	self.toppings += [topping]
    def getSize(self):
	return str(self.size) + " cm"
    def getCheese(self):
    	return self.cheese
    def getToppings(self):
	return self.toppings

def show_pizza(req):
    pizza = Pizza(size=13, cheese="Edamer")
    pizza.addTopping("tomatoes")
    pizza.addTopping("anchovies")
    pizza.addTopping("mushrooms")
    pizza.addTopping("salami")
    req.writeTAL("pizza.html", {"pizza": pizza})
    return athana.HTTP_OK


The corresponding page template would look like this:

Code listing 1.8


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>

<head><title>Pizza Details</title></head>

<body xmlns:z="http://xml.zope.org/namespaces/tal">
  <h1>Pizza:</h1>
  <b>Size:</b> <span z:content="python:pizza.getSize()"></span> <br>
  <b>Cheese flavor:</b> <span z:content="python:pizza.getCheese()"></span> <br>
  <b>Topping(s):</b> <br>
  <div z:repeat="item python:pizza.getToppings()">
      <li z:replace="item">1</li>
  </div>
</body>

</html>



  Athana Manual