

from twisted.application import service
from twisted.application import internet

from nevow import iwoven
from nevow import renderer
from nevow import appserver
from nevow.stan import directive

from nevow.tags import *


def theTitle(context, data):
    return [x * 5 for x in "title"]


def sample(context, data):
    request = context.locate(iwoven.IRequest)
    session = request.getSession()
    count = getattr(session, 'count', 1)
    session.count = count + 1
    return ("Welcome, person from ", request.client[1], "! You are using ", 
        request.getHeader('user-agent'), " and have been here ", count, " times.")


class Simple(renderer.Renderer):
    document = html[
    head[
        title[
            theTitle
        ]
    ],
    body[
        h1["Hello."],
        sample,
    ]
]


application = service.Application("simple")
internet.TCPServer(
    8080, 
    appserver.NevowSite(
        Simple()
    )
).setServiceParent(application)
