示例#1
0
    def adapt(self, value):
        """Coerces value to a native type.

        If *value* is an instance of :attr:`type_`, returns it unchanged.  If
        a string, attempts to parse it and construct a :attr:`type` as
        described in the attribute documentation.

        """
        if value is None:
            return value
        elif isinstance(value, self.type_):
            return value
        elif isinstance(value, basestring):
            if self.strip:
                value = value.strip()
            match = self.regex.match(value)
            if not match:
                raise AdaptationError()
            try:
                args = [int(match.group(f)) for f in self.used]
                return self.type_(*args)
            except (TypeError, ValueError):
                raise AdaptationError()
        else:
            raise AdaptationError()
示例#2
0
    def adapt(self, value):
        """Generic numeric coercion.

        :returns: an instance of :attr:`type_` or ``None``

        Attempt to convert *value* using the class's :attr:`type_` callable.

        """
        if value is None:
            return None
        if isinstance(value, basestring):
            value = value.strip()  # decimal.Decimal doesn't like whitespace
        try:
            native = self.type_(value)
        except (ValueError, TypeError, ArithmeticError):
            raise AdaptationError()
        else:
            if not self.signed:
                if native < 0:
                    raise AdaptationError()
            return native
示例#3
0
    def adapt(self, value):
        """Coerce *value* to ``bool``.

        :returns: a ``bool`` or ``None``

        If *value* is a string, returns ``True`` if the value is in
        :attr:`true_synonyms`, ``False`` if in :attr:`false_synonyms` and
        ``None`` otherwise.

        For non-string values, equivalent to ``bool(value)``.

        """
        if not isinstance(value, basestring):
            return bool(value)
        elif value == self.true or value in self.true_synonyms:
            return True
        elif value == self.false or value in self.false_synonyms:
            return False
        raise AdaptationError()
示例#4
0
    def adapt(self, value):
        """Coerces value to a native UNIX timestamp.

        If value is an instance of int and it is a correct UNIX timestamp,
        returns it unchanged. Otherwise uses DateTime superclass to parse it.
        """
        if isinstance(value, int):
            try:
                # check if a value is a correct timestamp
                dt = datetime.datetime.utcfromtimestamp(value)
                return value
            except ValueError:
                raise AdaptationError()
        dt = super(DateTimeUNIX, self).adapt(value)
        if isinstance(dt, datetime.datetime):
            # XXX forces circular dependency when it is in the head import block
            from MoinMoin.themes import utctimestamp
            # TODO: Add support for timezones
            dt = utctimestamp(dt)
        return dt
示例#5
0
 def adapt(self, value):
     value = self.child_schema.adapt(value)
     if not self.valid_value(self, value):
         raise AdaptationError()
     return value