eric7/QScintilla/DocstringGenerator/SphinxdocGenerator.py

branch
eric7
changeset 8312
800c432b34c8
parent 8005
94a6a1bd5f80
child 8881
54e42bc2437a
diff -r 4e8b98454baa -r 800c432b34c8 eric7/QScintilla/DocstringGenerator/SphinxdocGenerator.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eric7/QScintilla/DocstringGenerator/SphinxdocGenerator.py	Sat May 15 18:45:04 2021 +0200
@@ -0,0 +1,87 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing the docstring generator for the 'sphinxdoc' style.
+"""
+
+
+def generateSphinxDoc(functionInfo):
+    """
+    Function to generate the docstring line list iaw. Sphinx documentation
+    style.
+    
+    Note: Text is created with DESCRIPTION placeholders for descriptions and
+    TYPE placeholders for type information
+    
+    @param functionInfo object containing the function information to base
+        the docstring on
+    @type FunctionInfo
+    @return list of docstring lines
+    @rtype str
+    """
+    # __IGNORE_WARNING_D202__
+    lines = []
+    
+    # function description
+    lines.append("")
+    
+    # remove 'self', 'this' or 'cls' from arguments list
+    if (
+        len(functionInfo.argumentsList) > 0 and
+        functionInfo.argumentsList[0][0] in ("self", "cls", "this")
+    ):
+        del functionInfo.argumentsList[0]
+    
+    # add an empty line if there is one of the other sections
+    if (
+        functionInfo.argumentsList or
+        functionInfo.hasYield or
+        functionInfo.returnTypeAnnotated or
+        functionInfo.returnValueInBody or
+        functionInfo.raiseList
+    ):
+        lines.append("")
+    
+    # add the parameters section
+    for argName, argType, argValue in functionInfo.argumentsList:
+        argLine = ":param {0}: DESCRIPTION".format(argName)
+        if argValue:
+            argLine += ", defaults to {0}".format(argValue)
+        lines.append(argLine)
+        
+        argLine = ":type {0}: ".format(argName)
+        if argType:
+            argLine += "{0}".format(argType)
+        else:
+            argLine += "TYPE"
+        if argValue:
+            argLine += ", optional"
+        lines.append(argLine)
+    
+    # add an exceptions section, if function raises something
+    if functionInfo.raiseList:
+        for exc in sorted(functionInfo.raiseList):
+            lines.append(":raises {0}: DESCRIPTION".format(exc))
+    
+    # add return section
+    if (
+        functionInfo.hasYield or
+        functionInfo.returnValueInBody or
+        functionInfo.returnTypeAnnotated
+    ):
+        if functionInfo.hasYield:
+            lines.append(":yield: DESCRIPTION")
+        else:
+            lines.append(":return: DESCRIPTION")
+        if functionInfo.returnTypeAnnotated:
+            lines.append(":rtype: {0}".format(
+                functionInfo.returnTypeAnnotated))
+        else:
+            lines.append(":rtype: TYPE")
+    
+    lines.append("")
+    
+    return lines

eric ide

mercurial