Example #1
0
    def fromUnicode(self, value):
        """
        Convert the input string to a boolean.

        Example:

            >>> from zope.configuration.fields import Bool
            >>> Bool().fromUnicode(u"yes")
            True
            >>> Bool().fromUnicode(u"y")
            True
            >>> Bool().fromUnicode(u"true")
            True
            >>> Bool().fromUnicode(u"no")
            False
            >>> Bool().fromUnicode(u"surprise")
            Traceback (most recent call last):
            ...
            zope.schema._bootstrapinterfaces.InvalidValue
        """
        value = value.lower()
        if value in ('1', 'true', 'yes', 't', 'y'):
            return True
        if value in ('0', 'false', 'no', 'f', 'n'):
            return False
        # Unlike the superclass, anything else is invalid.
        raise InvalidValue().with_field_and_value(self, value)
Example #2
0
    def convertTokensToValues(self, tokens):
        """Convert term tokens to the terms themselves.

        Tokens are used in the HTML form to represent terms. This method takes
        the form tokens and converts them back to terms.
        """
        values = []
        for token in tokens:
            try:
                term = self.vocabulary.getTermByToken(token)
            except LookupError, error:
                raise InvalidValue("token %r not found in vocabulary" % token)
            else:
                values.append(term.value)
 def convertTokensToValues(self, tokens):
     """Convert term tokens to the terms themselves.
     if vocabulary is None just append token in values list
     """
     values = []
     for token in tokens:
         if self.vocabulary is not None:
             try:
                 term = self.vocabulary.getTermByToken(token)
             except LookupError:
                 raise InvalidValue("token %r not found in vocabulary" %
                                    token)
             else:
                 values.append(term.value)
         else:
             values.append(token)
     return values
Example #4
0
 def _validate(self, value):
     super(PythonIdentifier, self)._validate(value)
     if value and not _is_identifier(value):
         raise InvalidValue(value).with_field_and_value(self, value)
Example #5
0
 def _validate(self, value):
     super(ASCII, self)._validate(value)
     if not value:
         return
     if not max(map(ord, value)) < 128:
         raise InvalidValue().with_field_and_value(self, value)
Example #6
0
 def _validate(self, value):
     value.strip()
     if not _is_canonical(value):
         raise InvalidValue(value)
     return value
Example #7
0
def _parse_with(func, string):
    try:
        return func(string)
    except isodate.ISO8601Error as e:
        e = InvalidValue(*e.args).with_field_and_value(None, string)
        six.reraise(InvalidValue, e, sys.exc_info()[2])