|
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 # __IGNORE_WARNING_D202__ |
|
28 lines = [] |
|
29 |
|
30 # function description |
|
31 lines.append("") |
|
32 |
|
33 # remove 'self', 'this' or 'cls' from arguments list |
|
34 if ( |
|
35 len(functionInfo.argumentsList) > 0 and |
|
36 functionInfo.argumentsList[0][0] in ("self", "cls", "this") |
|
37 ): |
|
38 del functionInfo.argumentsList[0] |
|
39 |
|
40 # determine additional indentation string |
|
41 indentWidth = editor.indentationWidth() |
|
42 if indentWidth == 0: |
|
43 indentWidth = editor.tabWidth() |
|
44 indent = indentWidth * " " |
|
45 |
|
46 # add the parameters section |
|
47 if functionInfo.argumentsList: |
|
48 lines.append("") |
|
49 lines.append("Args:") |
|
50 for argName, argType, argValue in functionInfo.argumentsList: |
|
51 argLine = "{0}{1} ".format(indent, argName) |
|
52 argLine += "(" |
|
53 if argType: |
|
54 argLine += "{0}".format(argType) |
|
55 else: |
|
56 argLine += "TYPE" |
|
57 argLine += "):" |
|
58 lines.append(argLine) |
|
59 argLine = "{0}".format(2 * indent) |
|
60 if argValue: |
|
61 argLine += "Optional; " |
|
62 argLine += "DESCRIPTION" |
|
63 if argValue: |
|
64 argLine += " Defaults to {0}.".format(argValue) |
|
65 lines.append(argLine) |
|
66 |
|
67 # add return section |
|
68 lines.append("") |
|
69 if functionInfo.hasYield: |
|
70 lines.append("Yields:") |
|
71 else: |
|
72 lines.append("Returns:") |
|
73 if functionInfo.returnTypeAnnotated: |
|
74 lines.append("{0}{1}: DESCRIPTION".format( |
|
75 indent, functionInfo.returnTypeAnnotated)) |
|
76 elif functionInfo.returnValueInBody: |
|
77 lines.append("{0}TYPE: DESCRIPTION") |
|
78 else: |
|
79 lines.append("{0}None".format(indent)) |
|
80 |
|
81 # add an exceptions section, if function raises something |
|
82 if functionInfo.raiseList: |
|
83 lines.append("") |
|
84 lines.append("Raises:") |
|
85 for exc in sorted(functionInfo.raiseList): |
|
86 lines.append("{0}{1}: DESCRIPTION".format(indent, exc)) |
|
87 |
|
88 return lines |