Ejemplo n.º 1
0
def loadxml(el, datatype):
    print (el, datatype, len(el))
    if el.get('nil') == 'true':
        return None
    if isinstance(datatype, list):
        return [loadxml(item, datatype[0]) for item in el.findall('item')]
    elif isarray(datatype):
        return [
            loadxml(item, datatype.item_type) for item in el.findall('item')
        ]
    elif isinstance(datatype, dict):
        key_type, value_type = list(datatype.items())[0]
        return dict((
            (loadxml(item.find('key'), key_type),
                loadxml(item.find('value'), value_type))
            for item in el.findall('item')
        ))
    elif isdict(datatype):
        return dict((
            (loadxml(item.find('key'), datatype.key_type),
                loadxml(item.find('value'), datatype.value_type))
            for item in el.findall('item')
        ))
    elif isdict(datatype):
        return dict((
            (loadxml(item.find('key'), datatype.key_type),
                loadxml(item.find('value'), datatype.value_type))
            for item in el.findall('item')
        ))
    elif len(el):
        d = {}
        for attr in datatype._wsme_attributes:
            name = attr.name
            child = el.find(name)
            print (name, attr, child)
            if child is not None:
                d[name] = loadxml(child, attr.datatype)
        print (d)
        return d
    else:
        if datatype == wsme.types.binary:
            return base64.decodestring(el.text.encode('ascii'))
        if isusertype(datatype):
            datatype = datatype.basetype
        if datatype == datetime.date:
            return parse_isodate(el.text)
        if datatype == datetime.time:
            return parse_isotime(el.text)
        if datatype == datetime.datetime:
            return parse_isodatetime(el.text)
        if datatype == wsme.types.text:
            return datatype(el.text if el.text else u(''))
        if datatype == bool:
            return el.text.lower() != 'false'
        if datatype is None:
            return el.text
        if datatype is wsme.types.bytes:
            return el.text.encode('ascii')
        return datatype(el.text)
Ejemplo n.º 2
0
def loadxml(el, datatype):
    print(el, datatype, len(el))
    if el.get('nil') == 'true':
        return None
    if isinstance(datatype, list):
        return [loadxml(item, datatype[0]) for item in el.findall('item')]
    elif isarray(datatype):
        return [
            loadxml(item, datatype.item_type) for item in el.findall('item')
        ]
    elif isinstance(datatype, dict):
        key_type, value_type = list(datatype.items())[0]
        return dict(((loadxml(item.find('key'),
                              key_type), loadxml(item.find('value'),
                                                 value_type))
                     for item in el.findall('item')))
    elif isdict(datatype):
        return dict(((loadxml(item.find('key'), datatype.key_type),
                      loadxml(item.find('value'), datatype.value_type))
                     for item in el.findall('item')))
    elif isdict(datatype):
        return dict(((loadxml(item.find('key'), datatype.key_type),
                      loadxml(item.find('value'), datatype.value_type))
                     for item in el.findall('item')))
    elif len(el):
        d = {}
        for attr in datatype._wsme_attributes:
            name = attr.name
            child = el.find(name)
            print(name, attr, child)
            if child is not None:
                d[name] = loadxml(child, attr.datatype)
        print(d)
        return d
    else:
        if datatype == wsme.types.binary:
            return base64.decodestring(el.text.encode('ascii'))
        if isusertype(datatype):
            datatype = datatype.basetype
        if datatype == datetime.date:
            return parse_isodate(el.text)
        if datatype == datetime.time:
            return parse_isotime(el.text)
        if datatype == datetime.datetime:
            return parse_isodatetime(el.text)
        if datatype == wsme.types.text:
            return datatype(el.text if el.text else u(''))
        if datatype == bool:
            return el.text.lower() != 'false'
        if datatype is None:
            return el.text
        if datatype is wsme.types.bytes:
            return el.text.encode('ascii')
        return datatype(el.text)
