File: fixup-gir-for-doc.py

package info (click to toggle)
vips 8.17.3-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 52,228 kB
  • sloc: ansic: 169,684; cpp: 12,156; python: 4,887; sh: 733; perl: 40; makefile: 25; javascript: 6
file content (103 lines) | stat: -rwxr-xr-x 2,739 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3

import argparse
import xml.etree.ElementTree as ET
from pathlib import Path


def register_all_namespaces(filename):
    namespaces = dict([node for _, node in ET.iterparse(filename, events=['start-ns'])])
    for ns in namespaces:
        ET.register_namespace(ns, namespaces[ns])


def fixup_gir_for_doc(args):
    tree = ET.parse(args.in_path)
    root = tree.getroot()

    register_all_namespaces(args.in_path)
    namespace = {
        'goi': 'http://www.gtk.org/introspection/core/1.0'
    }

    namespace_node = root.find('goi:namespace', namespace)
    image_node = namespace_node.find('goi:class[@name="Image"]', namespace)
    blob_node = namespace_node.find('goi:record[@name="Blob"]', namespace)

    # A list of functions that needs to be converted to an Image constructor.
    # Note that all functions containing "load" are already converted.
    image_ctors = [
        'black',
        'eye',
        'fractsurf',
        'gaussmat',
        'gaussnoise',
        'grey',
        'identity',
        'logmat',
        'mask_butterworth',
        'mask_butterworth_band',
        'mask_butterworth_ring',
        'mask_fractal',
        'mask_gaussian',
        'mask_gaussian_band',
        'mask_gaussian_ring',
        'mask_ideal',
        'mask_ideal_band',
        'mask_ideal_ring',
        'perlin',
        'sdf',
        'sines',
        'system',
        'text',
        'thumbnail',
        'thumbnail_buffer',
        'thumbnail_source',
        'tonelut',
        'worley',
        'xyz',
        'zone',
    ]

    # Functions that take multiple images as input argument ... make them
    # part of the Image class.
    # Keep-in-sync with gen-function-list.py
    image_funcs = [
        'arrayjoin',
        'bandjoin',
        'bandrank',
        'composite',
        'sum',
        'switch',
    ]

    for node in namespace_node.findall('goi:function', namespace):
        name = node.get('name')
        if name == 'profile_load':
            namespace_node.remove(node)

            node.tag = 'constructor'
            blob_node.append(node)
        elif 'load' in name or name in image_ctors:
            namespace_node.remove(node)

            node.tag = 'constructor'
            image_node.append(node)
        elif name in image_funcs:
            namespace_node.remove(node)
            image_node.append(node)

    tree.write(args.out_path, encoding='utf-8', xml_declaration=True)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('in_path', help='input file', type=Path)
    parser.add_argument('out_path', help='output file', type=Path)

    args = parser.parse_args()
    fixup_gir_for_doc(args)


if __name__ == '__main__':
    main()