Tue, 08 Nov 2022 11:50:50 +0100
Changed docstring generator imports to use importlib.import_module().
7998 | 1 | # -*- coding: utf-8 -*- |
2 | ||
8881
54e42bc2437a
Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8358
diff
changeset
|
3 | # Copyright (c) 2021 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
7998 | 4 | # |
5 | ||
6 | """ | |
7 | Module implementing a docstring generator base class. | |
8 | """ | |
9 | ||
9486
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
10 | import importlib |
7998 | 11 | import re |
12 | ||
8318
962bce857696
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
13 | from PyQt6.QtCore import Qt |
962bce857696
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
14 | from PyQt6.QtWidgets import QMenu |
7998 | 15 | |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
16 | from eric7 import Preferences |
9413
80c06d472826
Changed the eric7 import statements to include the package name (i.e. eric7) in order to not fiddle with sys.path.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
17 | from eric7.EricWidgets.EricApplication import ericApp |
7998 | 18 | |
19 | ||
20 | def getIndentStr(text): | |
21 | """ | |
22 | Function to get the indentation of a text. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
23 | |
7998 | 24 | @param text text to extract indentation from |
25 | @type str | |
26 | @return indentation string | |
27 | @rtype str | |
28 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
29 | indent = "" |
7998 | 30 | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
31 | ret = re.match(r"(\s*)", text) |
7998 | 32 | if ret: |
33 | indent = ret.group(1) | |
34 | ||
35 | return indent | |
36 | ||
37 | ||
8207
d359172d11be
Applied some more code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8143
diff
changeset
|
38 | class BaseDocstringGenerator: |
7998 | 39 | """ |
40 | Class implementing a docstring generator base class. | |
41 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
42 | |
7998 | 43 | def __init__(self, editor): |
44 | """ | |
45 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
46 | |
7998 | 47 | @param editor reference to the editor widget |
48 | @type Editor | |
49 | """ | |
50 | self.editor = editor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
51 | |
7998 | 52 | def isFunctionStart(self, text): |
53 | """ | |
54 | Public method to test, if a text is the start of a function or method | |
55 | definition. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
56 | |
7998 | 57 | @param text line of text to be tested |
58 | @type str | |
59 | @return flag indicating that the given text starts a function or | |
60 | method definition (always False) | |
61 | @rtype bool | |
62 | """ | |
63 | return False | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
64 | |
7998 | 65 | def hasFunctionDefinition(self, cursorPosition): |
66 | """ | |
67 | Public method to test, if the cursor is right below a function | |
68 | definition. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
69 | |
7998 | 70 | @param cursorPosition current cursor position (line and column) |
71 | @type tuple of (int, int) | |
72 | @return flag indicating cursor is right below a function definition | |
73 | @rtype bool | |
74 | """ | |
75 | return False | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
76 | |
7998 | 77 | def isDocstringIntro(self, cursorPosition): |
78 | """ | |
79 | Public function to test, if the line up to the cursor position might be | |
80 | introducing a docstring. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
81 | |
7998 | 82 | @param cursorPosition current cursor position (line and column) |
83 | @type tuple of (int, int) | |
84 | @return flag indicating a potential start of a docstring | |
85 | @rtype bool | |
86 | """ | |
87 | return False | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
88 | |
7998 | 89 | def insertDocstring(self, cursorPosition, fromStart=True): |
90 | """ | |
91 | Public method to insert a docstring for the function at the cursor | |
92 | position. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
93 | |
7998 | 94 | @param cursorPosition position of the cursor (line and index) |
95 | @type tuple of (int, int) | |
96 | @param fromStart flag indicating that the editor text cursor is placed | |
97 | on the line starting the function definition | |
98 | @type bool | |
99 | """ | |
100 | # just do nothing in the base class | |
101 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
102 | |
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
|
103 | def insertDocstringFromShortcut(self, cursorPosition): |
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
|
104 | """ |
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
|
105 | Public method to insert a docstring for the function at the cursor |
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
|
106 | position initiated via a keyboard shortcut. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
107 | |
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
|
108 | @param cursorPosition position of the cursor (line and index) |
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
|
109 | @type tuple of (int, int) |
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
|
110 | """ |
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
|
111 | # just do nothing in the base class |
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
|
112 | return |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
113 | |
7998 | 114 | def getDocstringType(self): |
115 | """ | |
116 | Public method to determine the docstring type to be generated. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
117 | |
7998 | 118 | @return docstring type (one of 'ericdoc', 'numpydoc', 'googledoc', |
119 | 'sphinxdoc') | |
120 | @rtype str | |
121 | """ | |
122 | docstringStyle = "" | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
123 | |
8356
68ec9c3d4de5
Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8318
diff
changeset
|
124 | project = ericApp().getObject("Project") |
7998 | 125 | filename = self.editor.getFileName() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
126 | if filename and project.isOpen() and project.isProjectFile(filename): |
7998 | 127 | docstringStyle = project.getDocstringType().lower() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
128 | |
7998 | 129 | if docstringStyle == "": |
130 | docstringStyle = Preferences.getEditor("DocstringType") | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
131 | |
7998 | 132 | return docstringStyle |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
133 | |
7998 | 134 | def _generateDocstringList(self, functionInfo, docstringType): |
135 | """ | |
136 | Protected method to generate type specific docstrings based on the | |
137 | extracted function information. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
138 | |
7998 | 139 | @param functionInfo reference to the function info object |
140 | @type FunctionInfo | |
141 | @param docstringType kind of docstring to be generated | |
142 | @return list of docstring lines | |
143 | @rtype str | |
144 | """ | |
9486
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
145 | generatorModuleMapping = { |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
146 | "ericdoc": "EricdocGenerator", |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
147 | "numpydoc": "NumpydocGenerator", |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
148 | "goodledoc": "GoogledocGenerator", |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
149 | "sphinxdoc": "SphinxdocGenerator", |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
150 | } |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
151 | if docstringType in generatorModuleMapping: |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
152 | mod = importlib.import_module( |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
153 | "eric7.QScintilla.DocstringGenerator.{0}".format( |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
154 | generatorModuleMapping[docstringType] |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
155 | ) |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
156 | ) |
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
157 | return mod.generateDoc(functionInfo, self.editor) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
158 | |
9486
5a8179763e38
Changed docstring generator imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
159 | return [] |
7998 | 160 | |
161 | ||
8207
d359172d11be
Applied some more code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8143
diff
changeset
|
162 | class FunctionInfo: |
7998 | 163 | """ |
164 | Class implementing an object to store function information. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
165 | |
7998 | 166 | Methods to extract the relevant information need to be implemented in |
167 | language specific subclasses. | |
168 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
169 | |
7998 | 170 | def __init__(self): |
171 | """ | |
172 | Constructor | |
173 | """ | |
174 | self.hasInfo = False | |
175 | self.funcionText = "" | |
176 | self.argumentsText = "" | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
177 | |
7998 | 178 | self.functionIndent = "" |
179 | # indentation fo function definition | |
180 | self.argumentsList = [] | |
181 | # list of tuples with name, type and value | |
182 | self.returnTypeAnnotated = None | |
183 | # return type extracted from type annotation | |
184 | self.returnValueInBody = [] | |
185 | # return values extracted from function body | |
186 | self.raiseList = None | |
187 | # exceptions raised by function | |
188 | self.hasYield = False | |
189 | # function is a generator | |
190 | self.functionType = "" | |
191 | # function type with these values | |
192 | # classmethod, staticmethod, qtslot, constructor or empty (i.e. | |
193 | # standard) | |
194 | self.isAsync = False | |
195 | # function is an asynchronous function, i.e. async def f(): | |
196 | self.visibility = "" | |
197 | # function visibility with allowed values: | |
198 | # public, protected, private or special (i.e. starting and | |
199 | # ending with '__' | |
200 | ||
201 | ||
202 | class DocstringMenuForEnterOnly(QMenu): | |
203 | """ | |
204 | Class implementing a special menu reacting to the enter/return keys only. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
205 | |
7998 | 206 | If a keyboard input is not the "enter key", the menu is closed and the |
207 | input is inserted to the code editor. | |
208 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
209 | |
7998 | 210 | def __init__(self, editor): |
211 | """ | |
212 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
213 | |
7998 | 214 | @param editor reference to the editor |
215 | @type Editor | |
216 | """ | |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8207
diff
changeset
|
217 | super().__init__(editor) |
7998 | 218 | self.__editor = editor |
219 | ||
220 | def keyPressEvent(self, evt): | |
221 | """ | |
222 | Protected method to handle key press events. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
223 | |
7998 | 224 | @param evt reference to the key press event object |
225 | @type QKeyEvent | |
226 | """ | |
227 | key = evt.key() | |
8143
2c730d5fd177
Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8000
diff
changeset
|
228 | if key not in (Qt.Key.Key_Enter, Qt.Key.Key_Return): |
7998 | 229 | self.__editor.keyPressEvent(evt) |
230 | self.close() | |
231 | else: | |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8207
diff
changeset
|
232 | super().keyPressEvent(evt) |