Ejemplo n.º 1
0
Archivo: _base.py Proyecto: rch/rpclib
def base_from_element(prot, cls, element):
    if prot.validator is prot.SOFT_VALIDATION and not (cls.validate_string(
            cls, element.text)):
        raise ValidationError(element.text)
    retval = cls.from_string(element.text)
    if prot.validator is prot.SOFT_VALIDATION and not (cls.validate_native(
            cls, retval)):
        raise ValidationError(element.text)
    return retval
Ejemplo n.º 2
0
    def from_string(cls, string):
        duration = _duration_re.match(string).groupdict(0)
        if duration is None:
            ValidationError(string)

        days = int(duration['days'])
        days += int(duration['months']) * 30
        days += int(duration['years']) * 365
        hours = int(duration['hours'])
        minutes = int(duration['minutes'])
        seconds = float(duration['seconds'])
        f, i = math.modf(seconds)
        seconds = i
        microseconds = int(1e6 * f)

        delta = datetime.timedelta(days=days,
                                   hours=hours,
                                   minutes=minutes,
                                   seconds=seconds,
                                   microseconds=microseconds)

        if duration['sign'] == "-":
            delta *= -1

        return delta
Ejemplo n.º 3
0
 def from_string(cls, string):
     try:
         return int(string)
     except ValueError:
         try:
             return int(string)
         except ValueError:
             raise ValidationError(string)
Ejemplo n.º 4
0
    def from_string(cls, string):
        """Expects ISO formatted dates."""

        match = _date_re.match(string)
        if match is None:
            raise ValidationError(string)

        fields = match.groupdict(0)

        return datetime.date(fields['year'], fields['month'], fields['day'])
Ejemplo n.º 5
0
Archivo: _base.py Proyecto: rch/rpclib
 def wrapper(prot, cls, element):
     if bool(element.get('{%s}nil' % _ns_xsi)):
         if prot.validator is prot.SOFT_VALIDATION and (
                 not cls.Attributes.nillable
                 or cls.Attributes._has_non_nillable_children):
             raise ValidationError('')
         else:
             return cls.Attributes.default
     else:
         return func(prot, cls, element)
Ejemplo n.º 6
0
    def from_string(cls, string):
        """Expects ISO formatted dates."""

        match = _time_re.match(string)
        if match is None:
            raise ValidationError(string)

        fields = match.groupdict(0)

        return datetime.time(int(fields['hr']), int(fields['min']),
                             int(fields['sec']),
                             int(fields.get("sec_frac", '.')[1:]))
Ejemplo n.º 7
0
    def from_string(cls, string):
        """expect ISO formatted dates"""

        match = _utc_re.match(string)
        if match:
            return cls.parse(match, tz=pytz.utc)

        match = _offset_re.match(string)
        if match:
            tz_hr, tz_min = [int(match.group(x)) for x in ("tz_hr", "tz_min")]
            return cls.parse(match, tz=FixedOffset(tz_hr * 60 + tz_min, {}))

        match = _local_re.match(string)
        if match is None:
            raise ValidationError(string)

        return cls.parse(match)
Ejemplo n.º 8
0
 def from_string(cls, string):
     try:
         return etree.fromstring(string)
     except etree.XMLSyntaxError:
         raise ValidationError(string)
Ejemplo n.º 9
0
 def from_string(cls, string):
     try:
         return decimal.Decimal(string)
     except decimal.InvalidOperation, e:
         raise ValidationError(string)
Ejemplo n.º 10
0
 def from_string(cls, string):
     try:
         return pickle.loads(string)
     except:
         raise ValidationError(string)
Ejemplo n.º 11
0
Archivo: http.py Proyecto: rch/rpclib
    def dict_to_object(self, doc, inst_class):
        simple_type_info = inst_class.get_simple_type_info(inst_class)
        inst = inst_class.get_deserialization_instance()

        # this is for validating cls.Attributes.{min,max}_occurs
        frequencies = {}

        for k, v in doc.items():
            member = simple_type_info.get(k, None)
            if member is None:
                logger.debug("discarding field %r" % k)
                continue

            mo = member.type.Attributes.max_occurs
            value = getattr(inst, k, None)
            if value is None:
                value = []
            elif mo == 1:
                raise Fault('Client.ValidationError',
                        '"%s" member must occur at most %d times' % (k, max_o))

            for v2 in v:
                if (self.validator is self.SOFT_VALIDATION and not
                            member.type.validate_string(member.type, v2)):
                    raise ValidationError(v2)
                native_v2 = member.type.from_string(v2)
                if (self.validator is self.SOFT_VALIDATION and not
                            member.type.validate_native(member.type, native_v2)):
                    raise ValidationError(v2)

                value.append(native_v2)

                # set frequencies of parents.
                if not (member.path[:-1] in frequencies):
                    for i in range(1,len(member.path)):
                        logger.debug("\tset freq %r = 1" % (member.path[:i],))
                        frequencies[member.path[:i]] = 1

                freq = frequencies.get(member.path, 0)
                freq += 1
                frequencies[member.path] = freq
                logger.debug("\tset freq %r = %d" % (member.path, freq))

            if mo == 1:
                value = value[0]

            cinst = inst
            ctype_info = inst_class.get_flat_type_info(inst_class)
            pkey = member.path[0]
            for i in range(len(member.path) - 1):
                pkey = member.path[i]
                if not (ctype_info[pkey].Attributes.max_occurs in (0,1)):
                    raise Exception("HttpRpc deserializer does not support "
                                    "non-primitives with max_occurs > 1")

                ninst = getattr(cinst, pkey, None)
                if ninst is None:
                    ninst = ctype_info[pkey].get_deserialization_instance()
                    setattr(cinst, pkey, ninst)
                cinst = ninst

                ctype_info = ctype_info[pkey]._type_info

            if isinstance(cinst, list):
                cinst.extend(value)
                logger.debug("\tset array   %r(%r) = %r" %
                                                    (member.path, pkey, value))
            else:
                setattr(cinst, member.path[-1], value)
                logger.debug("\tset default %r(%r) = %r" %
                                                    (member.path, pkey, value))

        try:
            print inst.qo.vehicles
        except Exception,e:
            print e
Ejemplo n.º 12
0
def enum_from_element(prot, cls, element):
    if prot.validator is prot.SOFT_VALIDATION and not (
                                        cls.validate_string(cls, element.text)):
        raise ValidationError(element.text)
    return getattr(cls, element.text)