|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the docstring generator for the 'sphinxdoc' style. |
|
8 """ |
|
9 |
|
10 |
|
11 def generateGoogleDoc(functionInfo, editor): |
|
12 """ |
|
13 Function to generate the docstring line list iaw. Sphinx documentation |
|
14 style. |
|
15 |
|
16 Note: Text is created with DESCRIPTION placeholders for descriptions and |
|
17 TYPE placeholders for type information |
|
18 |
|
19 @param functionInfo object containing the function information to base |
|
20 the docstring on |
|
21 @type FunctionInfo |
|
22 @param editor reference to the editor |
|
23 @type Editor |
|
24 @return list of docstring lines |
|
25 @rtype str |
|
26 """ |
|
27 lines = [] |
|
28 |
|
29 # function description |
|
30 lines.append("DESCRIPTION") |
|
31 |
|
32 # remove 'self', 'this' or 'cls' from arguments list |
|
33 if ( |
|
34 len(functionInfo.argumentsList) > 0 and |
|
35 functionInfo.argumentsList[0][0] in ("self", "cls", "this") |
|
36 ): |
|
37 del functionInfo.argumentsList[0] |
|
38 |
|
39 # determine additional indentation string |
|
40 indentWidth = editor.indentationWidth() |
|
41 if indentWidth == 0: |
|
42 indentWidth = editor.tabWidth() |
|
43 indent = indentWidth * " " |
|
44 |
|
45 # add the parameters section |
|
46 if functionInfo.argumentsList: |
|
47 lines.append("") |
|
48 lines.append("Args:") |
|
49 for argName, argType, argValue in functionInfo.argumentsList: |
|
50 argLine = "{0}{1} ".format(indent, argName) |
|
51 argLine += "(" |
|
52 if argType: |
|
53 argLine += "{0}".format(argType) |
|
54 else: |
|
55 argLine += "TYPE" |
|
56 argLine += "):" |
|
57 lines.append(argLine) |
|
58 argLine = "{0}".format(2 * indent) |
|
59 if argValue: |
|
60 argLine += "Optional; " |
|
61 argLine += "DESCRIPTION" |
|
62 if argValue: |
|
63 argLine += " Defaults to {0}.".format(argValue) |
|
64 lines.append(argLine) |
|
65 |
|
66 # add return section |
|
67 lines.append("") |
|
68 if functionInfo.hasYield: |
|
69 lines.append("Yields:") |
|
70 else: |
|
71 lines.append("Returns:") |
|
72 if functionInfo.returnTypeAnnotated: |
|
73 lines.append("{0}{1}: DESCRIPTION".format( |
|
74 indent, functionInfo.returnTypeAnnotated)) |
|
75 elif functionInfo.returnValueInBody: |
|
76 lines.append("{0}TYPE: DESCRIPTION") |
|
77 else: |
|
78 lines.append("{0}None".format(indent)) |
|
79 |
|
80 # add an exceptions section, if function raises something |
|
81 if functionInfo.raiseList: |
|
82 lines.append("") |
|
83 lines.append("Raises:") |
|
84 for exc in sorted(functionInfo.raiseList): |
|
85 lines.append("{0}{1}: DESCRIPTION".format(indent, exc)) |
|
86 |
|
87 return lines |