示例#1
0
    def register_path(self, model, path, variables, converters, required,
                      get_converters, absorb, model_factory):
        """Register a route.

        See :meth:`morepath.App.path` for more information.

        :param model: model class
        :param path: route
        :param variables: function that given model instance extracts
          dictionary with variables used in path and URL parameters.
        :param converters: converters structure
        :param required: required URL parameters
        :param get_converters: get a converter dynamically.
        :param absorb: absorb path
        :param model_factory: function that constructs model object given
          variables extracted from path and URL parameters.
        """
        converters = converters or {}
        if get_converters is not None:
            converters.update(get_converters())
        arguments = get_arguments(model_factory, SPECIAL_ARGUMENTS)
        converters = self.converter_registry.argument_and_explicit_converters(
            arguments, converters)

        info = arginfo(model_factory)
        if info.varargs is not None:
            raise DirectiveError(
                "Cannot use varargs in function signature: %s" % info.varargs)
        if info.keywords is not None:
            raise DirectiveError(
                "Cannot use keywords in function signature: %s" %
                info.keywords)

        path_variables = TrajectPath(path).variables()
        for path_variable in path_variables:
            if path_variable not in arguments:
                raise DirectiveError(
                    "Variable in path not found in function signature: %s" %
                    path_variable)

        parameters = filter_arguments(arguments, path_variables)
        if required is None:
            required = set()
        required = set(required)

        extra = 'extra_parameters' in arguments
        if parameters or converters or required or extra:
            parameter_factory = ParameterFactory(parameters, converters,
                                                 required, extra)
        else:
            parameter_factory = _simple_parameter_factory

        self.add_pattern(path, (model_factory, parameter_factory), converters,
                         absorb)

        if variables is not None:
            self.register_path_variables(model, variables)

        self.register_inverse_path(model, path, arguments, converters, absorb)
示例#2
0
def get_converter(type):
    """Get the converter for a given type.

    :param type: a class or type.
    :return: a :class:`morepath.Converter` instance.
    """
    raise DirectiveError("Cannot find converter for type: %r" % type)
示例#3
0
    def converter_for_type(self, type):
        """Get converter for type.

        Is aware of inheritance; if nothing is registered for given
        type it returns the converter registered for its base class.

        :param type: The type for which to look up the converter.
        :return: a :class:`morepath.Converter` instance.
        """
        result = self._registry.component(type)
        if result is None:
            raise DirectiveError("Cannot find converter for type: %r" % type)
        return result
示例#4
0
    def converter_for_value(self, v):
        """Get converter for value.

        Is aware of inheritance; if nothing is registered for type of
        given value it returns the converter registered for its base class.

        :param value: The value for which to look up the converter.
        :return: a :class:`morepath.Converter` instance.
        """
        if v is None:
            return IDENTITY_CONVERTER
        try:
            return self.converter_for_type(type(v))
        except DirectiveError:
            raise DirectiveError(
                "Cannot find converter for default value: %r (%s)" %
                (v, type(v)))