|
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 'sphinxdoc' style. |
|
8 """ |
|
9 |
|
10 |
|
11 def generateSphinxDoc(functionInfo): |
|
12 """ |
|
13 Function to generate the docstring line list iaw. Sphinx 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 @return list of docstring lines |
|
23 @rtype str |
|
24 """ |
|
25 # __IGNORE_WARNING_D202__ |
|
26 lines = [] |
|
27 |
|
28 # function description |
|
29 lines.append("") |
|
30 |
|
31 # remove 'self', 'this' or 'cls' from arguments list |
|
32 if ( |
|
33 len(functionInfo.argumentsList) > 0 and |
|
34 functionInfo.argumentsList[0][0] in ("self", "cls", "this") |
|
35 ): |
|
36 del functionInfo.argumentsList[0] |
|
37 |
|
38 # add an empty line if there is one of the other sections |
|
39 if ( |
|
40 functionInfo.argumentsList or |
|
41 functionInfo.hasYield or |
|
42 functionInfo.returnTypeAnnotated or |
|
43 functionInfo.returnValueInBody or |
|
44 functionInfo.raiseList |
|
45 ): |
|
46 lines.append("") |
|
47 |
|
48 # add the parameters section |
|
49 for argName, argType, argValue in functionInfo.argumentsList: |
|
50 argLine = ":param {0}: DESCRIPTION".format(argName) |
|
51 if argValue: |
|
52 argLine += ", defaults to {0}".format(argValue) |
|
53 lines.append(argLine) |
|
54 |
|
55 argLine = ":type {0}: ".format(argName) |
|
56 if argType: |
|
57 argLine += "{0}".format(argType) |
|
58 else: |
|
59 argLine += "TYPE" |
|
60 if argValue: |
|
61 argLine += ", optional" |
|
62 lines.append(argLine) |
|
63 |
|
64 # add an exceptions section, if function raises something |
|
65 if functionInfo.raiseList: |
|
66 for exc in sorted(functionInfo.raiseList): |
|
67 lines.append(":raises {0}: DESCRIPTION".format(exc)) |
|
68 |
|
69 # add return section |
|
70 if ( |
|
71 functionInfo.hasYield or |
|
72 functionInfo.returnValueInBody or |
|
73 functionInfo.returnTypeAnnotated |
|
74 ): |
|
75 if functionInfo.hasYield: |
|
76 lines.append(":yield: DESCRIPTION") |
|
77 else: |
|
78 lines.append(":return: DESCRIPTION") |
|
79 if functionInfo.returnTypeAnnotated: |
|
80 lines.append(":rtype: {0}".format( |
|
81 functionInfo.returnTypeAnnotated)) |
|
82 else: |
|
83 lines.append(":rtype: TYPE") |
|
84 |
|
85 lines.append("") |
|
86 |
|
87 return lines |