class ConverterRegistry(object): """A registry for converters. Used to decode/encode URL parameters and path variables used by the :meth:`morepath.AppBase.path` directive. Is aware of inheritance. """ def __init__(self): self._map = Map() 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._map[ClassMapKey(type)] = converter def converter_for_type(self, type): """Get converter for type. Is aware of inheritance; if nothing is registered for given type will return converter registered for base class. :param type: The type for which to look up the converter. :returns: a :class:`morepath.Converter` instance. """ return self._map.get(ClassMapKey(type)) def converter_for_value(self, v): """Get converter for value. Is aware of inheritance; if nothing is registered for type of given value will return converter registered for base class. :param value: The value for which to look up the converter. :returns: a :class:`morepath.Converter` instance. """ if v is None: return IDENTITY_CONVERTER return self.converter_for_type(type(v))
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._map = Map() def clear(self): self._map = Map() 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._map[ClassMapKey(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. :returns: a :class:`morepath.Converter` instance. """ result = self._map.get(ClassMapKey(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. :returns: 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. :returns: 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
def test_map_get_default(): m = Map() a = MapKey('a') assert m.get(a, 'default') == 'default'
def test_map_get(): m = Map() a = MapKey('a') m[a] = u'Value for A' assert m.get(a) == u'Value for A'