diff options
author | Frederic Peters <fpeters@entrouvert.com> | 2008-04-29 12:02:56 +0000 |
---|---|---|
committer | Frederic Peters <fpeters@entrouvert.com> | 2008-04-29 12:02:56 +0000 |
commit | 8cff338971f7363c8b9b04c7c09f834f98df1f33 (patch) | |
tree | 4245cb0d301945c1915250c996ac09ac32f5cd5a | |
parent | 58d3f1b48bd41f0e0854fd801675891dcc11033c (diff) | |
download | lasso-8cff338971f7363c8b9b04c7c09f834f98df1f33.tar.gz lasso-8cff338971f7363c8b9b04c7c09f834f98df1f33.tar.xz lasso-8cff338971f7363c8b9b04c7c09f834f98df1f33.zip |
[project @ fpeters@0d.be-20071008113045-hi02eeinwh7a1703]
some support to generate python docstrings (on methods)
Original author: Frederic Peters <fpeters@0d.be>
Date: 2007-10-08 13:30:45.285000+02:00
-rw-r--r-- | bindings/lang_python.py | 38 | ||||
-rw-r--r-- | bindings/t.py | 26 |
2 files changed, 64 insertions, 0 deletions
diff --git a/bindings/lang_python.py b/bindings/lang_python.py index c1f8d459..616d3491 100644 --- a/bindings/lang_python.py +++ b/bindings/lang_python.py @@ -285,6 +285,10 @@ import lasso c_args = '' print >> fd, ' def %s(self%s):' % (format_underscore_as_py(mname), py_args) + if m.docstring: + print >> fd, " '''" + print >> fd, self.format_docstring(m, mname, 8) + print >> fd, " '''" if m.return_type == 'void': print >> fd, ' _lasso.%s(self._cptr%s)' % ( m.name[6:], c_args) @@ -304,6 +308,40 @@ import lasso print >> fd, '' + def format_docstring(self, func, method_name, indent): + docstring = func.docstring + if func.args: + first_arg_name = func.args[0][1] + else: + first_arg_name = None + def rep(s): + type = s.group(1)[0] + var = s.group(1)[1:] + if type == '#': # struct + if var.startswith('Lasso'): + return var[5:] + elif type == '%': # %TRUE, %FALSE + if var == 'TRUE': + return 'True' + if var == 'FALSE': + return 'False' + print >> sys.stderr, 'W: unknown docstring thingie: %s' % s.group(1) + elif type == '@': + if var == first_arg_name: + return 'self' + + return s.group(1) + lines = [] + for l in docstring.splitlines(): + if l.strip() and not lines: + continue + lines.append(l) + s = '\n'.join([indent * ' ' + x for x in lines[1:]]) + s = s.replace('NULL', 'None') + regex = re.compile(r'([\#%@]\w+)', re.DOTALL) + s = regex.sub(rep, s) + return s + def generate_wrapper(self, fd): print >> fd, open('lang_python_wrapper_top.c').read() for h in self.binding_data.headers: diff --git a/bindings/t.py b/bindings/t.py index 700188db..e8f1d558 100644 --- a/bindings/t.py +++ b/bindings/t.py @@ -59,6 +59,30 @@ class BindingData: c.methods.append(f) self.functions.remove(f) + def look_for_docstrings(self): + regex = re.compile(r'\/\*\*\s(.*?)\*\*\/', re.DOTALL) + for base, dirnames, filenames in os.walk('../lasso/'): + if base.endswith('/.svn'): + # ignore svn directories + continue + if not 'Makefile.am' in filenames: + # not a source dir + continue + makefile_am = open(os.path.join(base, 'Makefile.am')).read() + filenames = [x for x in filenames if x.endswith('.c') if x in makefile_am] + for filename in filenames: + s = open(os.path.join(base, filename)).read() + docstrings = regex.findall(s) + for d in docstrings: + docstring = '\n'.join([x[3:] for x in d.splitlines()]) + function_name = docstring.splitlines(1)[0].strip().strip(':') + func = [f for f in self.functions if f.name == function_name] + if not func: + continue + func = func[0] + func.docstring = docstring + + class Struct: def __init__(self, name): @@ -82,6 +106,7 @@ class Function: return_type = None name = None args = None + docstring = None def __repr__(self): return '%s %s %r' % (self.return_type, self.name, self.args) @@ -251,6 +276,7 @@ def parse_headers(): binding = BindingData() parse_headers() binding.order_class_hierarchy() +binding.look_for_docstrings() binding.attach_methods() python_binding = lang_python.PythonBinding(binding) |