-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgenerate_phpdoc.py
331 lines (262 loc) · 9.91 KB
/
generate_phpdoc.py
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
from pyvips import Image, Operation, GValue, Error, \
ffi, values_for_enum, vips_lib, gobject_lib, \
type_map, type_name, type_from_name, nickname_find
# This file generates the phpdoc comments for the magic methods and properties.
# It's in Python, since we use the whole of FFI, not just the
# small bit exposed by php-vips-ext.
# Regenerate docs with something like:
#
# cd src
# python ../examples/generate_phpdoc.py
# this needs pyvips
#
# pip install --user pyvips
# map a Python gtype to PHP argument type names
gtype_to_php_arg = {
GValue.gbool_type: 'bool',
GValue.gint_type: 'integer',
GValue.gdouble_type: 'float',
GValue.gstr_type: 'string',
GValue.refstr_type: 'string',
GValue.genum_type: 'string',
GValue.gflags_type: 'integer',
GValue.gobject_type: 'string',
GValue.image_type: 'Image',
GValue.array_int_type: 'integer[]|integer',
GValue.array_double_type: 'float[]|float',
GValue.array_image_type: 'Image[]|Image',
GValue.blob_type: 'string'
}
# php result type names are different, annoyingly, and very restricted
gtype_to_php_result = {
GValue.gbool_type: 'bool',
GValue.gint_type: 'integer',
GValue.gdouble_type: 'float',
GValue.gstr_type: 'string',
GValue.refstr_type: 'string',
GValue.genum_type: 'string',
GValue.gflags_type: 'integer',
GValue.gobject_type: 'string',
GValue.image_type: 'Image',
GValue.array_int_type: 'array',
GValue.array_double_type: 'array',
GValue.array_image_type: 'array',
GValue.blob_type: 'string'
}
# values for VipsArgumentFlags
_REQUIRED = 1
_INPUT = 16
_OUTPUT = 32
_DEPRECATED = 64
_MODIFY = 128
# for VipsOperationFlags
_OPERATION_DEPRECATED = 8
def gtype_to_php(gtype, result=False):
"""Map a gtype to PHP type name we use to represent it.
"""
fundamental = gobject_lib.g_type_fundamental(gtype)
gtype_map = gtype_to_php_result if result else gtype_to_php_arg
if gtype in gtype_map:
return gtype_map[gtype]
if fundamental in gtype_map:
return gtype_map[fundamental]
return '<unknown type>'
def remove_prefix(enum_str):
prefix = 'Vips'
if enum_str.startswith(prefix):
return enum_str[len(prefix):]
return enum_str
def generate_operation(operation_name):
op = Operation.new_from_name(operation_name)
# we are only interested in non-deprecated args
args = [[name, flags] for name, flags in op.get_args()
if not flags & _DEPRECATED]
# find the first required input image arg, if any ... that will be self
member_x = None
for name, flags in args:
if ((flags & _INPUT) != 0 and
(flags & _REQUIRED) != 0 and
op.get_typeof(name) == GValue.image_type):
member_x = name
break
required_input = [name for name, flags in args
if (flags & _INPUT) != 0 and
(flags & _REQUIRED) != 0 and
name != member_x]
required_output = [name for name, flags in args
if ((flags & _OUTPUT) != 0 and
(flags & _REQUIRED) != 0) or
((flags & _INPUT) != 0 and
(flags & _REQUIRED) != 0 and
(flags & _MODIFY) != 0)]
result = ' * @method '
if member_x is None:
result += 'static '
if len(required_output) == 0:
result += 'void '
elif len(required_output) == 1:
result += '{0} '.format(gtype_to_php(op.get_typeof(required_output[0]), True))
else:
# we generate a Returns: block for this case, see below
result += 'array '
result += '{0}('.format(operation_name)
for name in required_input:
gtype = op.get_typeof(name)
result += '{0} ${1}, '.format(gtype_to_php(gtype), name)
result += 'array $options = []) '
description = op.get_description()
result += description[0].upper() + description[1:] + '.\n'
# find any Enums we've referenced and output @see lines for them
for name in required_output + required_input:
gtype = op.get_typeof(name)
fundamental = gobject_lib.g_type_fundamental(gtype)
if fundamental != GValue.genum_type:
continue
result += ' * @see {0} for possible values for ${1}\n'.format(remove_prefix(type_name(gtype)), name)
if len(required_output) > 1:
result += ' * Return array with: [\n'
for name in required_output:
gtype = op.get_typeof(name)
blurb = op.get_blurb(name)
result += ' * \'{0}\' => @type {1} {2}\n'.format(name, gtype_to_php(gtype),
blurb[0].upper() + blurb[1:])
result += ' * ];\n'
result += ' * @throws Exception\n'
return result
preamble = """<?php
/**
* This file was generated automatically. Do not edit!
*
* PHP version 7
*
* LICENSE:
*
* Copyright (c) 2016 John Cupitt
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @category Images
* @package Jcupitt\\Vips
* @author John Cupitt <jcupitt@gmail.com>
* @copyright 2016 John Cupitt
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/jcupitt/php-vips
*/
"""
class_header = """ * @category Images
* @package Jcupitt\\Vips
* @author John Cupitt <jcupitt@gmail.com>
* @copyright 2016 John Cupitt
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/jcupitt/php-vips
"""
def generate_auto_doc(filename):
all_nicknames = []
def add_nickname(gtype, a, b):
nickname = nickname_find(gtype)
try:
# can fail for abstract types
op = Operation.new_from_name(nickname)
# we are only interested in non-deprecated operations
if (op.get_flags() & _OPERATION_DEPRECATED) == 0:
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))
all_nicknames.sort()
# these have hand-written methods, don't autodoc them
no_generate = [
'bandjoin',
'bandrank',
'ifthenelse',
'add',
'subtract',
'multiply',
'divide',
'remainder'
]
all_nicknames = [x for x in all_nicknames if x not in no_generate]
print('Generating {0} ...'.format(filename))
with open(filename, 'w') as f:
f.write(preamble)
f.write('\n')
f.write('namespace Jcupitt\\Vips;\n')
f.write('\n')
f.write('/**\n')
f.write(' * Autodocs for the Image class.\n')
f.write(class_header)
f.write(' *\n')
for nickname in all_nicknames:
f.write(generate_operation(nickname))
f.write(' *\n')
# all magic properties
tmp_file = Image.new_temp_file('%s.v')
all_properties = tmp_file.get_fields()
for name in all_properties:
php_name = name.replace('-', '_')
gtype = tmp_file.get_typeof(name)
fundamental = gobject_lib.g_type_fundamental(gtype)
f.write(' * @property {0} ${1} {2}\n'.format(gtype_to_php(gtype), php_name, tmp_file.get_blurb(name)))
if fundamental == GValue.genum_type:
f.write(' * @see {0} for possible values\n'.format(remove_prefix(type_name(gtype))))
f.write(' */\n')
f.write('abstract class ImageAutodoc\n')
f.write('{\n')
f.write('}\n')
def generate_enums():
# otherwise we're missing some enums
vips_lib.vips_token_get_type()
vips_lib.vips_saveable_get_type()
vips_lib.vips_image_type_get_type()
all_enums = []
def add_enum(gtype, a, b):
nickname = type_name(gtype)
all_enums.append(nickname)
type_map(gtype, add_enum)
return ffi.NULL
type_map(type_from_name('GEnum'), add_enum)
for name in all_enums:
gtype = type_from_name(name)
php_name = remove_prefix(name)
print('Generating {0}.php ...'.format(php_name))
with open('{0}.php'.format(php_name), 'w') as f:
f.write(preamble)
f.write('\n')
f.write('namespace Jcupitt\\Vips;\n')
f.write('\n')
f.write('/**\n')
f.write(' * The {0} enum.\n'.format(php_name))
f.write(class_header)
f.write(' */\n')
f.write('abstract class {0}\n'.format(php_name))
f.write('{\n')
for value in values_for_enum(gtype):
php_name = value.replace('-', '_').upper()
f.write(' const {0} = \'{1}\';\n'.format(php_name, value))
f.write('}\n')
generate_auto_doc('ImageAutodoc.php')
generate_enums()