Ejemplo n.º 3
0
 def test_parse_isodate(self):
     good_dates = [
         ('2008-02-01', datetime.date(2008, 2, 1)),
         ('2009-01-04', datetime.date(2009, 1, 4)),
     ]
     ill_formatted_dates = ['24-12-2004']
     out_of_range_dates = [
         '0000-00-00',
         '2012-02-30',
     ]
     for s, d in good_dates:
         assert utils.parse_isodate(s) == d
     for s in ill_formatted_dates + out_of_range_dates:
         self.assertRaises(ValueError, utils.parse_isodate, s)
Ejemplo n.º 4
0
 def test_parse_isodate(self):
     good_dates = [
         ('2008-02-01', datetime.date(2008, 2, 1)),
         ('2009-01-04', datetime.date(2009, 1, 4)),
     ]
     ill_formatted_dates = [
         '24-12-2004'
     ]
     out_of_range_dates = [
         '0000-00-00',
         '2012-02-30',
     ]
     for s, d in good_dates:
         assert utils.parse_isodate(s) == d
     for s in ill_formatted_dates + out_of_range_dates:
         self.assertRaises(ValueError, utils.parse_isodate, s)
Ejemplo n.º 5
0
def decode_result(value, datatype):
    if value is None:
        return None
    if datatype == wsme.types.binary:
        value = base64.decodestring(value.encode('ascii'))
        return value
    if isusertype(datatype):
        datatype = datatype.basetype
    if isinstance(datatype, list):
        value = [decode_result(item, datatype[0]) for item in value]
    elif isarray(datatype):
        value = [decode_result(item, datatype.item_type) for item in value]
    elif isinstance(datatype, dict):
        key_type, value_type = list(datatype.items())[0]
        value = dict((
            (decode_result(key, key_type),
                decode_result(value, value_type))
            for key, value in value.items()
        ))
    elif isdict(datatype):
        key_type, value_type = datatype.key_type, datatype.value_type
        value = dict((
            (decode_result(key, key_type),
                decode_result(value, value_type))
            for key, value in value.items()
        ))
    elif datatype == datetime.time:
        value = parse_isotime(value)
    elif datatype == datetime.date:
        value = parse_isodate(value)
    elif datatype == datetime.datetime:
        value = parse_isodatetime(value)
    elif hasattr(datatype, '_wsme_attributes'):
        for attr in datatype._wsme_attributes:
            if attr.key not in value:
                continue
            value[attr.key] = decode_result(value[attr.key], attr.datatype)
    elif datatype == decimal.Decimal:
        value = decimal.Decimal(value)
    elif datatype == wsme.types.bytes:
        value = value.encode('ascii')
    elif datatype is not None and type(value) != datatype:
        value = datatype(value)
    return value
Ejemplo n.º 6
0
def prepare_result(value, datatype):
    print(value, datatype)
    if value is None:
        return None
    if datatype == wsme.types.binary:
        return base64.decodestring(value.encode("ascii"))
    if isusertype(datatype):
        datatype = datatype.basetype
    if isinstance(datatype, list):
        return [prepare_result(item, datatype[0]) for item in value]
    if isarray(datatype):
        return [prepare_result(item, datatype.item_type) for item in value]
    if isinstance(datatype, dict):
        return dict(
            (
                (prepare_result(item[0], list(datatype.keys())[0]), prepare_result(item[1], list(datatype.values())[0]))
                for item in value.items()
            )
        )
    if isdict(datatype):
        return dict(
            (
                (prepare_result(item[0], datatype.key_type), prepare_result(item[1], datatype.value_type))
                for item in value.items()
            )
        )
    if datatype == datetime.date:
        return parse_isodate(value)
    if datatype == datetime.time:
        return parse_isotime(value)
    if datatype == datetime.datetime:
        return parse_isodatetime(value)
    if hasattr(datatype, "_wsme_attributes"):
        for attr in datatype._wsme_attributes:
            if attr.key not in value:
                continue
            value[attr.key] = prepare_result(value[attr.key], attr.datatype)
        return value
    if datatype == wsme.types.bytes:
        return value.encode("ascii")
    if type(value) != datatype:
        print(type(value), datatype)
        return datatype(value)
    return value
