Beispiel #1
0
class ViewMatcher(Matcher):
    def __init__(self):
        self.reg = PredicateRegistry([Predicate('name', KeyIndex),
                                      Predicate('request_method', KeyIndex)])

    def register(self, predicates, registration):
        self.reg.register(predicates, registration)

    def get_predicates(self, request):
        result = {}
        result['request_method'] = request.method
        result['name'] = request.resolver_info()['name']
        return result

    def __call__(self, request, model):
        request_predicates = self.get_predicates(request)
        return self.reg.get(request_predicates)
Beispiel #2
0
class ViewMatcher(Matcher):
    def __init__(self):
        self.reg = PredicateRegistry([
            Predicate('name', KeyIndex),
            Predicate('request_method', KeyIndex)
        ])

    def register(self, predicates, registration):
        self.reg.register(predicates, registration)

    def get_predicates(self, request):
        result = {}
        result['request_method'] = request.method
        result['name'] = request.resolver_info()['name']
        return result

    def __call__(self, request, model):
        request_predicates = self.get_predicates(request)
        return self.reg.get(request_predicates)
Beispiel #3
0
 def __init__(self):
     self._registry = PredicateRegistry(match_class('cls', lambda cls: cls))
Beispiel #4
0
class ConverterRegistry(object):
    """A registry for converters.

    Used to decode/encode URL parameters and path variables used
    by the :meth:`morepath.App.path` directive.

    Is aware of inheritance.
    """
    def __init__(self):
        self._registry = PredicateRegistry(match_class('cls', lambda cls: cls))

    def register_converter(self, type, converter):
        """Register a converter for type.

        :param type: the Python type for which to register
          the converter.
        :param converter: a :class:`morepath.Converter` instance.
        """
        self._registry.register(type, converter)

    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

    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)))

    def converter_for_explicit_or_type(self, c):
        """Given a converter or a type, turn it into an explicit one.
        """
        if type(c) in [type, ClassType]:
            return self.converter_for_type(c)
        return c

    def converter_for_explicit_or_type_or_list(self, c):
        """Given a converter or type or list, turn it into an explicit one.

        :param c: can either be a converter, or a type for which
          a converter can be looked up, or a list with a converter or a type
          in it.
        :return: a :class:`Converter` instance.
        """
        if isinstance(c, list):
            if len(c) == 0:
                c = IDENTITY_CONVERTER
            else:
                c = self.converter_for_explicit_or_type(c[0])
            return ListConverter(c)
        return self.converter_for_explicit_or_type(c)

    def explicit_converters(self, converters):
        """Given converter dictionary, make everything in it explicit.

        This means types have converters looked up for them, and
        lists are turned into :class:`ListConverter`.
        """
        return {
            name: self.converter_for_explicit_or_type_or_list(value)
            for name, value in converters.items()
        }

    def argument_and_explicit_converters(self, arguments, converters):
        """Use explict converters unless none supplied, then use default args.
        """
        result = self.explicit_converters(converters)
        for name, value in arguments.items():
            if name not in result:
                result[name] = self.converter_for_value(value)
        return result
Beispiel #5
0
 def __init__(self):
     self._registry = PredicateRegistry(match_class('cls', lambda cls: cls))
Beispiel #6
0
class ConverterRegistry(object):
    """A registry for converters.

    Used to decode/encode URL parameters and path variables used
    by the :meth:`morepath.App.path` directive.

    Is aware of inheritance.
    """
    def __init__(self):
        self._registry = PredicateRegistry(match_class('cls', lambda cls: cls))

    def register_converter(self, type, converter):
        """Register a converter for type.

        :param type: the Python type for which to register
          the converter.
        :param converter: a :class:`morepath.Converter` instance.
        """
        self._registry.register(type, converter)

    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

    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)))

    def converter_for_explicit_or_type(self, c):
        """Given a converter or a type, turn it into an explicit one.
        """
        if type(c) in [type, ClassType]:
            return self.converter_for_type(c)
        return c

    def converter_for_explicit_or_type_or_list(self, c):
        """Given a converter or type or list, turn it into an explicit one.

        :param c: can either be a converter, or a type for which
          a converter can be looked up, or a list with a converter or a type
          in it.
        :return: a :class:`Converter` instance.
        """
        if isinstance(c, list):
            if len(c) == 0:
                c = IDENTITY_CONVERTER
            else:
                c = self.converter_for_explicit_or_type(c[0])
            return ListConverter(c)
        return self.converter_for_explicit_or_type(c)

    def explicit_converters(self, converters):
        """Given converter dictionary, make everything in it explicit.

        This means types have converters looked up for them, and
        lists are turned into :class:`ListConverter`.
        """
        return {name: self.converter_for_explicit_or_type_or_list(value) for
                name, value in converters.items()}

    def argument_and_explicit_converters(self, arguments, converters):
        """Use explict converters unless none supplied, then use default args.
        """
        result = self.explicit_converters(converters)
        for name, value in arguments.items():
            if name not in result:
                result[name] = self.converter_for_value(value)
        return result
Beispiel #7
0
 def __init__(self):
     self.reg = PredicateRegistry([Predicate('name', KeyIndex),
                                   Predicate('request_method', KeyIndex)])
Beispiel #8
0
 def clear(self):
     self._registry = PredicateRegistry(match_class('cls', lambda cls: cls))
Beispiel #9
0
 def __init__(self):
     self._registry = PredicateRegistry(match_class('cls'))
Beispiel #10
0
 def __init__(self):
     self.reg = PredicateRegistry([
         Predicate('name', KeyIndex),
         Predicate('request_method', KeyIndex)
     ])