Пример #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)
Пример #2
0
def getdesc(root, host_url=''):
    methods = {}

    for path, funcdef in root.getapi():
        method = funcdef.extra_options.get('method', None)
        name = '_'.join(path)
        if method is not None:
            path = path[:-1]
        else:
            method = 'GET'
            for argdef in funcdef.arguments:
                if types.iscomplex(argdef.datatype) \
                        or types.isarray(argdef.datatype) \
                        or types.isdict(argdef.datatype):
                    method = 'POST'
                    break

        required_params = []
        optional_params = []
        for argdef in funcdef.arguments:
            if method == 'GET' and argdef.mandatory:
                required_params.append(argdef.name)
            else:
                optional_params.append(argdef.name)

        methods[name] = {
            'method': method,
            'path': '/'.join(path)
        }
        if required_params:
            methods[name]['required_params'] = required_params
        if optional_params:
            methods[name]['optional_params'] = optional_params
        if funcdef.doc:
            methods[name]['documentation'] = funcdef.doc

    formats = []
    for p in root.protocols:
        if p.name == 'restxml':
            formats.append('xml')
        if p.name == 'restjson':
            formats.append('json')

    api = {
        'base_url': host_url + root._webpath,
        'version': '0.1',
        'name': getattr(root, 'name', 'name'),
        'authority': '',
        'formats': [
            'json',
            'xml'
        ],
        'methods': methods
    }

    return json.dumps(api, indent=4)
Пример #3
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)
Пример #4
0
def getdesc(root, host_url=''):
    methods = {}

    for path, funcdef in root.getapi():
        method = funcdef.extra_options.get('method', None)
        name = '_'.join(path)
        if method is not None:
            path = path[:-1]
        else:
            method = 'GET'
            for argdef in funcdef.arguments:
                if types.iscomplex(argdef.datatype) \
                        or types.isarray(argdef.datatype) \
                        or types.isdict(argdef.datatype):
                    method = 'POST'
                    break

        required_params = []
        optional_params = []
        for argdef in funcdef.arguments:
            if method == 'GET' and argdef.mandatory:
                required_params.append(argdef.name)
            else:
                optional_params.append(argdef.name)

        methods[name] = {'method': method, 'path': '/'.join(path)}
        if required_params:
            methods[name]['required_params'] = required_params
        if optional_params:
            methods[name]['optional_params'] = optional_params
        if funcdef.doc:
            methods[name]['documentation'] = funcdef.doc

    formats = []
    for p in root.protocols:
        if p.name == 'restxml':
            formats.append('xml')
        if p.name == 'restjson':
            formats.append('json')

    api = {
        'base_url': host_url + root._webpath,
        'version': '0.1',
        'name': getattr(root, 'name', 'name'),
        'authority': '',
        'formats': ['json', 'xml'],
        'methods': methods
    }

    return json.dumps(api, indent=4)
Пример #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
Пример #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
Пример #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
Пример #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
Пример #9
0
def test_generate():
    class A(SABase):
        __tablename__ = 'a'
        id = Column(Integer, primary_key=True)
        name = Column(String(50))

        _b_id = Column(ForeignKey('b.id'))

        b = relation('B')

    class B(SABase):
        __tablename__ = 'b'
        id = Column(Integer, primary_key=True)
        name = Column(String(50))

        alist = relation(A)

    newtypes = wsmeext.sqlalchemy.types.generate_types(A, B)

    assert newtypes['A'].id.datatype is int
    assert newtypes['A'].b.datatype is newtypes['B']
    assert newtypes['B'].id.datatype is int
    assert isarray(newtypes['B'].alist.datatype)
    assert newtypes['B'].alist.datatype.item_type is newtypes['A']