Esempio n. 1
0
    def test_get_existing_keys_from_default(self):
        config = Config({}, {'foo': {
            'bar': 'baz',
            'doo': 'ding',
        }})

        self.assertEqual(config.get_config('foo', 'bar'), 'baz')
        self.assertEqual(config.get_config('foo', 'doo'), 'ding')
Esempio n. 2
0
    def test_get_existing_keys_from_default(self):
        config = Config({}, {'foo': {
            'bar': 'baz',
            'doo': 'ding',
        }})

        self.assertEqual(config.get_config('foo', 'bar'), 'baz')
        self.assertEqual(config.get_config('foo', 'doo'), 'ding')
Esempio n. 3
0
    def test_default_config(self):
        config = Config()

        from resources.template import default_config as template_config
        from resources.i18n import default_config as i18n_config

        self.assertEqual(config.get_config('resources.template', 'templates_dir'), template_config['templates_dir'])
        self.assertEqual(config.get_config('resources.i18n', 'locale'), i18n_config['locale'])
        self.assertEqual(config.get_config('resources.i18n', 'timezone'), i18n_config['timezone'])
Esempio n. 4
0
    def test_override_config2(self):
        config = Config({
            'resources.i18n': {
                'timezone': 'America/Sao_Paulo',
            },
        })

        self.assertEqual(config.get_config('resources.i18n', 'locale'), 'en_US')
        self.assertEqual(config.get_config('resources.i18n', 'timezone'), 'America/Sao_Paulo')
Esempio n. 5
0
    def test_get_dict_existing_keys(self):
        config = Config({'foo': {
            'bar': 'baz',
            'doo': 'ding',
        }})

        self.assertEqual(config.get_config('foo'), {
            'bar': 'baz',
            'doo': 'ding',
        })
Esempio n. 6
0
    def test_default_config_with_non_existing_key(self):
        config = Config()

        from resources.i18n import default_config as i18n_config

        # In the first time the module config will be loaded normally.
        self.assertEqual(config.get_config('resources.i18n', 'locale'), i18n_config['locale'])

        # In the second time it won't be loaded, but won't find the value and then use the default.
        self.assertEqual(config.get_config('resources.i18n', 'i_dont_exist', 'foo'), 'foo')
Esempio n. 7
0
    def test_get_dict_existing_keys(self):
        config = Config({'foo': {
            'bar': 'baz',
            'doo': 'ding',
        }})

        self.assertEqual(config.get_config('foo'), {
            'bar': 'baz',
            'doo': 'ding',
        })
Esempio n. 8
0
    def test_override_config2(self):
        config = Config({
            'resources.i18n': {
                'timezone': 'America/Sao_Paulo',
            },
        })

        self.assertEqual(config.get_config('resources.i18n', 'locale'),
                         'en_US')
        self.assertEqual(config.get_config('resources.i18n', 'timezone'),
                         'America/Sao_Paulo')
Esempio n. 9
0
    def test_default_config_with_non_existing_key(self):
        config = Config()

        from resources.i18n import default_config as i18n_config

        # In the first time the module config will be loaded normally.
        self.assertEqual(config.get_config('resources.i18n', 'locale'),
                         i18n_config['locale'])

        # In the second time it won't be loaded, but won't find the value and then use the default.
        self.assertEqual(
            config.get_config('resources.i18n', 'i_dont_exist', 'foo'), 'foo')
Esempio n. 10
0
    def test_get(self):
        config = Config({'foo': {
            'bar': 'baz',
            'doo': 'ding',
        }})

        self.assertEqual(config.get('foo'), {
            'bar': 'baz',
            'doo': 'ding',
        })

        self.assertEqual(config.get('bar'), {})
Esempio n. 11
0
    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')
Esempio n. 12
0
    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')
Esempio n. 13
0
    def test_get(self):
        config = Config({'foo': {
            'bar': 'baz',
            'doo': 'ding',
        }})

        self.assertEqual(config.get('foo'), {
            'bar': 'baz',
            'doo': 'ding',
        })

        self.assertEqual(config.get('bar'), {})
