controller.py :  » Web-Server » SkunkWEB » skunkweb-3.4.4 » SkunkWeb » Services » mvc » services » 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 Server » SkunkWEB 
SkunkWEB » skunkweb 3.4.4 » SkunkWeb » Services » mvc » services » controller.py
import _shared
import SkunkWeb.Configuration as Cfg
from web.protocol import PreemptiveResponse,HandleConnection
from templating.Handler import _handleException
from SkunkWeb.constants import WEB_JOB
from SkunkWeb.LogObj import logException
from mvc.log import debug,info
from mvc.utils import is_exposed,_import_a_class
from mvc.base import Response
import types

Cfg.mergeDefaults(controllers={},
                  defaultErrorMimetype='text/html')

def _resolve_string(controllerName):
    try:
        c=_import_a_class(controllerName)
    except ImportError:
        logException()
        return None
    else:
        # do we need to instantiate one?
        # who says this needs to be a instance anyway?
        return c

def controllerHandler(connection, sessionDict):
    if not Cfg.MvcOn:
        return
    cname=sessionDict.get('CONTROLLER')
    data=sessionDict.get('CONTROLLER_ARGS')
    aname=sessionDict.get('ACTION')
    debug("do we have a controller? %s",  ('No', 'Yes')[bool(cname)])
    if cname:
        c=Cfg.controllers.get(cname)
        if c:
            if isinstance(c, basestring):
                # resolve it
                c=_resolve_string(c)
                if not c:
                    return
            if not aname:
                aname='index'
            meth=getattr(c, aname, None)
            if meth and not is_exposed(meth):
                info("request for method that isn't exposed, not honoring")
                meth=None
            if not meth:
                # look for a default method and pass
                # the action name in as a keyword argument
                data['action']=aname
                if callable(c):
                    meth=c
                else:
                    # "default" doesn't need to be exposed
                    meth=getattr(c, "default", None)
        
            if meth:
                # some defaults
                connection.setStatus(200)
                connection.setContentType('text/html')
                try:
                    res=meth(**data)
                except PreemptiveResponse:
                    # not a problem, raise it
                    raise
                except Response, res:
                    res(connection)
                    return connection.response()
                except:
                    # OK, something bad.  This should be a server error.
                    return _handleException(connection)
                else:
                    if res:
                        if isinstance(res, basestring):
                            connection.write(res)
                        elif callable(res):
                            res(connection)
                        else:
                            # an iterable, hopefully
                            for thing in res:
                                connection.write(thing)
                        return connection.response()
                
                    
glob='%s*' % WEB_JOB
# go *before* the templating handlers
HandleConnection.addFunction(controllerHandler, glob, 0)


                    
                
                
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.