summaryrefslogtreecommitdiffstats
path: root/docs/update-props.py
blob: 7f2c2944d24fe6ad488412b31d5f3c31665dc2b5 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env

import sys
from gi.repository import Uca, GLib, GObject


def get_default_optional(prop):
    if hasattr(prop, 'default_value'):
        return '    | *Default:* {}'.format(prop.default_value)
    return ''


def get_range_optional(prop):
    if hasattr(prop, 'minimum') and hasattr(prop, 'maximum'):
        return '    | *Range:* [{}, {}]'.format(prop.minimum, prop.maximum)
    return ''


def get_type_description(value_type):
    mapping = {
        GObject.TYPE_BOOLEAN: 'bool',
        GObject.TYPE_CHAR: 'char',
        GObject.TYPE_UCHAR: 'unsigned char',
        GObject.TYPE_FLOAT: 'float',
        GObject.TYPE_INT: 'int',
        GObject.TYPE_UINT: 'unsigned int',
        GObject.TYPE_LONG: 'long',
        GObject.TYPE_DOUBLE: 'double',
        GObject.TYPE_STRING: 'string',
        GObject.TYPE_ENUM: 'enum',
    }

    return mapping.get(value_type, None)


def get_prop_string(prop):
    template = """
{type} **{name}**
    {blurb}

{optionals}"""

    optionals = '\n'.join(opt for opt in (get_default_optional(prop), get_range_optional(prop)) if opt)

    return template.format(name=prop.name, blurb=prop.blurb,
                           type=get_type_description(prop.value_type),
                           optionals=optionals)

def output(camera, name):
    template = """
{name}
{name_underline}
{props}"""

    name_underline = '='*len(name)

    stream = open('{}.rst'.format(name), 'w')
    props = '\n'.join((get_prop_string(prop) for prop in camera.props))
    stream.write(template.format(name=name, name_underline=name_underline,
                                 props=props))


if __name__ == '__main__':
    pm = Uca.PluginManager()

    if len(sys.argv) < 2:
        print("Usage: update-props.py NAME")
        sys.exit(0)

    name = sys.argv[1]

    try:
        output(pm.get_camerav(name, []), name)
    except GLib.GError as e:
        print("Could not query {}: {}".format(name, e.message))