1 | import inspect |
||
2 | from collections import OrderedDict |
||
3 | |||
4 | from coalib.misc.Enum import enum |
||
5 | |||
6 | |||
7 | class DocumentationComment: |
||
8 | _ParseMode = enum("DESCRIPTION", "PARAM", "RETVAL") |
||
9 | |||
10 | def __init__(self, desc, param_dict, retval_desc): |
||
11 | """ |
||
12 | Represents a documentation comment of a python class or function. |
||
13 | |||
14 | :param desc: A description as string. |
||
15 | :param param_dict: A dictionary containing parameter names as key and |
||
16 | their description as value. To preserve the order, |
||
17 | use OrderedDict. |
||
18 | :param retval_desc: A string describing the return value. |
||
19 | """ |
||
20 | self.desc = desc |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
21 | self.param_dict = param_dict |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
22 | self.retval_desc = retval_desc |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
23 | |||
24 | @classmethod |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
25 | def from_docstring(cls, docstring): |
||
26 | """ |
||
27 | Parses a python docstring. Usable attributes are: |
||
28 | :param |
||
29 | @param |
||
30 | :return |
||
31 | @return |
||
32 | """ |
||
33 | lines = inspect.cleandoc(docstring).split("\n") |
||
34 | |||
35 | parse_mode = cls._ParseMode.DESCRIPTION |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
36 | cur_param = "" |
||
37 | |||
38 | desc = "" |
||
39 | param_dict = OrderedDict() |
||
40 | retval_desc = "" |
||
41 | for line in lines: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
42 | line = line.strip() |
||
43 | |||
44 | if line == "": |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
45 | parse_mode = cls._ParseMode.DESCRIPTION |
||
46 | |||
47 | continue |
||
48 | |||
49 | if line.startswith(":param ") or line.startswith("@param "): |
||
50 | parse_mode = cls._ParseMode.PARAM |
||
51 | splitted = line[7:].split(":", 1) |
||
52 | cur_param = splitted[0] |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
53 | param_dict[cur_param] = splitted[1].strip() |
||
54 | |||
55 | continue |
||
56 | |||
57 | if line.startswith(":return: ") or line.startswith("@return: "): |
||
58 | parse_mode = cls._ParseMode.RETVAL |
||
59 | retval_desc = line[9:].strip() |
||
60 | |||
61 | continue |
||
62 | |||
63 | if parse_mode == cls._ParseMode.RETVAL: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
64 | retval_desc += " " + line |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
65 | elif parse_mode == cls._ParseMode.PARAM: |
||
66 | param_dict[cur_param] += " " + line |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
67 | else: |
||
68 | desc += " " + line |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
69 | |||
70 | return (cls(desc=desc.strip(), |
||
71 | param_dict=param_dict, |
||
72 | retval_desc=retval_desc.strip())) |
||
73 | |||
74 | def __str__(self): |
||
75 | return str(self.desc) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
76 |