Ejemplo n.º 7
0
def decode_result(value, datatype):
    if value is None:
        return None
    if datatype == wsme.types.binary:
        value = base64.decodestring(value.encode('ascii'))
        return value
    if isusertype(datatype):
        datatype = datatype.basetype
    if isinstance(datatype, list):
        value = [decode_result(item, datatype[0]) for item in value]
    elif isarray(datatype):
        value = [decode_result(item, datatype.item_type) for item in value]
    elif isinstance(datatype, dict):
        key_type, value_type = list(datatype.items())[0]
        value = dict((
            (decode_result(key, key_type),
                decode_result(value, value_type))
            for key, value in value.items()
        ))
    elif isdict(datatype):
        key_type, value_type = datatype.key_type, datatype.value_type
        value = dict((
            (decode_result(key, key_type),
                decode_result(value, value_type))
            for key, value in value.items()
        ))
    elif datatype == datetime.time:
        value = parse_isotime(value)
    elif datatype == datetime.date:
        value = parse_isodate(value)
    elif datatype == datetime.datetime:
        value = parse_isodatetime(value)
    elif hasattr(datatype, '_wsme_attributes'):
        for attr in datatype._wsme_attributes:
            if attr.key not in value:
                continue
            value[attr.key] = decode_result(value[attr.key], attr.datatype)
    elif datatype == decimal.Decimal:
        value = decimal.Decimal(value)
    elif datatype == wsme.types.bytes:
        value = value.encode('ascii')
    elif datatype is not None and type(value) != datatype:
        value = datatype(value)
    return value
Ejemplo n.º 8
0
def prepare_result(value, datatype):
    print(value, datatype)
    if value is None:
        return None
    if datatype == wsme.types.binary:
        return base64.decodestring(value.encode('ascii'))
    if isusertype(datatype):
        datatype = datatype.basetype
    if isinstance(datatype, list):
        return [prepare_result(item, datatype[0]) for item in value]
    if isarray(datatype):
        return [prepare_result(item, datatype.item_type) for item in value]
    if isinstance(datatype, dict):
        return dict((
            (prepare_result(item[0], list(datatype.keys())[0]),
                prepare_result(item[1], list(datatype.values())[0]))
            for item in value.items()
        ))
    if isdict(datatype):
        return dict((
            (prepare_result(item[0], datatype.key_type),
                prepare_result(item[1], datatype.value_type))
            for item in value.items()
        ))
    if datatype == datetime.date:
        return parse_isodate(value)
    if datatype == datetime.time:
        return parse_isotime(value)
    if datatype == datetime.datetime:
        return parse_isodatetime(value)
    if hasattr(datatype, '_wsme_attributes'):
        for attr in datatype._wsme_attributes:
            if attr.key not in value:
                continue
            value[attr.key] = prepare_result(value[attr.key], attr.datatype)
        return value
    if datatype == wsme.types.bytes:
        return value.encode('ascii')
    if type(value) != datatype:
        print(type(value), datatype)
        return datatype(value)
    return value
Ejemplo n.º 9
0
def date_fromsoap(datatype, el, ns):
    if el.get(nil_qn) == 'true':
        return None
    if el.get(type_qn) not in (None, 'xs:date'):
        raise exc.InvalidInput(el.tag, ET.tostring(el))
    return parse_isodate(el.text)
Ejemplo n.º 10
0
def date_from_param(datatype, value):
    return parse_isodate(value) if value else None
Ejemplo n.º 11
0
def date_fromsoap(datatype, el, ns):
    if el.get(nil_qn) == 'true':
        return None
    if el.get(type_qn) not in (None, 'xs:date'):
        raise exc.InvalidInput(el.tag, ET.tostring(el))
    return parse_isodate(el.text)
Ejemplo n.º 12
0
def date_from_param(datatype, value):
    return parse_isodate(value) if value else None
Ejemplo n.º 13
0
def date_fromjson(datatype, value):
    if value is None or value == '':
        return None
    return parse_isodate(value)
Ejemplo n.º 14
0
def date_fromjson(datatype, value):
    if value is None or value == '':
        return None
    return parse_isodate(value)