|
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 'numpydoc' style. |
|
8 """ |
|
9 |
|
10 |
|
11 def generateNumpyDoc(functionInfo, editor): |
|
12 """ |
|
13 Function to generate the docstring line list iaw. NumPy 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("Parameters") |
|
50 lines.append("----------") |
|
51 for argName, argType, argValue in functionInfo.argumentsList: |
|
52 argLine = "{0} : ".format(argName) |
|
53 if argType: |
|
54 argLine += "{0}".format(argType) |
|
55 else: |
|
56 argLine += "TYPE" |
|
57 if argValue: |
|
58 argLine += ", optional" |
|
59 lines.append(argLine) |
|
60 argLine = "{0}DESCRIPTION.".format(indent) |
|
61 if argValue: |
|
62 argLine += " The default is {0}".format(argValue) |
|
63 lines.append(argLine) |
|
64 |
|
65 # add an exceptions section, if function raises something |
|
66 if functionInfo.raiseList: |
|
67 lines.append("") |
|
68 lines.append("Raises") |
|
69 lines.append("------") |
|
70 for exc in sorted(functionInfo.raiseList): |
|
71 lines.append("{0}".format(exc)) |
|
72 lines.append("{0}DESCRIPTION".format(indent)) |
|
73 |
|
74 # add return section |
|
75 lines.append("") |
|
76 if functionInfo.hasYield: |
|
77 lines.append("Yields") |
|
78 lines.append("------") |
|
79 else: |
|
80 lines.append("Returns") |
|
81 lines.append("-------") |
|
82 if functionInfo.returnTypeAnnotated: |
|
83 lines.append("{0}".format(functionInfo.returnTypeAnnotated)) |
|
84 lines.append("{0}DESCRIPTION.".format(indent)) |
|
85 elif functionInfo.returnValueInBody: |
|
86 lines.append("TYPE") |
|
87 lines.append("{0}DESCRIPTION.".format(indent)) |
|
88 else: |
|
89 lines.append("{0}None".format(indent)) |
|
90 |
|
91 return lines |