Mon, 07 Nov 2022 17:19:58 +0100
Corrected/acknowledged some bad import style and removed some obsolete code.
7998 | 1 | # -*- coding: utf-8 -*- |
2 | ||
8881
54e42bc2437a
Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
3 | # Copyright (c) 2021 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
7998 | 4 | # |
5 | ||
6 | """ | |
7 | Module implementing the docstring generator for the 'sphinxdoc' style. | |
8 | """ | |
9 | ||
10 | ||
11 | def generateSphinxDoc(functionInfo): | |
12 | """ | |
13 | Function to generate the docstring line list iaw. Sphinx documentation | |
14 | style. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
15 | |
7998 | 16 | Note: Text is created with DESCRIPTION placeholders for descriptions and |
17 | TYPE placeholders for type information | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
18 | |
7998 | 19 | @param functionInfo object containing the function information to base |
20 | the docstring on | |
21 | @type FunctionInfo | |
22 | @return list of docstring lines | |
23 | @rtype str | |
24 | """ | |
8000
47b15df088e4
Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7998
diff
changeset
|
25 | # __IGNORE_WARNING_D202__ |
7998 | 26 | lines = [] |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
27 | |
7998 | 28 | # function description |
8000
47b15df088e4
Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7998
diff
changeset
|
29 | lines.append("") |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
30 | |
7998 | 31 | # remove 'self', 'this' or 'cls' from arguments list |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
32 | if len(functionInfo.argumentsList) > 0 and functionInfo.argumentsList[0][0] in ( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
33 | "self", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
34 | "cls", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
35 | "this", |
7998 | 36 | ): |
37 | del functionInfo.argumentsList[0] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
38 | |
7998 | 39 | # add an empty line if there is one of the other sections |
40 | if ( | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
41 | functionInfo.argumentsList |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
42 | or functionInfo.hasYield |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
43 | or functionInfo.returnTypeAnnotated |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
44 | or functionInfo.returnValueInBody |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
45 | or functionInfo.raiseList |
7998 | 46 | ): |
47 | lines.append("") | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
48 | |
7998 | 49 | # add the parameters section |
50 | for argName, argType, argValue in functionInfo.argumentsList: | |
51 | argLine = ":param {0}: DESCRIPTION".format(argName) | |
52 | if argValue: | |
53 | argLine += ", defaults to {0}".format(argValue) | |
54 | lines.append(argLine) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
55 | |
7998 | 56 | argLine = ":type {0}: ".format(argName) |
57 | if argType: | |
58 | argLine += "{0}".format(argType) | |
59 | else: | |
60 | argLine += "TYPE" | |
61 | if argValue: | |
62 | argLine += ", optional" | |
63 | lines.append(argLine) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
64 | |
7998 | 65 | # add an exceptions section, if function raises something |
66 | if functionInfo.raiseList: | |
67 | for exc in sorted(functionInfo.raiseList): | |
68 | lines.append(":raises {0}: DESCRIPTION".format(exc)) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
69 | |
7998 | 70 | # add return section |
8005
94a6a1bd5f80
Docstring: corrected the return section generation for ericdoc and sphinxdoc.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8000
diff
changeset
|
71 | if ( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
72 | functionInfo.hasYield |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
73 | or functionInfo.returnValueInBody |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
74 | or functionInfo.returnTypeAnnotated |
8005
94a6a1bd5f80
Docstring: corrected the return section generation for ericdoc and sphinxdoc.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8000
diff
changeset
|
75 | ): |
7998 | 76 | if functionInfo.hasYield: |
77 | lines.append(":yield: DESCRIPTION") | |
78 | else: | |
79 | lines.append(":return: DESCRIPTION") | |
80 | if functionInfo.returnTypeAnnotated: | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
81 | lines.append(":rtype: {0}".format(functionInfo.returnTypeAnnotated)) |
7998 | 82 | else: |
83 | lines.append(":rtype: TYPE") | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
84 | |
7998 | 85 | lines.append("") |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
86 | |
7998 | 87 | return lines |