Commit 0a437254 authored by Olivier Aubert's avatar Olivier Aubert

python-ctypes: allow to specify class docstrings in override.py

parent 96cf477d
......@@ -418,8 +418,11 @@ def parse_override(name):
"""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
......@@ -429,18 +432,24 @@ def parse_override(name):
if m:
# Dump old data
if current is not None:
res[current]="".join(data)
code[current]="".join(data)
current=m.group(1)
data=[]
continue
data.append(l)
res[current]="".join(data)
code[current]="".join(data)
f.close()
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.
overriden_methods=dict( (k, re.findall('^\s+def\s+(\w+)', v)) for (k, v) in res.iteritems() )
overridden_methods=dict( (k, re.findall('^\s+def\s+(\w+)', v, re.MULTILINE)) for (k, v) in code.iteritems() )
return res, overriden_methods
return code, overridden_methods, docstring
def fix_python_comment(c):
"""Fix comment by removing first and last parameters (self and exception)
......@@ -470,11 +479,14 @@ def generate_wrappers(methods):
),
key=operator.itemgetter(0))
overrides, overriden_methods=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 __new__(cls, pointer=None):
'''Internal method used for instanciating wrappers from ctypes.
'''
......
class Instance:
@staticmethod
def new(*p):
"""Create a new Instance.
"""
e=VLCException()
return libvlc_new(len(p), p, e)
"""Create a new Instance instance.
class MediaControl:
@staticmethod
def new(*p):
"""Create a new MediaControl
"""
e=MediaControlException()
return mediacontrol_new(len(p), p, e)
It may take as parameter either:
* a string
* a list of strings as first parameters
* the parameters given as the constructor parameters (must be strings)
* a MediaControl instance
"""
def __new__(cls, *p):
if p and p[0] == 0:
return None
elif p and isinstance(p[0], (int, long)):
# instance creation from ctypes
o=object.__new__(cls)
o._as_parameter_=ctypes.c_void_p(p[0])
return o
elif len(p) == 1 and isinstance(p[0], basestring):
# Only 1 string parameter: should be a parameter line
p=p[0].split()
elif len(p) == 1 and isinstance(p[0], (tuple, list)):
p=p[0]
if p and isinstance(p[0], MediaControl):
return p[0].get_instance()
else:
e=VLCException()
return libvlc_new(len(p), p, e)
@staticmethod
def new_from_instance(i):
"""Create a new MediaControl from an existing Instance.
"""
e=MediaControlException()
return mediacontrol_new_from_instance(i, e)
class MediaControl:
"""Create a new MediaControl instance
class MediaList:
def __len__(self):
e=VLCException()
return libvlc_media_list_count(self, e)
It may take as parameter either:
* a string
* a list of strings as first parameters
* the parameters given as the constructor parameters (must be strings)
* a vlc.Instance
"""
def __new__(cls, *p):
if p and p[0] == 0:
return None
elif p and isinstance(p[0], (int, long)):
# instance creation from ctypes
o=object.__new__(cls)
o._as_parameter_=ctypes.c_void_p(p[0])
return o
elif len(p) == 1 and isinstance(p[0], basestring):
# Only 1 string parameter: should be a parameter line
p=p[0].split()
elif len(p) == 1 and isinstance(p[0], (tuple, list)):
p=p[0]
def __getitem__(self, i):
e=VLCException()
return libvlc_media_list_item_at_index(self, i, e)
if p and isinstance(p[0], Instance):
e=MediaControlException()
return mediacontrol_new_from_instance(p[0])
else:
e=MediaControlException()
return mediacontrol_new(len(p), p, e)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment