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): ...@@ -418,8 +418,11 @@ def parse_override(name):
"""Parse override definitions file. """Parse override definitions file.
It is possible to override methods definitions in classes. It is possible to override methods definitions in classes.
It returns a tuple
(code, overriden_methods, docstring)
""" """
res={} code={}
data=[] data=[]
current=None current=None
...@@ -429,18 +432,24 @@ def parse_override(name): ...@@ -429,18 +432,24 @@ def parse_override(name):
if m: if m:
# Dump old data # Dump old data
if current is not None: if current is not None:
res[current]="".join(data) code[current]="".join(data)
current=m.group(1) current=m.group(1)
data=[] data=[]
continue continue
data.append(l) data.append(l)
res[current]="".join(data) code[current]="".join(data)
f.close() 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. # 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): def fix_python_comment(c):
"""Fix comment by removing first and last parameters (self and exception) """Fix comment by removing first and last parameters (self and exception)
...@@ -470,11 +479,14 @@ def generate_wrappers(methods): ...@@ -470,11 +479,14 @@ def generate_wrappers(methods):
), ),
key=operator.itemgetter(0)) 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)): 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 """ print """
class %(name)s(object):
def __new__(cls, pointer=None): def __new__(cls, pointer=None):
'''Internal method used for instanciating wrappers from ctypes. '''Internal method used for instanciating wrappers from ctypes.
''' '''
......
class Instance: class Instance:
@staticmethod """Create a new Instance instance.
def new(*p):
"""Create a new Instance.
"""
e=VLCException()
return libvlc_new(len(p), p, e)
class MediaControl: It may take as parameter either:
@staticmethod * a string
def new(*p): * a list of strings as first parameters
"""Create a new MediaControl * the parameters given as the constructor parameters (must be strings)
""" * a MediaControl instance
e=MediaControlException() """
return mediacontrol_new(len(p), p, e) 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 class MediaControl:
def new_from_instance(i): """Create a new MediaControl instance
"""Create a new MediaControl from an existing Instance.
"""
e=MediaControlException()
return mediacontrol_new_from_instance(i, e)
class MediaList: It may take as parameter either:
def __len__(self): * a string
e=VLCException() * a list of strings as first parameters
return libvlc_media_list_count(self, e) * 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): if p and isinstance(p[0], Instance):
e=VLCException() e=MediaControlException()
return libvlc_media_list_item_at_index(self, i, e) 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