File: gen-operators.py

package info (click to toggle)
vips 8.18.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 53,240 kB
  • sloc: ansic: 172,611; cpp: 12,257; python: 5,077; sh: 773; perl: 40; makefile: 25; javascript: 6
file content (295 lines) | stat: -rwxr-xr-x 8,956 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3

# This file generates the member definitions and declarations for all vips
# operators.

# this needs pyvips
#   pip install --user pyvips

# rebuild with:
#   meson compile -Cbuild vips-operators-header
#   meson compile -Cbuild vips-operators-source

# Sample member declaration:
# VImage invert(VOption *options = nullptr) const;

# Sample member definition:
# VImage
# VImage::invert(VOption *options) const
# {
#   VImage out;
#
# 	call("invert", (options ? options : VImage::option())
# 				->set("in", *this)
# 				->set("out", &out));
#
# 	return out;
# }

import argparse

from pyvips import Introspect, Operation, GValue, Error, \
    ffi, gobject_lib, type_map, type_from_name, nickname_find, type_name

# turn a GType into a C++ type
gtype_to_cpp = {
    GValue.gbool_type: 'bool',
    GValue.gint_type: 'int',
    GValue.gdouble_type: 'double',
    GValue.gstr_type: 'const char *',
    GValue.refstr_type: 'char *',
    GValue.image_type: 'VImage',
    GValue.source_type: 'VSource',
    GValue.target_type: 'VTarget',
    GValue.guint64_type: 'guint64',
    type_from_name('VipsInterpolate'): 'VInterpolate',
    GValue.array_int_type: 'std::vector<int>',
    GValue.array_double_type: 'std::vector<double>',
    GValue.array_image_type: 'std::vector<VImage>',
    GValue.blob_type: 'VipsBlob *'
}

cplusplus_suffixes = ('*', '&')
cplusplus_keywords = ('case', 'switch')

# values for VipsArgumentFlags
_REQUIRED = 1
_INPUT = 16
_OUTPUT = 32
_DEPRECATED = 64
_MODIFY = 128

# for VipsOperationFlags
_OPERATION_NOCACHE = 4
_OPERATION_DEPRECATED = 8


def get_cpp_type(gtype):
    """Map a gtype to the C++ type name we use to represent it.
    """
    if gtype in gtype_to_cpp:
        return gtype_to_cpp[gtype]

    fundamental = gobject_lib.g_type_fundamental(gtype)

    # enum/flag params use the C name as their name
    if fundamental == GValue.genum_type or fundamental == GValue.gflags_type:
        return type_name(gtype)

    if fundamental in gtype_to_cpp:
        return gtype_to_cpp[fundamental]

    return '<unknown type>'


# swap any '-' for '_'
def cppize(name):
    return name.replace('-', '_')


