def test_setdefault(self): config = Config() self.assertRaises(KeyError, config.get_config, 'foo') config.setdefault('foo', { 'bar': 'baz', 'doo': 'ding', }) self.assertEqual(config.get_config('foo', 'bar'), 'baz') self.assertEqual(config.get_config('foo', 'doo'), 'ding')
def test_setdefault2(self): config = Config({'foo': { 'bar': 'baz', }}) self.assertEqual(config.get_config('foo'), { 'bar': 'baz', }) config.setdefault('foo', { 'bar': 'wooo', 'doo': 'ding', }) self.assertEqual(config.get_config('foo', 'bar'), 'baz') self.assertEqual(config.get_config('foo', 'doo'), 'ding')
class WSGIApplication(object): """The WSGI application which centralizes URL dispatching, configuration and hooks for an App Rngine app. """ #: Default class for requests. request_class = Request #: Default class for responses. response_class = Response def __init__(self, config=None): """Initializes the application. :param config: Dictionary with configuration for the application modules. """ # Set an accessor to this instance. local.app = self # Load default config and update with values for this instance. self.config = Config(config) self.config.setdefault('tipfy', default_config) # Set the url rules. self.url_map = self.config.get('tipfy', 'url_map') if not self.url_map: self.url_map = self.get_url_map() # Cache for loaded handler classes. self.handlers = {} extensions = self.config.get('tipfy', 'extensions') middleware = self.config.get('tipfy', 'middleware') if extensions: # For backwards compatibility only. set_extensions_compatibility(extensions, middleware) # Middleware factory and registry. self.middleware_factory = factory = MiddlewareFactory() # Store the app middleware dict. self.middleware = factory.get_middleware(self, middleware) def __call__(self, environ, start_response): """Called by WSGI when a request comes in.""" try: # Set local variables for a single request. local.app = self local.request = request = self.request_class(environ) # Kept here for backwards compatibility. Soon to be removed. local.response = self.response_class() self.url_adapter = self.rule = self.rule_args = None # Check requested method. method = request.method.lower() if method not in ALLOWED_METHODS: raise MethodNotAllowed() # Bind url map to the current request location. self.url_adapter = self.url_map.bind_to_environ(environ, server_name=self.config.get('tipfy', 'server_name', None), subdomain=self.config.get('tipfy', 'subdomain', None)) # Match the path against registered rules. self.rule, self.rule_args = self.url_adapter.match(request.path, return_rule=True) # Import handler set in matched rule. name = self.rule.handler if name not in self.handlers: self.handlers[name] = import_string(name) # Execute pre_dispatch_handler middleware. for hook in self.middleware.get('pre_dispatch_handler', []): response = hook() if response: break else: # Instantiate handler and dispatch request method. handler = self.handlers[name]() response = handler.dispatch(method, **self.rule_args) # Execute post_dispatch_handler middleware. for hook in self.middleware.get('post_dispatch_handler', []): response = hook(response) except RequestRedirect, e: # Execute redirects raised by the routing system or the application. response = e except Exception, e: # Handle http and uncaught exceptions. response = self.handle_exception(e)