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 'ericdoc' 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 | """ |
9486
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
13 | Function to generate the docstring line list iaw. eric documentation |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
14 | style. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
15 | |
9486
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
16 | Note: Text is created with DESCRIPTION placeholders for descriptions and |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
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 | |
9486
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
22 | @param editor reference to the editor |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
23 | @type Editor |
7998 | 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 | # create a basic/partial function description |
31 | if functionInfo.functionType == "classmethod": | |
32 | descr = "Class method " | |
33 | elif functionInfo.functionType == "staticmethod": | |
34 | descr = "Static method " | |
35 | elif functionInfo.functionType == "constructor": | |
36 | descr = "Constructor" | |
37 | else: | |
38 | if functionInfo.visibility == "public": | |
39 | descr = "Public " | |
40 | elif functionInfo.visibility == "protected": | |
41 | descr = "Protected " | |
42 | elif functionInfo.visibility == "private": | |
43 | descr = "Private " | |
44 | elif functionInfo.visibility == "special": | |
45 | descr = "Special " | |
46 | else: | |
47 | descr = "" | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
48 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
49 | 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
|
50 | "self", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
51 | "cls", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
52 | "this", |
7998 | 53 | ): |
54 | if functionInfo.isAsync: | |
55 | descr += "coroutine " | |
56 | elif functionInfo.functionType == "qtslot": | |
57 | descr += "slot " | |
58 | else: | |
59 | descr += "method " | |
60 | else: | |
61 | if functionInfo.isAsync: | |
62 | descr = "Coroutine " | |
63 | elif functionInfo.functionType == "qtslot": | |
64 | descr = "Slot " | |
65 | else: | |
66 | descr = "Function " | |
67 | lines.append(descr) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
68 | |
7998 | 69 | # 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
|
70 | 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
|
71 | "self", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
72 | "cls", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
73 | "this", |
7998 | 74 | ): |
75 | del functionInfo.argumentsList[0] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
76 | |
7998 | 77 | # add an empty line if there is one of the other sections |
78 | if ( | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
79 | functionInfo.argumentsList |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
80 | or functionInfo.hasYield |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
81 | or functionInfo.returnTypeAnnotated |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
82 | or functionInfo.returnValueInBody |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
83 | or functionInfo.raiseList |
7998 | 84 | ): |
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 | # add the parameters section |
88 | tag = "@param" | |
89 | for argName, argType, argValue in functionInfo.argumentsList: | |
90 | if argName == "*": | |
91 | tag = "@keyparam" | |
92 | continue | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
93 | |
7998 | 94 | argLine = "{0} {1} DESCRIPTION".format(tag, argName) |
95 | if argValue: | |
96 | argLine += " (defaults to {0})".format(argValue) | |
97 | lines.append(argLine) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
98 | |
7998 | 99 | if argType is None: |
100 | argType = "TYPE" | |
101 | argLine = "@type {0}".format(argType) | |
102 | if argValue: | |
103 | argLine += " (optional)" | |
104 | lines.append(argLine) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
105 | |
7998 | 106 | # 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
|
107 | if ( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
108 | functionInfo.hasYield |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
109 | or functionInfo.returnValueInBody |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
110 | 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
|
111 | ): |
7998 | 112 | if functionInfo.hasYield: |
113 | lines.append("@yield DESCRIPTION") | |
114 | rType = "@ytype" | |
115 | else: | |
116 | lines.append("@return DESCRIPTION") | |
117 | rType = "@rtype" | |
118 | if functionInfo.returnTypeAnnotated: | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
119 | lines.append("{0} {1}".format(rType, functionInfo.returnTypeAnnotated)) |
7998 | 120 | else: |
121 | lines.append("{0} TYPE".format(rType)) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
122 | |
7998 | 123 | # add an exceptions section, if function raises something |
124 | if functionInfo.raiseList: | |
125 | for exc in sorted(functionInfo.raiseList): | |
126 | lines.append("@exception {0} DESCRIPTION".format(exc)) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
127 | |
7998 | 128 | return lines |