summaryrefslogtreecommitdiff
path: root/generate/async_annotate.py
blob: f05c0674ff396035fd30fff5dae976b389662eca (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
#!/usr/bin/python2.4

import sys

try:
    from elementtree.ElementTree import fromstring, tostring, ElementTree, Element
except ImportError:
    print "You need to install ElementTree (http://effbot.org/zone/element-index.htm)"
    sys.exit(1)

from xml.dom.minidom import parseString
from telepathy.server import *

import sys

def strip (element):
    if element.text:
        element.text = element.text.strip()
    if element.tail:
        element.tail = element.tail.strip()
    for child in element:
        strip (child)

class AsyncAnnotation:
    def __init__(self, line):
        self.cls, self.interface, self.method = line.strip().split()

def annotate(root, annotations):
    assert root.tag == 'node'
    annotations = [a for a in annotations if root.get('name') == '/' + a.cls]

    for interface_elt in root:
        if interface_elt.tag != 'interface':
            continue
        for method_elt in interface_elt:
            if method_elt.tag != 'method':
                continue
            for a in annotations:
                if (interface_elt.get('name') == a.interface
                    and method_elt.get('name') == a.method):
                    a_elt = Element('annotation',
                                    name='org.freedesktop.DBus.GLib.Async',
                                    value='')
                    method_elt.insert(0, a_elt)

if __name__ == '__main__':
    annotations = [AsyncAnnotation(line) for line in file(sys.argv[1])]

    root = ElementTree(file=sys.argv[2]).getroot()
    annotate(root, annotations)

    # pretty print
    strip(root)
    xml = tostring(root)
    dom = parseString(xml)

    output = file(sys.argv[3], 'w')
    output.write(dom.toprettyxml('  ', '\n'))
    output.close()