method,
params,
comment)
+ comment=''
def output_ctypes(rtype, method, params, comment):
"""Output ctypes decorator for the given method.
"""Parse override definitions file.
It is possible to override methods definitions in classes.
+
+ It returns a tuple
+ (code, overriden_methods, docstring)
"""
- res={}
+ code={}
data=[]
current=None
if m:
# Dump old data
if current is not None:
- res[current]="\n".join(data)
+ code[current]="".join(data)
current=m.group(1)
data=[]
continue
data.append(l)
- res[current]="\n".join(data)
+ code[current]="".join(data)
f.close()
- return res
+
+ docstring={}
+ for k, v in code.iteritems():
+ if v.lstrip().startswith('"""'):
+ # Starting comment. Use it as docstring.
+ dummy, docstring[k], code[k]=v.split('"""', 2)
+
+ # Not robust wrt. internal methods, but this works for the moment.
+ overridden_methods=dict( (k, re.findall('^\s+def\s+(\w+)', v, re.MULTILINE)) for (k, v) in code.iteritems() )
+
+ return code, overridden_methods, docstring
def fix_python_comment(c):
"""Fix comment by removing first and last parameters (self and exception)
),
key=operator.itemgetter(0))
- overrides=parse_override('override.py')
+ overrides, overriden_methods, docstring=parse_override('override.py')
for classname, el in itertools.groupby(elements, key=operator.itemgetter(0)):
+ print """class %(name)s(object):""" % {'name': classname}
+ if classname in docstring:
+ print ' """%s\n """' % docstring[classname]
+
print """
-class %(name)s(object):
- def __init__(self, pointer=None):
+ def __new__(cls, pointer=None):
'''Internal method used for instanciating wrappers from ctypes.
'''
if pointer is None:
raise Exception("Internal method. You should instanciate objects through other class methods (probably named 'new' or ending with 'new')")
- self._as_parameter_=ctypes.c_void_p(pointer)
+ if pointer == 0:
+ return None
+ else:
+ o=object.__new__(cls)
+ o._as_parameter_=ctypes.c_void_p(pointer)
+ return o
@staticmethod
def from_param(arg):
# Strip prefix
name=method.replace(prefix, '').replace('libvlc_', '')
ret.add(method)
+ if name in overriden_methods.get(cl, []):
+ # Method already defined in override.py
+ continue
+
if params:
params[0]=(params[0][0], 'self')
if params and params[-1][0] in ('libvlc_exception_t*', 'mediacontrol_Exception*'):
args=", ".join( p[1] for p in params )
print " def %s(%s):" % (name, args)
- print ' """%s\n"""' % fix_python_comment(comment)
+ print ' """%s\n """' % fix_python_comment(comment)
if params and params[-1][0] == 'libvlc_exception_t*':
# Exception handling
print " e=VLCException()"
else:
print " return %s(%s)" % (method, args)
print
+
+ # Check for standard methods
+ if name == 'count':
+ # There is a count method. Generate a __len__ one.
+ print " def __len__(self):"
+ print " e=VLCException()"
+ print " return %s(self, e)" % method
+ print
+ elif name.endswith('item_at_index'):
+ # Indexable (and thus iterable)"
+ print " def __getitem__(self, i):"
+ print " e=VLCException()"
+ print " return %s(self, i, e)" % method
+ print
+ print " def __iter__(self):"
+ print " e=VLCException()"
+ print " for i in xrange(len(self)):"
+ print " yield self[i]"
+ print
+
return ret
if __name__ == '__main__':