def generate_operation(operation_name, declaration_only=False, indent=''):
    intro = Introspect.get(operation_name)

    required_output = [name for name in intro.required_output if
                       name != intro.member_x]

    # We are only interested in non-deprecated arguments
    optional_input = [name for name in intro.optional_input if
                      intro.details[name]['flags'] & _DEPRECATED == 0]

    # Drop "revalidate" flag from operations marked "nocache"
    optional_input = [name for name in optional_input if
                      name != 'revalidate' or (intro.flags & _OPERATION_NOCACHE) == 0]

    has_output = len(required_output) >= 1

    # Add a C++ style comment block with some additional markings (@param,
    # @return)
    if declaration_only:
        description = intro.description.capitalize()
        # hide library-specific implementation details
        description = (description
                       .replace('imagemagick7', 'imagemagick')
                       .replace(' (pdfium)', '')
                       .replace(' (poppler)', ''))
        result = f'\n{indent}/**'
        result += f'\n{indent} * {description}.'

        if len(optional_input) > 0:
            result += f'\n{indent} *'
            result += f'\n{indent} * **Optional parameters**'
            for name in optional_input:
                details = intro.details[name]
                result += f'\n{indent} *   - **{cppize(name)}** -- '
                result += f'{details["blurb"]}, '
                result += f'{get_cpp_type(details["type"])}.'
            result += f'\n{indent} *'

        for name in intro.method_args:
            details = intro.details[name]
            result += f'\n{indent} * @param {cppize(name)} {details["blurb"]}.'

        if has_output:
            # skip the first element
            for name in required_output[1:]:
                details = intro.details[name]
                result += f'\n{indent} * @param {cppize(name)} {details["blurb"]}.'

        result += f'\n{indent} * @param options Set of options.'

        if has_output:
            details = intro.details[required_output[0]]
            result += f'\n{indent} * @return {details["blurb"]}.'

        result += f'\n{indent} */\n'
        if intro.flags & _OPERATION_DEPRECATED:
            result += f'{indent}G_DEPRECATED\n'
    else:
        result = '\n'

    if intro.member_x is None and declaration_only:
        result += f'{indent}static '
    else:
        result += indent
    if has_output:
        # the first output arg will be used as the result
        cpp_type = get_cpp_type(intro.details[required_output[0]]['type'])
        result += f'{cpp_type}'
    else:
        cpp_type = 'void'
        result += cpp_type

    if declaration_only:
        result += '' if cpp_type.endswith(cplusplus_suffixes) else ' '
    else:
        result += '\nVImage::'

    cplusplus_operation = operation_name
    if operation_name in cplusplus_keywords:
        cplusplus_operation += '_image'

    result += f'{cplusplus_operation}('
    for name in intro.method_args:
        details = intro.details[name]
        gtype = details['type']
        cpp_type = get_cpp_type(gtype)
        spacing = '' if cpp_type.endswith(cplusplus_suffixes) else ' '
        result += f'{cpp_type}{spacing}{cppize(name)}, '

    # output params are passed by reference
    if has_output:
        # skip the first element
        for name in required_output[1:]:
            details = intro.details[name]
            gtype = details['type']
            cpp_type = get_cpp_type(gtype)
            spacing = '' if cpp_type.endswith(cplusplus_suffixes) else ' '
            result += f'{cpp_type}{spacing}*{cppize(name)}, '

    result += f'VOption *options{" = nullptr" if declaration_only else ""})'

    # if no 'this' available, it's a class method and they are all const
    if intro.member_x is not None:
        result += ' const'

    if declaration_only:
        result += ';'

        return result

    result += '\n{\n'

    if has_output:
        # the first output arg will be used as the result
        name = required_output[0]
        cpp_type = get_cpp_type(intro.details[name]['type'])
        spacing = '' if cpp_type.endswith(cplusplus_suffixes) else ' '
        result += f'\t{cpp_type}{spacing}{cppize(name)};\n\n'

    result += f'\tcall("{operation_name}", (options ? options : VImage::option())'
    if intro.member_x is not None:
        result += f'\n\t\t\t->set("{intro.member_x}", *this)'

    all_required = intro.method_args

    if has_output:
        # first element needs to be passed by reference
        arg = cppize(required_output[0])
        result += f'\n\t\t\t->set("{required_output[0]}", &{arg})'

        # append the remaining list
        all_required += required_output[1:]

    for name in all_required:
        arg = cppize(name)
        result += f'\n\t\t\t->set("{name}", {arg})'

    result += ');\n'

    if has_output:
        result += f'\n'
        result += f'\treturn {required_output[0]};\n'

    result += '}'

    return result


def generate_operators(declarations_only=False):
    all_nicknames = []

    # hidden CLI savers needs to be excluded from the API
    hidden_savers = [
        'avifsave_target',
        'magicksave_bmp',
        'magicksave_bmp_buffer',
        'pbmsave_target',
        'pfmsave_target',
        'pgmsave_target',
        'pnmsave_target',
    ]

    def add_nickname(gtype, a, b):
        nickname = nickname_find(gtype)
        try:
            # can fail for abstract types
            _ = Introspect.get(nickname)

            all_nicknames.append(nickname)
        except Error:
            pass

        type_map(gtype, add_nickname)

        return ffi.NULL

    type_map(type_from_name('VipsOperation'), add_nickname)

    # add 'missing' synonyms by hand
    all_nicknames.append('crop')

    # make list unique and sort
    all_nicknames = list(set(all_nicknames) - set(hidden_savers))
    all_nicknames.sort()

    indent = '\t' if declarations_only else ''

    print(f'''{indent}// {'headers' if declarations_only else 'bodies'} for vips operations
{indent}// this file is generated automatically, do not edit!
{indent}// clang-format off''')

    for nickname in all_nicknames:
        print(generate_operation(nickname, declarations_only, indent))


parser = argparse.ArgumentParser(description='C++ binding generator')
parser.add_argument('--gen', '-g',
                    default='cpp',
                    choices=['h', 'cpp'],
                    help='File to generate: h (headers) or cpp ' + \
                         '(implementations) (default: %(default)s)')

if __name__ == '__main__':
    args = parser.parse_args()

    generate_operators(args.gen == 'h')