From 0a437254bffc90c4716b1265444082cab7fb94c2 Mon Sep 17 00:00:00 2001 From: Olivier Aubert Date: Fri, 31 Jul 2009 16:39:32 +0200 Subject: [PATCH] python-ctypes: allow to specify class docstrings in override.py --- bindings/python-ctypes/generate.py | 28 ++++++++--- bindings/python-ctypes/override.py | 79 ++++++++++++++++++++---------- 2 files changed, 73 insertions(+), 34 deletions(-) diff --git a/bindings/python-ctypes/generate.py b/bindings/python-ctypes/generate.py index 4f6e6ef45d..f05411f211 100755 --- a/bindings/python-ctypes/generate.py +++ b/bindings/python-ctypes/generate.py @@ -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. ''' diff --git a/bindings/python-ctypes/override.py b/bindings/python-ctypes/override.py index 88db892553..e9542d2ac8 100644 --- a/bindings/python-ctypes/override.py +++ b/bindings/python-ctypes/override.py @@ -1,31 +1,58 @@ 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) -- 2.39.2