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

Source Code for Package wtf.impl

  1  # -*- coding: ascii -*- 
  2  # 
  3  # Copyright 2006-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  WSGI Implementations 
 19  ==================== 
 20   
 21  This package holds implementations of specific WSGI bridges. 
 22  """ 
 23  __author__ = u"Andr\xe9 Malo" 
 24  __docformat__ = "restructuredtext en" 
 25   
 26  from wtf.config import ConfigurationError 
 27   
 28   
29 -class ServerInterface(object):
30 """ 31 Interface for WSGI server implementations 32 """ 33
34 - def __init__(self, config, opts, args):
35 """ 36 Initialization 37 38 :Parameters: 39 - `config`: The application config 40 - `opts`: Command line option container 41 - `args`: Fixed commandline arguments 42 43 :Types: 44 - `config`: `wtf.config.Config` 45 - `opts`: ``optparse.OptionContainer`` 46 - `args`: ``list`` 47 """
48
49 - def handle(self, accepted, application, flags):
50 """ 51 Handle an accepted socket 52 53 :Parameters: 54 - `accepted`: The accepted socket, being a tuple of socket object and 55 peername 56 - `application`: The WSGI application to call 57 - `flags`: Worker flags 58 59 :Types: 60 - `accepted`: ``tuple`` 61 - `application`: ``callable`` 62 - `flags`: `FlagsInterface` 63 """
64 65
66 -class FlagsInterface(object):
67 """ 68 Interface for worker flag containers 69 70 :CVariables: 71 - `multithread`: Is it a multithreaded server? 72 - `multiprocess`: Is it a multiprocessed server? 73 - `run_once`: Is the server supposed to run once? 74 75 :Types: 76 - `multithread`: ``bool`` 77 - `multiprocess`: ``bool`` 78 - `run_once`: ``bool`` 79 """ 80
81 - def shutdown(self):
82 """ 83 Retrieve shutdown-pending flag 84 85 :return: The state of the flag 86 :rtype: ``bool`` 87 """
88 89
90 -def factory(config, opts, args):
91 """ 92 Create the server instance selected by configuration 93 94 :Parameters: 95 - `config`: configuration 96 - `opts`: Option container 97 - `args`: Fixed arguments 98 99 :Types: 100 - `config`: `config.Config` 101 - `opts`: ``optparse.OptionContainer`` 102 - `args`: ``list`` 103 104 :return: server instance 105 :rtype: `ServerInterface` 106 """ 107 self = factory 108 109 try: 110 protocol = config.wtf.protocol 111 except KeyError: 112 raise ConfigurationError("Missing protocol configuration") 113 114 if protocol not in self.impl: # pylint: disable = E1101 115 raise ConfigurationError("Unknown protocol %s" % protocol) 116 117 return self.impl[protocol](config, opts, args) # pylint: disable = E1101
118 factory.impl = {} # pylint: disable = W0612 119 120
121 -def register(name, klass):
122 """ 123 Register a server implementation 124 125 :Parameters: 126 - `name`: The name (in the config) 127 - `klass`: The implementation class 128 129 :Types: 130 - `name`: ``str`` 131 - `klass`: `ServerInterface` 132 """ 133 factory.impl[name] = klass # pylint: disable = E1101
134