Esempio n. 14
0
    def test_update(self):
        config = Config({'foo': {
            'bar': 'baz',
            'doo': 'ding',
        }})

        self.assertEqual(config.get_config('foo', 'bar'), 'baz')
        self.assertEqual(config.get_config('foo', 'doo'), 'ding')

        config.update('foo', {'bar': 'other'})

        self.assertEqual(config.get_config('foo', 'bar'), 'other')
        self.assertEqual(config.get_config('foo', 'doo'), 'ding')
Esempio n. 15
0
    def test_default_config(self):
        config = Config()

        from resources.template import default_config as template_config
        from resources.i18n import default_config as i18n_config

        self.assertEqual(
            config.get_config('resources.template', 'templates_dir'),
            template_config['templates_dir'])
        self.assertEqual(config.get_config('resources.i18n', 'locale'),
                         i18n_config['locale'])
        self.assertEqual(config.get_config('resources.i18n', 'timezone'),
                         i18n_config['timezone'])
Esempio n. 16
0
    def test_override_config(self):
        config = Config({
            'resources.template': {
                'templates_dir': 'apps/templates'
            },
            'resources.i18n': {
                'locale': 'pt_BR',
                'timezone': 'America/Sao_Paulo',
            },
        })

        self.assertEqual(config.get_config('resources.template', 'templates_dir'), 'apps/templates')
        self.assertEqual(config.get_config('resources.i18n', 'locale'), 'pt_BR')
        self.assertEqual(config.get_config('resources.i18n', 'timezone'), 'America/Sao_Paulo')
Esempio n. 17
0
    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)
Esempio n. 18
0
    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')
Esempio n. 19
0
    def test_setitem_no_dict_values(self):
        config = Config()

        def setitem(key, value):
            config[key] = value
            return config

        self.assertRaises(AssertionError, setitem, 'foo', 'bar')
        self.assertRaises(AssertionError, setitem, 'foo', None)
Esempio n. 20
0
    def test_override_config(self):
        config = Config({
            'resources.template': {
                'templates_dir': 'apps/templates'
            },
            'resources.i18n': {
                'locale': 'pt_BR',
                'timezone': 'America/Sao_Paulo',
            },
        })

        self.assertEqual(
            config.get_config('resources.template', 'templates_dir'),
            'apps/templates')
        self.assertEqual(config.get_config('resources.i18n', 'locale'),
                         'pt_BR')
        self.assertEqual(config.get_config('resources.i18n', 'timezone'),
                         'America/Sao_Paulo')
Esempio n. 21
0
    def test_update(self):
        config = Config({'foo': {
            'bar': 'baz',
            'doo': 'ding',
        }})

        self.assertEqual(config.get_config('foo', 'bar'), 'baz')
        self.assertEqual(config.get_config('foo', 'doo'), 'ding')

        config.update('foo', {'bar': 'other'})

        self.assertEqual(config.get_config('foo', 'bar'), 'other')
        self.assertEqual(config.get_config('foo', 'doo'), 'ding')
Esempio n. 22
0
    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')
Esempio n. 23
0
 def test_missing_module2(self):
     config = Config()
     self.assertRaises(KeyError, config.get_config, 'i_dont_exist')
Esempio n. 24
0
 def test_required_config(self):
     config = Config()
     self.assertRaises(KeyError, config.get_config, 'resources.i18n', 'foo')
Esempio n. 25
0
 def test_missing_key(self):
     config = Config()
     self.assertRaises(KeyError, config['resources.i18n'].__getitem__,
                       'i_dont_exist')
Esempio n. 26
0
 def test_missing_key(self):
     config = Config()
     self.assertRaises(KeyError, config.get_config, 'resources.i18n',
                       'i_dont_exist')
