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+)')
90 enum_re=re.compile('typedef\s+(enum)\s*(\S+\s*)?\{\s*(.+)\s*\}\s*(\S+);')
92 # Definition of parameter passing mode for types. This should not be
93 # hardcoded this way, but works alright ATM.
94 parameter_passing=DefaultDict(default=1)
95 parameter_passing['libvlc_exception_t*']=3
97 # C-type to ctypes/python type conversion.
98 # Note that enum types conversions are generated (cf convert_enum_names)
100 'libvlc_exception_t*': 'ctypes.POINTER(VLCException)',
102 'libvlc_media_player_t*': 'MediaPlayer',
103 'libvlc_instance_t*': 'Instance',
104 'libvlc_media_t*': 'Media',
105 'libvlc_log_t*': 'Log',
106 'libvlc_log_iterator_t*': 'LogIterator',
107 'libvlc_log_message_t*': 'LogMessage',
108 'libvlc_event_type_t': 'EventType',
109 'libvlc_event_manager_t*': 'EventManager',
110 'libvlc_media_discoverer_t*': 'MediaDiscoverer',
111 'libvlc_media_library_t*': 'MediaLibrary',
112 'libvlc_media_list_t*': 'MediaList',
113 'libvlc_media_list_player_t*': 'MediaListPlayer',
114 'libvlc_media_list_view_t*': 'MediaListView',
115 'libvlc_track_description_t*': 'TrackDescription',
116 'libvlc_audio_output_t*': 'AudioOutput',
118 'mediacontrol_Instance*': 'MediaControl',
119 'mediacontrol_Exception*': 'MediaControlException',
120 'mediacontrol_RGBPicture*': 'RGBPicture',
121 'mediacontrol_PlaylistSeq*': 'MediaControlPlaylistSeq',
122 'mediacontrol_Position*': 'MediaControlPosition',
123 'mediacontrol_StreamInformation*': 'MediaControlStreamInformation',
124 'WINDOWHANDLE': 'ctypes.c_ulong',
127 'void*': 'ctypes.c_void_p',
128 'short': 'ctypes.c_short',
129 'char*': 'ctypes.c_char_p',
130 'char**': 'ListPOINTER(ctypes.c_char_p)',
131 'uint32_t': 'ctypes.c_uint',
132 'float': 'ctypes.c_float',
133 'unsigned': 'ctypes.c_uint',
134 'int': 'ctypes.c_int',
135 '...': 'FIXMEva_list',
136 'libvlc_callback_t': 'FIXMEcallback',
137 'libvlc_time_t': 'ctypes.c_longlong',
140 # Defined python classes, i.e. classes for which we want to generate
141 # class wrappers around libvlc functions
160 #'MediaControlPosition',
161 #'MediaControlStreamInformation',
164 # Definition of prefixes that we can strip from method names when
165 # wrapping them into class methods
166 prefixes=dict( (v, k[:-2]) for (k, v) in typ2class.iteritems() if v in defined_classes )
167 prefixes['MediaControl']='mediacontrol_'
170 """Parse a C parameter expression.
172 It is used to parse both the type/name for methods, and type/name
173 for their parameters.
175 It returns a tuple (type, name).
178 s=s.replace('const', '')
179 if 'VLC_FORWARD' in s:
180 m=forward_re.match(s)
181 s=m.group(1)+m.group(2)
184 const, typ, name=m.groups()
185 while name.startswith('*'):
189 # K&R definition: const char * const*
191 typ=typ.replace(' ', '')
194 # K&R definition: only type
195 return s.replace(' ', ''), ''
197 def generate_header(classes=None):
198 """Generate header code.
200 f=open('header.py', 'r')
202 if 'build_date' in l:
203 print 'build_date="%s"' % time.ctime()
208 def convert_enum_names(enums):
210 for (typ, name, values) in enums:
212 raise Exception('This method only handles enums')
213 pyname=re.findall('(libvlc|mediacontrol)_(.+?)(_t)?$', name)[0][1]
215 pyname=pyname.title().replace('_', '')
217 pyname=pyname.capitalize()
221 def generate_enums(enums):
222 for (typ, name, values) in enums:
224 raise Exception('This method only handles enums')
225 pyname=typ2class[name]
227 print "class %s(ctypes.c_uint):" % pyname
230 # Convert symbol names
234 # Single character. Some symbols use 1_1, 5_1, etc.
235 n="_".join( k.split('_')[:-2] )
236 if re.match('^[0-9]', n):
237 # Cannot start an identifier with a number
242 print " %s=%s" % (conv[k], v)
246 print " %s: '%s'," % (v, conv[k])
251 return ".".join((self.__class__.__module__, self.__class__.__name__, self._names[self.value]))
254 def parse_typedef(name):
255 """Parse include file for typedef expressions.
257 This generates a tuple for each typedef:
258 (type, name, value_list)
259 with type == 'enum' (for the moment) and value_list being a list of (name, value)
260 Note that values are string, since this is intended for code generation.
265 # Note: lstrip() should not be necessary, but there is 1 badly
266 # formatted comment in vlc1.0.0 includes
267 if l.lstrip().startswith('/**'):
270 elif l.startswith(' * '):
271 comment = comment + l[3:]
277 accumulator=" ".join( (accumulator, l) )
282 elif l.startswith('typedef enum') and not l.endswith(';'):
283 # Multiline definition. Accumulate until end of definition
290 (typ, dummy, data, name)=m.groups()
291 for i, l in enumerate(paramlist_re.split(data)):
293 if l.startswith('/*'):
296 # A value was specified. Use it.
297 values.append(re.split('\s*=\s*', l))
300 values.append( (l, str(i)) )
301 yield (typ, name, values)
303 def parse_include(name):
304 """Parse include file.
306 This generates a tuple for each function:
307 (return_type, method_name, parameter_list, comment)
308 with parameter_list being a list of tuples (parameter_type, parameter_name).
314 # Note: lstrip() should not be necessary, but there is 1 badly
315 # formatted comment in vlc1.0.0 includes
316 if l.lstrip().startswith('/**'):
319 elif l.startswith(' * '):
320 comment = comment + l[3:]
326 accumulator=" ".join( (accumulator, l) )
331 elif l.startswith('VLC_PUBLIC_API') and not l.endswith(');'):
332 # Multiline definition. Accumulate until end of definition
338 (ret, param)=m.groups()
340 rtype, method=parse_param(ret)
343 for p in paramlist_re.split(param):
344 params.append( parse_param(p) )
346 if len(params) == 1 and params[0][0] == 'void':
347 # Empty parameter list
350 if list(p for p in params if not p[1]):
351 # Empty parameter names. Have to poke into comment.
352 names=comment_re.findall(comment)
353 if len(names) < len(params):
354 # Bad description: all parameters are not specified.
355 # Generate default parameter names
356 badnames=[ "param%d" % i for i in xrange(len(params)) ]
357 # Put in the existing ones
358 for (i, p) in enumerate(names):
361 print "### Error ###"
362 print "### Cannot get parameter names from comment for %s: %s" % (method, comment.replace("\n", ' '))
363 # Note: this was previously
364 # raise Exception("Cannot get parameter names from comment for %s: %s" % (method, comment))
365 # but it prevented code generation for a minor detail (some bad descriptions).
366 params=[ (p[0], names[i]) for (i, p) in enumerate(params) ]
368 for typ, name in params:
369 if not typ in typ2class:
370 raise Exception("No conversion for %s (from %s:%s)" % (typ, method, name))
372 # Transform Doxygen syntax into epydoc syntax
373 comment=comment.replace('\\param', '@param').replace('\\return', '@return')
376 print '********************'
379 print "%s (%s)" % (method, rtype)
380 for typ, name in params:
381 print " %s (%s)" % (name, typ)
382 print '********************'
389 def output_ctypes(rtype, method, params, comment):
390 """Output ctypes decorator for the given method.
392 if method in blacklist:
397 print "prototype=ctypes.CFUNCTYPE(%s, %s)" % (typ2class.get(rtype, 'FIXME_%s' % rtype),
398 ",".join( typ2class[p[0]] for p in params ))
400 print "prototype=ctypes.CFUNCTYPE(%s)" % typ2class.get(rtype, 'FIXME_%s' % rtype)
404 flags='paramflags= tuple()'
405 elif len(params) == 1:
406 flags="paramflags=( (%d, ), )" % parameter_passing[params[0][0]]
408 flags="paramflags=%s" % ",".join( '(%d,)' % parameter_passing[p[0]] for p in params )
410 print '%s = prototype( ("%s", dll), paramflags )' % (method, method)
412 # A VLCException is present. Process it.
413 print "%s.errcheck = check_vlc_exception" % method
414 print '%s.__doc__ = """%s"""' % (method, comment)
417 def parse_override(name):
418 """Parse override definitions file.
420 It is possible to override methods definitions in classes.
428 m=re.match('class (\S+):', l)
431 if current is not None:
432 res[current]="".join(data)
437 res[current]="".join(data)
440 # Not robust wrt. internal methods, but this works for the moment.
441 overriden_methods=dict( (k, re.findall('^\s+def\s+(\w+)', v)) for (k, v) in res.iteritems() )
443 return res, overriden_methods
445 def fix_python_comment(c):
446 """Fix comment by removing first and last parameters (self and exception)
449 body=itertools.takewhile(lambda l: not '@param' in l, data)
450 param=[ python_param_re.sub('\\1:\\2', l) for l in itertools.ifilter(lambda l: '@param' in l, data) ]
451 ret=[ l.replace('@return', '@return:') for l in itertools.ifilter(lambda l: '@return' in l, data) ]
455 elif len(param) == 1:
458 return "\n".join(itertools.chain(body, param, ret))
460 def generate_wrappers(methods):
461 """Generate class wrappers for all appropriate methods.
463 @return: the set of wrapped method names
466 # Sort methods against the element they apply to.
467 elements=sorted( ( (typ2class.get(params[0][0]), rt, met, params, c)
468 for (rt, met, params, c) in methods
469 if params and typ2class.get(params[0][0], '_') in defined_classes
471 key=operator.itemgetter(0))
473 overrides, overriden_methods=parse_override('override.py')
475 for classname, el in itertools.groupby(elements, key=operator.itemgetter(0)):
477 class %(name)s(object):
478 def __new__(cls, pointer=None):
479 '''Internal method used for instanciating wrappers from ctypes.
482 raise Exception("Internal method. You should instanciate objects through other class methods (probably named 'new' or ending with 'new')")
486 o=object.__new__(cls)
487 o._as_parameter_=ctypes.c_void_p(pointer)
492 '''(INTERNAL) ctypes parameter conversion method.
494 return arg._as_parameter_
495 """ % {'name': classname}
497 if classname in overrides:
498 print overrides[classname]
500 prefix=prefixes.get(classname, '')
502 for cl, rtype, method, params, comment in el:
503 if method in blacklist:
506 name=method.replace(prefix, '').replace('libvlc_', '')
508 if name in overriden_methods.get(cl, []):
509 # Method already defined in override.py
513 params[0]=(params[0][0], 'self')
514 if params and params[-1][0] in ('libvlc_exception_t*', 'mediacontrol_Exception*'):
515 args=", ".join( p[1] for p in params[:-1] )
517 args=", ".join( p[1] for p in params )
519 print " def %s(%s):" % (name, args)
520 print ' """%s\n"""' % fix_python_comment(comment)
521 if params and params[-1][0] == 'libvlc_exception_t*':
523 print " e=VLCException()"
524 print " return %s(%s, e)" % (method, args)
525 elif params and params[-1][0] == 'mediacontrol_Exception*':
527 print " e=MediaControlException()"
528 print " return %s(%s, e)" % (method, args)
530 print " return %s(%s)" % (method, args)
533 # Check for standard methods
535 # There is a count method. Generate a __len__ one.
536 print " def __len__(self):"
537 print " e=VLCException()"
538 print " return %s(self, e)" % method
540 elif name.endswith('item_at_index'):
541 # Indexable (and thus iterable)"
542 print " def __getitem__(self, i):"
543 print " e=VLCException()"
544 print " return %s(self, i, e)" % method
546 print " def __iter__(self):"
547 print " e=VLCException()"
548 print " for i in xrange(len(self)):"
549 print " yield self[i]"
554 if __name__ == '__main__':
556 for name in sys.argv[1:]:
557 enums.extend(list(parse_typedef(name)))
558 # Generate python names for enums
559 typ2class.update(convert_enum_names(enums))
562 for name in sys.argv[1:]:
563 methods.extend(list(parse_include(name)))
568 generate_enums(enums)
569 wrapped=generate_wrappers(methods)
573 all=set( t[1] for t in methods )
574 not_wrapped=all.difference(wrapped)
575 print "# Not wrapped methods:"
576 for m in not_wrapped: