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