#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-

import sys
from fcntl import flock
from mod_python import psp


LOGPATH = "/home/httpd/html/py/ip/ip2/3.1/count.log"

def index(req):
    """Browser request. Reads a number to display. 
    Runs template with variables.
    """
    
    get = read()
    req.content_type='text/html; charset = utf-8'
    tmpl=psp.PSP(req, filename='count.html')
    tmpl.run(vars={'greet': get})

def read():
    """Read a number from a file, returns str after calculation."""
    
    cfile = open(LOGPATH, 'r')
    flock(cfile, 2)
    number = cfile.read()
    flock(cfile, 8)
    cfile.close()
    i = int(number)
    i += 1
    renumber = write(repr(i))
    return renumber
		
def write(var):
    """Takes a string and writes it to file"""
    
    cfile = open(LOGPATH, 'w')
    flock(cfile, 2)
    cfile.write(var)
    flock(cfile, 8)
    cfile.close()
    return var
