Package wtf :: Package app
[hide private]
[frames] | no frames]

Source Code for Package wtf.app

  1  # -*- coding: ascii -*- 
  2  # 
  3  # Copyright 2007-2012 
  4  # Andr\xe9 Malo or his licensors, as applicable 
  5  # 
  6  # Licensed under the Apache License, Version 2.0 (the "License"); 
  7  # you may not use this file except in compliance with the License. 
  8  # You may obtain a copy of the License at 
  9  # 
 10  #     http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, software 
 13  # distributed under the License is distributed on an "AS IS" BASIS, 
 14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 15  # See the License for the specific language governing permissions and 
 16  # limitations under the License. 
 17  """ 
 18  Application wrapper package 
 19  =========================== 
 20   
 21  This package contains application wrappers and related stuff, plus a 
 22  sample hello-world-application. 
 23  """ 
 24  __author__ = u"Andr\xe9 Malo" 
 25  __docformat__ = "restructuredtext en" 
26 27 28 -class Application(object):
29 """ 30 Application wrapper 31 32 :IVariables: 33 - `shutdown`: Shutdown callable 34 - `call`: Application callable 35 36 :Types: 37 - `shutdown`: ``callable`` 38 - `call`: ``callable`` 39 """ 40 shutdown = None 41 42 @classmethod
43 - def factory(cls, config, opts, args):
44 """ 45 Call the main application factory based on the configuration 46 47 :Parameters: 48 - `config`: Configuration 49 - `opts`: Command line options 50 - `args`: Positioned command line arguments 51 52 :Types: 53 - `config`: `wtf.config.Config` 54 - `opts`: ``optparse.OptionContainer`` 55 - `args`: ``list`` 56 57 :return: New Application instance 58 :rtype: `Application` 59 """ 60 from wtf import services, util 61 62 # BEWARE: Order is important here - first the services, then the app. 63 # This is because the app modules may depend on initialized services 64 # on top level. 65 manager = services.init( 66 config, opts, args, config.wtf('services', ()) 67 ) 68 app = manager.apply( 69 util.load_dotted(config.wtf.application)(config, opts, args) 70 ) 71 return cls(manager, app)
72
73 - def __init__(self, manager, app):
74 """ 75 Initialization 76 77 :Parameters: 78 - `manager`: Service manager 79 - `app`: WSGI callable 80 81 :Types: 82 - `manager`: `ServiceManager` 83 - `app`: ``callable`` 84 """ 85 self.shutdown = manager.shutdown 86 self.call = app
87
88 - def __del__(self):
89 """ Destruction """ 90 func, self.shutdown = self.shutdown, None 91 if func is not None: 92 func()
93 94 factory = Application.factory
95 96 97 -def hello_world(config, opts, args):
98 """ 99 Sample application bootstrapper 100 101 :Parameters: 102 - `config`: Configuration 103 - `opts`: Command line options 104 - `args`: Positioned command line arguments 105 106 :Types: 107 - `config`: `wtf.config.Config` 108 - `opts`: ``optparse.OptionContainer`` 109 - `args`: ``list`` 110 111 :return: WSGI application function 112 :rtype: ``callable`` 113 """ 114 def myapp(environ, start_response): 115 """ 116 WSGI sample application 117 118 :Parameters: 119 - `environ`: Request WSGI environment 120 - `start_response`: response starter function 121 122 :Types: 123 - `environ`: ``dict`` 124 - `start_response`: ``callable`` 125 126 :return: Response body iterator 127 :rtype: ``iterable`` 128 """ 129 # pylint: disable = W0613 130 131 start_response("200 OK", [("Content-Type", "text/plain")]) 132 return ["Hello World\n"]
133 134 return myapp 135