5 # Code generator for python ctypes bindings for VLC
6 # Copyright (C) 2009 the VideoLAN team
9 # Authors: Olivier Aubert <olivier.aubert at liris.cnrs.fr>
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 """This module parses VLC public API include files and generates
27 corresponding python/ctypes code. Moreover, it generates class
28 wrappers for most methods.
37 # DefaultDict from ASPN python cookbook
39 class DefaultDict(dict):
40 """Dictionary with a default value for unknown keys."""
41 def __init__(self, default=None, **items):
42 dict.__init__(self, **items)
43 self.default = default
45 def __getitem__(self, key):
49 ## Need copy in case self.default is something like []
50 return self.setdefault(key, copy.deepcopy(self.default))
53 return DefaultDict(self.default, **self)
55 # Methods not decorated/not referenced
57 "libvlc_exception_raise",
58 "libvlc_exception_raised",
59 "libvlc_exception_get_message",
60 "libvlc_get_vlc_instance",
62 "libvlc_media_add_option_flag",
63 "libvlc_media_list_view_index_of_item",
64 "libvlc_media_list_view_insert_at_index",
65 "libvlc_media_list_view_remove_at_index",
66 "libvlc_media_list_view_add_item",
68 # In svn but not in current 1.0.0
69 'libvlc_video_set_deinterlace',
70 'libvlc_video_get_marquee_option_as_int',
71 'libvlc_video_get_marquee_option_as_string',
72 'libvlc_video_set_marquee_option_as_int',
73 'libvlc_video_set_marquee_option_as_string',
74 'libvlc_vlm_get_event_manager',
76 'mediacontrol_PlaylistSeq__free',
79 "libvlc_event_detach",
80 "libvlc_event_attach",
84 api_re=re.compile('VLC_PUBLIC_API\s+(\S+\s+.+?)\s*\(\s*(.+?)\s*\)')
85 param_re=re.compile('\s*(const\s*|unsigned\s*|struct\s*)?(\S+\s*\**)\s+(.+)')
86 paramlist_re=re.compile('\s*,\s*')
87 comment_re=re.compile('\\param\s+(\S+)')
88 python_param_re=re.compile('(@param\s+\S+)(.+)')
89 forward_re=re.compile('.+\(\s*(.+?)\s*\)(\s*\S+)')
91 # Definition of parameter passing mode for types. This should not be
92 # hardcoded this way, but works alright ATM.
93 parameter_passing=DefaultDict(default=1)
94 parameter_passing['libvlc_exception_t*']=3
96 # C-type to ctypes/python type conversion
98 'libvlc_exception_t*': 'ctypes.POINTER(VLCException)',
100 'libvlc_media_player_t*': 'MediaPlayer',
101 'libvlc_instance_t*': 'Instance',
102 'libvlc_media_t*': 'Media',
103 'libvlc_log_t*': 'Log',
104 'libvlc_log_iterator_t*': 'LogIterator',
105 'libvlc_log_message_t*': 'LogMessage',
106 'libvlc_event_type_t': 'EventType',
107 'libvlc_event_manager_t*': 'EventManager',
108 'libvlc_media_discoverer_t*': 'MediaDiscoverer',
109 'libvlc_media_library_t*': 'MediaLibrary',
110 'libvlc_media_list_t*': 'MediaList',
111 'libvlc_media_list_player_t*': 'MediaListPlayer',
112 'libvlc_media_list_view_t*': 'MediaListView',
113 'libvlc_track_description_t*': 'TrackDescription',
114 'libvlc_audio_output_t*': 'AudioOutput',
116 'mediacontrol_Instance*': 'MediaControl',
117 'mediacontrol_Exception*': 'MediaControlException',
118 'mediacontrol_RGBPicture*': 'RGBPicture',
119 'mediacontrol_PlaylistSeq*': 'MediaControlPlaylistSeq',
120 'mediacontrol_Position*': 'MediaControlPosition',
121 'mediacontrol_StreamInformation*': 'MediaControlStreamInformation',
122 'mediacontrol_PositionOrigin': 'ctypes.c_uint',
123 'mediacontrol_PositionKey': 'ctypes.c_uint',
124 'WINDOWHANDLE': 'ctypes.c_ulong',
126 'short': 'ctypes.c_short',
127 'char*': 'ctypes.c_char_p',
128 'char**': 'ListPOINTER(ctypes.c_char_p)',
129 'uint32_t': 'ctypes.c_uint',
130 'float': 'ctypes.c_float',
131 'unsigned': 'ctypes.c_uint',
133 'void*': 'ctypes.c_void_p',
134 'int': 'ctypes.c_int',
135 '...': 'FIXMEva_list',
136 'libvlc_callback_t': 'FIXMEcallback',
137 'libvlc_time_t': 'ctypes.c_longlong',
138 'libvlc_video_marquee_int_option_t': 'ctypes.c_int',
139 'libvlc_video_marquee_string_option_t': 'ctypes.c_char_p',
140 # FIXME: enums -> to be processed
141 'libvlc_media_option_t': 'ctypes.c_uint',
142 'libvlc_meta_t': 'ctypes.c_uint',
143 'libvlc_state_t': 'State',
146 # Defined python classes, i.e. classes for which we want to generate
147 # class wrappers around libvlc functions
166 #'MediaControlPosition',
167 #'MediaControlStreamInformation',
170 # Definition of prefixes that we can strip from method names when
171 # wrapping them into class methods
172 prefixes=dict( (v, k[:-2]) for (k, v) in typ2class.iteritems() if v in defined_classes )
173 prefixes['MediaControl']='mediacontrol_'
176 """Parse a C parameter expression.
178 It is used to parse both the type/name for methods, and type/name
179 for their parameters.
181 It returns a tuple (type, name).
184 s=s.replace('const', '')
185 if 'VLC_FORWARD' in s:
186 m=forward_re.match(s)
187 s=m.group(1)+m.group(2)
190 const, typ, name=m.groups()
191 while name.startswith('*'):
195 # K&R definition: const char * const*
197 typ=typ.replace(' ', '')
200 # K&R definition: only type
201 return s.replace(' ', ''), ''
203 def generate_header(classes=None):
204 """Generate header code.
206 f=open('header.py', 'r')
208 if 'build_date' in l:
209 print 'build_date="%s"' % time.ctime()
214 def parse_include(name):
215 """Parse include file.
217 This generates a tuple for each function:
218 (return_type, method_name, parameter_list, comment)
219 with parameter_list being a list of tuples (parameter_type, parameter_name).
225 # Note: lstrip() should not be necessary, but there is 1 badly
226 # formatted comment in vlc1.0.0 includes
227 if l.lstrip().startswith('/**'):
230 elif l.startswith(' * '):
231 comment = comment + l[3:]
237 accumulator=" ".join( (accumulator, l) )
242 elif l.startswith('VLC_PUBLIC_API') and not l.endswith(');'):
243 # Multiline definition. Accumulate until end of definition
249 (ret, param)=m.groups()
251 rtype, method=parse_param(ret)
254 for p in paramlist_re.split(param):
255 params.append( parse_param(p) )
257 if len(params) == 1 and params[0][0] == 'void':
258 # Empty parameter list
261 if list(p for p in params if not p[1]):
262 # Empty parameter names. Have to poke into comment.
263 names=comment_re.findall(comment)
264 if len(names) < len(params):
265 # Bad description: all parameters are not specified.
266 # Generate default parameter names
267 badnames=[ "param%d" % i for i in xrange(len(params)) ]
268 # Put in the existing ones
269 for (i, p) in enumerate(names):
272 print "### Error ###"
273 print "### Cannot get parameter names from comment for %s: %s" % (method, comment.replace("\n", ' '))
274 # Note: this was previously
275 # raise Exception("Cannot get parameter names from comment for %s: %s" % (method, comment))
276 # but it prevented code generation for a minor detail (some bad descriptions).
277 params=[ (p[0], names[i]) for (i, p) in enumerate(params) ]
279 for typ, name in params:
280 if not typ in typ2class:
281 raise Exception("No conversion for %s (from %s:%s)" % (typ, method, name))
283 # Transform Doxygen syntax into epydoc syntax
284 comment=comment.replace('\\param', '@param').replace('\\return', '@return')
287 print '********************'
290 print "%s (%s)" % (method, rtype)
291 for typ, name in params:
292 print " %s (%s)" % (name, typ)
293 print '********************'
299 def output_ctypes(rtype, method, params, comment):
300 """Output ctypes decorator for the given method.
302 if method in blacklist:
307 print "prototype=ctypes.CFUNCTYPE(%s, %s)" % (typ2class.get(rtype, 'FIXME_%s' % rtype),
308 ",".join( typ2class[p[0]] for p in params ))
310 print "prototype=ctypes.CFUNCTYPE(%s)" % typ2class.get(rtype, 'FIXME_%s' % rtype)
314 flags='paramflags= tuple()'
315 elif len(params) == 1:
316 flags="paramflags=( (%d, ), )" % parameter_passing[params[0][0]]
318 flags="paramflags=%s" % ",".join( '(%d,)' % parameter_passing[p[0]] for p in params )
320 print '%s = prototype( ("%s", dll), paramflags )' % (method, method)
322 # A VLCException is present. Process it.
323 print "%s.errcheck = check_vlc_exception" % method
324 print '%s.__doc__ = """%s"""' % (method, comment)
327 def parse_override(name):
328 """Parse override definitions file.
330 It is possible to override methods definitions in classes.
338 m=re.match('class (\S+):', l)
341 if current is not None:
342 res[current]="\n".join(data)
347 res[current]="\n".join(data)
351 def fix_python_comment(c):
352 """Fix comment by removing first and last parameters (self and exception)
355 body=itertools.takewhile(lambda l: not '@param' in l, data)
356 param=[ python_param_re.sub('\\1:\\2', l) for l in itertools.ifilter(lambda l: '@param' in l, data) ]
357 ret=[ l.replace('@return', '@return:') for l in itertools.ifilter(lambda l: '@return' in l, data) ]
361 elif len(param) == 1:
364 return "\n".join(itertools.chain(body, param, ret))
366 def generate_wrappers(methods):
367 """Generate class wrappers for all appropriate methods.
369 @return: the set of wrapped method names
372 # Sort methods against the element they apply to.
373 elements=sorted( ( (typ2class.get(params[0][0]), rt, met, params, c)
374 for (rt, met, params, c) in methods
375 if params and typ2class.get(params[0][0], '_') in defined_classes
377 key=operator.itemgetter(0))
379 overrides=parse_override('override.py')
381 for classname, el in itertools.groupby(elements, key=operator.itemgetter(0)):
383 class %(name)s(object):
384 def __init__(self, pointer=None):
385 '''Internal method used for instanciating wrappers from ctypes.
388 raise Exception("Internal method. You should instanciate objects through other class methods (probably named 'new' or ending with 'new')")
389 self._as_parameter_=ctypes.c_void_p(pointer)
393 '''(INTERNAL) ctypes parameter conversion method.
395 return arg._as_parameter_
396 """ % {'name': classname}
398 if classname in overrides:
399 print overrides[classname]
401 prefix=prefixes.get(classname, '')
403 for cl, rtype, method, params, comment in el:
404 if method in blacklist:
407 name=method.replace(prefix, '').replace('libvlc_', '')
410 params[0]=(params[0][0], 'self')
411 if params and params[-1][0] in ('libvlc_exception_t*', 'mediacontrol_Exception*'):
412 args=", ".join( p[1] for p in params[:-1] )
414 args=", ".join( p[1] for p in params )
416 print " def %s(%s):" % (name, args)
417 print ' """%s\n"""' % fix_python_comment(comment)
418 if params and params[-1][0] == 'libvlc_exception_t*':
420 print " e=VLCException()"
421 print " return %s(%s, e)" % (method, args)
422 elif params and params[-1][0] == 'mediacontrol_Exception*':
424 print " e=MediaControlException()"
425 print " return %s(%s, e)" % (method, args)
427 print " return %s(%s)" % (method, args)
431 if __name__ == '__main__':
433 for name in sys.argv[1:]:
434 methods.extend(list(parse_include(name)))
439 wrapped=generate_wrappers(methods)
443 all=set( t[1] for t in methods )
444 not_wrapped=all.difference(wrapped)
445 print "# Not wrapped methods:"
446 for m in not_wrapped: