Sat, 31 Dec 2022 16:23:21 +0100
Updated copyright for 2023.
7998 | 1 | # -*- coding: utf-8 -*- |
2 | ||
9653
e67609152c5e
Updated copyright for 2023.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9486
diff
changeset
|
3 | # Copyright (c) 2021 - 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
7998 | 4 | # |
5 | ||
6 | """ | |
7 | Module implementing the docstring generator for the 'numpydoc' style. | |
8 | """ | |
9 | ||
10 | ||
9486
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
11 | def generateDoc(functionInfo, editor): |
7998 | 12 | """ |
13 | Function to generate the docstring line list iaw. NumPy 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 | @param editor reference to the editor | |
23 | @type Editor | |
24 | @return list of docstring lines | |
25 | @rtype str | |
26 | """ | |
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
|
27 | # __IGNORE_WARNING_D202__ |
7998 | 28 | lines = [] |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
29 | |
7998 | 30 | # 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
|
31 | lines.append("") |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
32 | |
7998 | 33 | # 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
|
34 | 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
|
35 | "self", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
36 | "cls", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
37 | "this", |
7998 | 38 | ): |
39 | del functionInfo.argumentsList[0] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
40 | |
7998 | 41 | # determine additional indentation string |
42 | indentWidth = editor.indentationWidth() | |
43 | if indentWidth == 0: | |
44 | indentWidth = editor.tabWidth() | |
45 | indent = indentWidth * " " | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
46 | |
7998 | 47 | # add the parameters section |
48 | if functionInfo.argumentsList: | |
49 | lines.append("") | |
50 | lines.append("Parameters") | |
51 | lines.append("----------") | |
52 | for argName, argType, argValue in functionInfo.argumentsList: | |
53 | argLine = "{0} : ".format(argName) | |
54 | if argType: | |
55 | argLine += "{0}".format(argType) | |
56 | else: | |
57 | argLine += "TYPE" | |
58 | if argValue: | |
59 | argLine += ", optional" | |
60 | lines.append(argLine) | |
61 | argLine = "{0}DESCRIPTION.".format(indent) | |
62 | if argValue: | |
63 | argLine += " The default is {0}".format(argValue) | |
64 | lines.append(argLine) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
65 | |
7998 | 66 | # add an exceptions section, if function raises something |
67 | if functionInfo.raiseList: | |
68 | lines.append("") | |
69 | lines.append("Raises") | |
70 | lines.append("------") | |
71 | for exc in sorted(functionInfo.raiseList): | |
72 | lines.append("{0}".format(exc)) | |
73 | lines.append("{0}DESCRIPTION".format(indent)) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
74 | |
7998 | 75 | # add return section |
76 | lines.append("") | |
77 | if functionInfo.hasYield: | |
78 | lines.append("Yields") | |
79 | lines.append("------") | |
80 | else: | |
81 | lines.append("Returns") | |
82 | lines.append("-------") | |
83 | if functionInfo.returnTypeAnnotated: | |
84 | lines.append("{0}".format(functionInfo.returnTypeAnnotated)) | |
85 | lines.append("{0}DESCRIPTION.".format(indent)) | |
86 | elif functionInfo.returnValueInBody: | |
87 | lines.append("TYPE") | |
88 | lines.append("{0}DESCRIPTION.".format(indent)) | |
89 | else: | |
90 | lines.append("{0}None".format(indent)) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
91 | |
7998 | 92 | return lines |