def __init__(self, namespace, type_handlers): assert is_valid_dot_atom(namespace) # The list-id-namespace that will be used when generating the list-id # string. This should be a domain name under the control of the # generator (see RFC 2919.) self.__namespace = namespace # A mapping of classes to functions that accept an instance of that # class, returning a tuple of values that will be used to generate the # list label. Returned values must be valid RFC 2822 dot-atom-text # values. self.__type_handlers = type_handlers
def __call__(self, instance): """ Build a list-id string from an instance. Raises ``UnregisteredTypeError`` if there is no registered handler for the instance type. Raises ``AssertionError`` if a valid list-id string cannot be generated from the values returned by the type handler. """ try: handler = self.__type_handlers[type(instance)] except KeyError: raise self.UnregisteredTypeError("Cannot generate mailing list identifier for {!r}".format(instance)) label = ".".join(map(str, handler(instance))) assert is_valid_dot_atom(label) return "{}.{}".format(label, self.__namespace)
def __call__(self, instance): """ Build a list-id string from an instance. Raises ``UnregisteredTypeError`` if there is no registered handler for the instance type. Raises ``AssertionError`` if a valid list-id string cannot be generated from the values returned by the type handler. """ try: handler = self.__type_handlers[type(instance)] except KeyError: raise self.UnregisteredTypeError( f"Cannot generate mailing list identifier for {instance!r}") label = ".".join(map(str, handler(instance))) assert is_valid_dot_atom(label) return f"<{label}.{self.__namespace}>"
def __call__(self, instance): """ Build a list-id string from an instance. Raises ``UnregisteredTypeError`` if there is no registered handler for the instance type. Raises ``AssertionError`` if a valid list-id string cannot be generated from the values returned by the type handler. """ try: handler = self.__type_handlers[type(instance)] except KeyError: raise self.UnregisteredTypeError( u'Cannot generate mailing list identifier for {!r}'.format( instance)) label = '.'.join(map(six.binary_type, handler(instance))) assert is_valid_dot_atom(label) return u'{}.{}'.format(label, self.__namespace)
def test_is_valid_dot_atom(): assert is_valid_dot_atom("foo") assert is_valid_dot_atom("foo.bar") assert not is_valid_dot_atom(".foo.bar") assert not is_valid_dot_atom("foo.bar.") assert not is_valid_dot_atom("foo.\x00")
def test_is_valid_dot_atom(): assert is_valid_dot_atom('foo') assert is_valid_dot_atom('foo.bar') assert not is_valid_dot_atom('.foo.bar') assert not is_valid_dot_atom('foo.bar.') assert not is_valid_dot_atom('foo.\x00')