-
Notifications
You must be signed in to change notification settings - Fork 0
/
missingDocs.py
208 lines (165 loc) · 7.15 KB
/
missingDocs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ***************************************************************************
# * Copyright (c) 2015-2024 by Pierre-Henri WUILLEMIN *
# * {prenom.nom}_at_lip6.fr *
# * *
# * "act" is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License as published by *
# * the Free Software Foundation; either version 2 of the License, or *
# * (at your option) any later version. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU General Public License for more details. *
# * *
# * You should have received a copy of the GNU General Public License *
# * along with this program; if not, write to the *
# * Free Software Foundation, Inc., *
# * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
# **************************************************************************
from typing import Iterable
import types
import inspect
import sys
from os.path import join, dirname
from .utils import notif
gumPath = join(dirname(sys.argv[0]), "build/pyAgrum/release/wrappers")
def _prefix(name: str) -> str:
return " " * (4 * (name.count(".") - 1)) + "- "
class PyAgrumDocCoverage:
def __init__(self, verbose: bool):
self._verbose = verbose
self.nb_class = 0
self.nb_meth = 0
self.nb_func = 0
self.undoc_class = []
self.undoc_func = []
self.undoc_meth = []
self.partial_doc_class = []
self.partial_doc_func = []
self.partial_doc_meth = []
@staticmethod
def _is_not_valid(msg: str) -> bool:
# deprecated does not follow the rules of validity for documentation
if msg.strip().startswith("Deprecated"):
return False
# msg les than 3 lines are not valid
if msg.count("\n") < 3:
return True
if "'PyObject *'" in msg:
return True
return False
def _check_function_oc(self, name: str, func: str):
res = "check"
is_meth = name.count(".") > 1
if is_meth:
self.nb_meth += 1
else:
self.nb_func += 1
if not hasattr(func, "__doc__") or func.__doc__ is None:
if is_meth:
self.undoc_meth.append(name)
else:
self.undoc_func.append(name)
res = "no doc"
else:
if self._is_not_valid(func.__doc__):
if is_meth:
self.partial_doc_meth.append(name)
else:
self.partial_doc_func.append(name)
res = "partial"
if self._verbose:
notif(_prefix(name) + name + " : " + res)
def _check_class_doc(self, name: str, clas: str):
res = "check"
self.nb_class += 1
if not hasattr(clas, "__doc__") or clas.__doc__ is None:
self.undoc_class.append(name)
res = "no doc"
else:
if self._is_not_valid(clas.__doc__):
self.partial_doc_class.append(name)
res = "partial"
if self._verbose:
notif(_prefix(name) + name + " : " + res)
@staticmethod
def _ignored_class(clas: str) -> bool:
if gumPath not in sys.path:
sys.path.insert(0, gumPath)
import pyAgrum as gum
return clas in {cls.__name__ for cls in gum.GumException.__subclasses__()}
def _traversal(self, entities: Iterable[str], container: str):
import pyAgrum as gum
for entity in entities:
if entity[0] != '_':
complete_entity_name = container + "." + entity
instance_entity = eval(complete_entity_name)
if type(instance_entity) is types.FunctionType:
self._check_function_oc(complete_entity_name, instance_entity)
elif inspect.isclass(instance_entity):
if not self._ignored_class(instance_entity.__name__):
self._check_class_doc(complete_entity_name, instance_entity.__name__)
self._traversal(dir(instance_entity), complete_entity_name)
def check_missing_doc(self):
DELIM: str = "\n + "
if gumPath not in sys.path:
sys.path.insert(0, gumPath)
import pyAgrum as gum
self.nb_class = 0
self.nb_meth = 0
self.nb_func = 0
self.undoc_class = []
self.undoc_func = []
self.undoc_meth = []
self.partial_doc_class = []
self.partial_doc_func = []
self.partial_doc_meth = []
self._traversal(dir(gum), "gum")
pc = 1.0 - (len(self.undoc_class) + len(self.partial_doc_class)) / (1.0 * self.nb_class)
pm = 1.0 - (len(self.undoc_meth) + len(self.partial_doc_meth)) / (1.0 * self.nb_meth)
pf = 1.0 - (len(self.undoc_func) + len(self.partial_doc_func)) / (1.0 * self.nb_func)
tc = self.nb_class - (len(self.undoc_class) + len(self.partial_doc_class))
tm = self.nb_meth - (len(self.undoc_meth) + len(self.partial_doc_meth))
tf = self.nb_func - (len(self.undoc_func) + len(self.partial_doc_func))
notif(f'Documentation in pyAgrum {gum.__version__}')
notif(f" Classes : coverage={(pc * 100.0):6.2f}% [({tc}/{self.nb_class})]")
if self._verbose:
notif("---------")
notif(" - nbr of classes : " + str(self.nb_class))
notif(" - nbr of partially documented classes : " + str(len(self.partial_doc_class)))
notif(DELIM.join([""] + self.partial_doc_class))
notif()
notif(" - nbr of undocumented classes : " + str(len(self.undoc_class)))
notif(DELIM.join([""] + self.undoc_class))
notif(f" Methods : coverage={(pm * 100.0):6.2f}% [({tm}/{self.nb_meth})]")
if self._verbose:
notif("---------")
notif(" - nbr of methods: " + str(self.nb_meth))
notif(" - nbr of partially documented methods : " + str(len(self.partial_doc_meth)))
notif(DELIM.join([""] + self.partial_doc_meth))
notif()
notif(" - nbr of undocumented methods : " + str(len(self.undoc_meth)))
notif(DELIM.join([""] + self.undoc_meth))
notif(f" Functions : coverage={(pf * 100.0):6.2f}% [({tf}/{self.nb_func})]")
if self._verbose:
notif("-----------")
notif(" - nbr of functions: " + str(self.nb_func))
notif(" - nbr of partially documented functions : " + str(len(self.partial_doc_func)))
notif(DELIM.join([""] + self.partial_doc_func))
notif()
notif(" - nbr of undocumented functions : " + str(len(self.undoc_func)))
notif(DELIM.join([""] + self.undoc_func))
return (len(self.undoc_class) +
len(self.partial_doc_class) +
len(self.undoc_meth) +
len(self.partial_doc_meth) +
len(self.undoc_func) +
len(self.partial_doc_func))
def missingDocs(show_funct: bool = False):
return PyAgrumDocCoverage(verbose=show_funct).check_missing_doc()
if __name__ == "__main__":
# execute only if run as a script
notif(f"\nNbr of documentation errors: {missingDocs(show_funct=True)}")