From kde-commits Fri Sep 30 21:54:32 2011 From: Sven Brauch Date: Fri, 30 Sep 2011 21:54:32 +0000 To: kde-commits Subject: [kdev-python] documentation_src/numpy: more complete version of the Message-Id: <20110930215432.AC521A60A6 () git ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=131741969303024 Git commit a3a051a623266beee370178bcaa599a41554ef1f by Sven Brauch. Committed on 30/09/2011 at 22:00. Pushed by brauch into branch 'master'. more complete version of the numpy doc generator M +56 -4 documentation_src/numpy/generate_numpy_doc.py http://commits.kde.org/kdev-python/a3a051a623266beee370178bcaa599a41554ef1f diff --git a/documentation_src/numpy/generate_numpy_doc.py b/documentation_src/numpy/generate_numpy_doc.py index 1c8fbe6..5dd43df 100644 --- a/documentation_src/numpy/generate_numpy_doc.py +++ b/documentation_src/numpy/generate_numpy_doc.py @@ -10,6 +10,30 @@ def indent(text): result += " " + line + "\n" return result +def get_indent(string): + string = string.split("\n")[0] + indent = 0 + for char in string: + if char in [' ', '\t']: + indent += 1 + else: + return indent + return 0 + +def remove_indent(string): + if type(string) == types.StringType: + string = string.split("\n") + max_remove_indent = get_indent(string[0]) + result = "" + for line in string: + for offset in xrange(0, len(line)): + if line[offset] not in [' ', '\t'] or offset > max_remove_indent: + result += line[offset:] + "\n" + break + return result + else: + return string + def make_documentation(class_or_module): result = "" for item_name in dir(class_or_module): @@ -21,8 +45,8 @@ def make_documentation(class_or_module): item = getattr(class_or_module, item_name) if type(item) in [types.FunctionType, types.BuiltinFunctionType, types.UnboundMethodType]: parameters = try_parse_func_docstring(item.__doc__) - result += "def %s():\n" % item_name - result += indent('"""%s"""\n\n' % item.__doc__) + result += "def %s(%s):\n" % ( item_name, ','.join(parameters) ) + result += indent('"""%s"""\n\n' % remove_indent(item.__doc__)) elif type(item) == types.TypeType: result += "class %s:\n" % item_name result += indent(make_documentation(item)) + "\n" @@ -38,9 +62,37 @@ def make_documentation(class_or_module): def try_parse_func_docstring(docstring): if type(docstring) == types.StringType: - params_found = re.search("\n[\\s]*Parameters[\\s]*\n", docstring, re.U) + indent = 0 + atLineBeginning = True + paramListBegin = paramListEnd = 0 + for offset in xrange(0, len(docstring)): + if docstring[offset] == "\n": + indent = 0 + if docstring[offset] in [' ', '\t'] and atLineBeginning: + indent += 1 + else: + atLineBeginning = False + if docstring[offset:offset+len("Parameters")] == "Parameters": + paramListBegin = offset + if docstring[offset:offset+len("Returns")] == "Returns": + paramListEnd = offset + relevantPart = docstring[paramListBegin:paramListEnd].split("\n")[2:] + if len(relevantPart): + firstIndent = get_indent(relevantPart[0]) + parameter_name_list = [] + for line_index in xrange(0, len(relevantPart)): + if get_indent(relevantPart[line_index]) == firstIndent: + s = relevantPart[line_index].split(' : ') + if len(s) == 2: + name = s[0] + type_string = s[1] + doc_for_param = None # TODO extract this, and display it in some way... or not + parameter_name_list.append(name.replace(' ', '').replace('\t', '')) + return parameter_name_list + else: + return [] else: - params_found = None + return [] if __name__ == '__main__': print make_documentation(numpy) \ No newline at end of file