_widget_plugin.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » nevow » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Nevow 
Nevow » Nevow 0.10.0 » nevow » _widget_plugin.py
# -*- test-case-name: nevow.test.test_athena -*-

"""
twistd subcommand plugin for launching an athena widget server.
"""

from twisted.python.usage import Options,UsageError
from twisted.python.reflect import namedAny
from twisted.application.internet import TCPServer

from nevow.loaders import stan
from nevow.athena import LivePage
from nevow.appserver import NevowSite
from nevow.tags import html,head,title,body,div,directive


class ElementRenderingLivePage(LivePage):
    """
    Trivial LivePage which renders an instance of a particular element class.
    """
    docFactory = stan(html[
            head(render=directive('liveglue'))[
                title(render=directive('title'))],
            body[
                div(render=directive('element'))]])

    def __init__(self, element):
        LivePage.__init__(self)
        self.element = element


    def render_title(self, ctx, data):
        return ctx.tag[self.element.__class__.__name__]


    def render_element(self, ctx, data):
        self.element.setFragmentParent(self)
        return ctx.tag[self.element]



class WidgetPluginRoot(LivePage):
    """
    Provide a top-level resource for creating new widget elements with each
    reqest.
    """
    def __init__(self, elementFactory):
        """
        Initialize the resource with the passwed plugin element.
        """
        LivePage.__init__(self)
        self.elementFactory = elementFactory


    def child_(self, ctx):
        """
        Instead of returning the default self, return a new instance of this
        class, thus allowing page reloads (that produce a new instance).
        """
        return ElementRenderingLivePage(self.elementFactory())



class Options(Options):
    """
    Command-line parameters for the athena widget twistd plugin.
    """
    def opt_port(self, portstr):
        """
        Specify the port number to listen on.
        """
        try:
            self['port'] = int(portstr)
        except ValueError:
            raise UsageError(
                "Specify an integer between 0 and 65535 as a port number.")
        if self['port'] >= 2 ** 16:
            raise UsageError(
                "Specify an integer between 0 and 65535 as a port number.")
        elif self['port'] < 0:
            raise UsageError(
                "Specify an integer between 0 and 65535 as a port number.")


    def opt_element(self, qualifiedName):
        """
        Specify the LiveElement or LiveFragment class to serve.
        """
        try:
            factory = namedAny(qualifiedName)
        except (ValueError, AttributeError):
            raise UsageError("Specify a valid class name to --element")
        self['element'] = factory


    def postOptions(self):
        """
        Assign default values for those arguments not specified.
        """
        if 'port' not in self:
            self['port'] = 8080
        if 'element' not in self:
            raise UsageError("Specify a class name to --element")



def makeService(options):
    """
    @type options: C{dict}

    @param options: Configuration for the NevowSite to start.  Required
    keys are::

        C{'element'}: a LiveElement or LiveFragment instance.

        C{'port'}: an C{int} giving the port number to which to bind.

    """
    element = options['element']
    page = WidgetPluginRoot(element)
    return TCPServer(options['port'], NevowSite(page))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.