File: spore.py

package info (click to toggle)
python-wsme 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 956 kB
  • ctags: 1,831
  • sloc: python: 8,452; makefile: 138
file content (64 lines) | stat: -rw-r--r-- 1,783 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from wsme import types

try:
    import simplejson as json
except ImportError:
    import json  # noqa


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)