Esempio n. 27
0
    def test_setdefault_no_dict_values(self):
        config = Config()

        self.assertRaises(AssertionError, config.setdefault, 'foo', 'bar')
        self.assertRaises(AssertionError, config.setdefault, 'foo', None)
Esempio n. 28
0
    def test_update_no_dict_values(self):
        config = Config()

        self.assertRaises(AssertionError, config.update, {'foo': 'bar'}, 'baz')
        self.assertRaises(AssertionError, config.update, {'foo': None}, 'baz')
        self.assertRaises(AssertionError, config.update, 'foo', 'bar')
Esempio n. 29
0
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)
Esempio n. 30
0
 def test_get_with_default_and_module_load(self):
     config = Config()
     self.assertEqual(config.get_config('resources.i18n', 'locale'), 'en_US')
     self.assertEqual(config.get_config('resources.i18n', 'locale', 'foo'), 'en_US')
Esempio n. 31
0
 def test_missing_module(self):
     config = Config()
     self.assertRaises(KeyError, config.__getitem__, 'i_dont_exist')
Esempio n. 32
0
 def test_required_config(self):
     config = Config()
     self.assertRaises(KeyError, config['resources.i18n'].__getitem__,
                       'foo')
     self.assertRaises(KeyError, config['resources.i18n'].__getitem__,
                       'required')
Esempio n. 33
0
    def test_get_with_default_and_none(self):
        config = Config({'foo': {
            'bar': None,
        }})

        self.assertEqual(config.get_config('foo', 'bar', 'ooops'), None)
Esempio n. 34
0
    def test_get_with_default(self):
        config = Config()

        self.assertRaises(KeyError, config.get_config, 'foo', 'bar', 'ooops')
        self.assertRaises(KeyError, config.get_config, 'foo', 'doo', 'wooo')
Esempio n. 35
0
    def test_get_dict_non_existing_keys(self):
        config = Config()

        self.assertRaises(KeyError, config.get_config, 'bar')
Esempio n. 36
0
    def test_get(self):
        config = Config({'foo': {
            'bar': 'baz',
        }})

        self.assertEqual(config.get_config('foo', 'bar'), 'baz')
Esempio n. 37
0
 def test_get_with_default_and_module_load(self):
     config = Config()
     self.assertEqual(config['resources.i18n']['locale'], 'en_US')
     self.assertEqual(config['resources.i18n'].get('locale', 'foo'),
                      'en_US')
Esempio n. 38
0
    def test_get_with_default(self):
        config = Config()

        self.assertEqual(config.get_config('resources.i18n', 'bar', 'baz'), 'baz')
Esempio n. 39
0
    def test_get_with_default(self):
        config = Config()

        self.assertEqual(config['resources.i18n'].get('bar', 'baz'), 'baz')
Esempio n. 40
0
    def test_get(self):
        config = Config({'foo': {
            'bar': 'baz',
        }})

        self.assertEqual(config.get_config('foo', 'bar'), 'baz')
Esempio n. 41
0
    def test_get(self):
        config = Config({'foo': {
            'bar': 'baz',
        }})

        self.assertEqual(config['foo']['bar'], 'baz')
Esempio n. 42
0
    def test_get_with_default_and_none(self):
        config = Config({'foo': {
            'bar': None,
        }})

        self.assertEqual(config.get_config('foo', 'bar', 'ooops'), None)
Esempio n. 43
0
 def test_missing_default_config(self):
     config = Config()
     self.assertRaises(KeyError, config.get_config, 'tipfy', 'foo')
Esempio n. 44
0
 def test_missing_default_config(self):
     config = Config()
     self.assertRaises(KeyError, config['tipfy'].__getitem__, 'foo')
Esempio n. 45
0
    def test_setitem(self):
        config = Config()
        config['foo'] = {'bar': 'baz'}

        self.assertEqual(config, {'foo': {'bar': 'baz'}})
        self.assertEqual(config['foo'], {'bar': 'baz'})