-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathbuild-doc.py
300 lines (235 loc) · 10.8 KB
/
build-doc.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import os
import OpenAttack
def getSubClasses(module, clss):
ret = []
for name in dir(module):
try:
if issubclass(module.__dict__[name], clss):
ret.append(name)
except TypeError:
continue
return ret
def getDocMembers(clss):
ret = []
for kw in dir(clss):
if kw.startswith("_"):
continue
if clss.__dict__[kw].__doc__ is not None:
ret.append(kw)
return ret
def make_attacker(path):
addition_members = {
"UATAttacker": ["get_triggers"],
}
opt = "===================\nAttackers API\n===================\n\n"
opt += "Attacker\n=============\n\n.. autoclass:: OpenAttack.Attacker\n :members:\n\n"
opt += "-" * 36 + "\n\n"
opt += "ClassificationAttacker\n==============================\n\n.. autoclass:: OpenAttack.attackers.ClassificationAttacker\n :members:\n\n"
opt += "-" * 36 + "\n\n"
for name in getSubClasses(OpenAttack.attackers, OpenAttack.ClassificationAttacker):
if name == "ClassificationAttacker":
continue
opt += name + "\n" + ("-" * (2 + len(name))) + "\n\n"
opt += ".. autoclass:: OpenAttack.attackers.%s(OpenAttack.attackers.ClassificationAttacker)\n" % name
members = ["__init__"] + (addition_members[name] if name in addition_members else [])
opt += " :members: " + (", ".join(members)) + "\n\n"
open(path, "w", encoding="utf-8").write(opt)
return opt
def make_attack_eval(path):
opt = "========================\nAttackEvals API\n========================\n\n"
opt += "AttackEval\n----------------\n\n.. autoclass:: OpenAttack.AttackEval\n :members: __init__, eval, ieval\n\n"
open(path, "w", encoding="utf-8").write(opt)
def make_victim(path):
addition_members = {
"TransformersClassifier": ["to"],
}
skip_list = {"Classifier"}
opt = "===================\nVictims API\n===================\n\n"
opt += "Classifier\n===========================\n\n.. autoclass:: OpenAttack.victim.classifiers.Classifier\n :members:\n\n"
opt += "-" * 36 + "\n\n"
for name in getSubClasses(OpenAttack.victim.classifiers, OpenAttack.victim.classifiers.Classifier):
if name in skip_list:
continue
opt += name + "\n" + ("-" * (2 + len(name))) + "\n\n"
opt += ".. autoclass:: OpenAttack.classifiers.%s(OpenAttack.Classifier)\n" % name
members = ["__init__"] + (addition_members[name] if name in addition_members else [])
opt += " :members: " + (", ".join(members)) + "\n\n"
open(path, "w", encoding="utf-8").write(opt)
return opt
def make_data_manager(path):
opt = "===================\nDataManager API\n===================\n\n.. autoclass:: OpenAttack.DataManager\n :members:"
open(path, "w", encoding="utf-8").write(opt)
return opt
def make_data(path):
import pkgutil
cats = {
}
for data in pkgutil.iter_modules(OpenAttack.data.__path__):
data = data.module_finder.find_loader(data.name)[0].load_module()
if hasattr(data, "NAME") and (data.NAME in OpenAttack.DataManager.AVAILABLE_DATAS):
name = data.NAME
if name == "test":
continue
cat = name.split(".")
if len(cat) == 1:
continue
name = ".".join(cat[1:])
cat = cat[0]
pack = data.__name__
if cat not in cats:
cats[cat] = []
cats[cat].append({
"name": name,
"package": pack
})
for cat in cats.keys():
opt = "=====================\n%s\n=====================\n\n" % cat
opt += ".. _label-data-%s:\n\n" % cat
for data in cats[cat]:
opt += data["name"] + "\n" + ("-" * (2 + len(data["name"]))) + "\n\n"
opt += ".. py:data:: " + cat + "." + data["name"] + "\n\n"
opt += " .. automodule:: OpenAttack.data." + data["package"] + "\n\n"
open(os.path.join(path, cat + ".rst"), "w", encoding="utf-8").write(opt)
return opt
def make_metric(path):
opt = "==================\nMetric API\n==================\n\n"
metrics = []
selector = []
for name in dir(OpenAttack.metric):
if isinstance(OpenAttack.metric.__dict__[name], type):
if issubclass(OpenAttack.metric.__dict__[name], OpenAttack.metric.AttackMetric):
if name == "AttackMetric":
continue
metrics.append(name)
elif issubclass(OpenAttack.metric.__dict__[name], OpenAttack.metric.MetricSelector):
if name == "MetricSelector":
continue
selector.append(name)
opt += """Attacker Metrics
==================
.. autoclass:: OpenAttack.metric.AttackMetric
:members:
"""
for name in metrics:
cls = OpenAttack.metric.__dict__[name]
opt += name + "\n" + ("-" * (2 + len(name))) + "\n\n"
opt += ".. autoclass:: OpenAttack.metric." + name + "\n"
if hasattr(cls, "calc_score"):
opt += " :members: __init__, calc_score" + "\n"
else:
opt += " :members: __init__" + "\n"
opt += " :exclude-members: TAGS" + "\n\n"
opt += """Metrics Selector
=======================
.. autoclass:: OpenAttack.metric.MetricSelector
:members:
"""
for name in selector:
opt += name + "\n" + ("-" * (2 + len(name))) + "\n\n"
opt += ".. autoclass:: OpenAttack.metric." + name + "\n"
opt += " :members: " + "\n"
opt += " :exclude-members: TAGS" + "\n\n"
open(path, "w", encoding="utf-8").write(opt)
return opt
def make_substitute(path):
opt = "======================\nSubstitutes API\n======================\n\n"
opt += """
Abstract Classes
------------------------
.. autoclass:: OpenAttack.attack_assist.substitute.word.WordSubstitute
:members: __call__
.. autoclass:: OpenAttack.attack_assist.substitute.char.CharSubstitute
:members: __call__
-------------------------------------------------------------------------------
"""
subs = getSubClasses(OpenAttack.attack_assist.substitute.word, OpenAttack.attack_assist.substitute.word.WordSubstitute)
embed_based_idx = subs.index("EmbedBasedSubstitute")
if embed_based_idx != -1:
subs[0], subs[embed_based_idx] = subs[embed_based_idx], subs[0]
for name in subs:
cls = OpenAttack.attack_assist.substitute.word.__dict__[name]
if cls is OpenAttack.attack_assist.substitute.word.WordSubstitute:
continue
opt += name + "\n" + ("-" * (2 + len(name))) + "\n\n"
opt += ".. autoclass:: OpenAttack.attack_assist.substitute.word.%s(OpenAttack.attack_assist.substitute.word.WordSubstitute)\n" % name
opt += " :members: __init__\n\n"
subs = getSubClasses(OpenAttack.attack_assist.substitute.char, OpenAttack.attack_assist.substitute.char.CharSubstitute)
for name in subs:
cls = OpenAttack.attack_assist.substitute.char.__dict__[name]
if cls is OpenAttack.attack_assist.substitute.char.CharSubstitute:
continue
opt += name + "\n" + ("-" * (2 + len(name))) + "\n\n"
opt += ".. autoclass:: OpenAttack.attack_assist.substitute.char.%s(OpenAttack.attack_assist.substitute.char.CharSubstitute)\n" % name
opt += " :members: __init__\n\n"
open(path, "w", encoding="utf-8").write(opt)
return opt
def make_text_processor(path):
opt = "========================\nText Processors API\n========================\n\n"
opt += "Tokenizers\n============================\n\n"
opt += """
.. autoclass:: OpenAttack.text_process.tokenizer.Tokenizer
:members: tokenize, detokenize
"""
import OpenAttack.text_process.tokenizer
for name in getSubClasses(OpenAttack.text_process.tokenizer, OpenAttack.text_process.tokenizer.Tokenizer):
if name == "Tokenizer":
continue
opt += name + "\n" + ("-" * (2 + len(name))) + "\n\n"
opt += ".. autoclass:: OpenAttack.text_process.tokenizer.%s(OpenAttack.text_process.tokenizer.Tokenizer)\n" % name
opt += " :members:\n\n"
import OpenAttack.text_process.lemmatizer
opt += "Lemmatizer\n============================\n\n"
opt += """
.. autoclass:: OpenAttack.text_process.lemmatizer.Lemmatizer
:members: lemmatize, delemmatize
"""
for name in getSubClasses(OpenAttack.text_process.lemmatizer, OpenAttack.text_process.lemmatizer.Lemmatizer):
if name == "Lemmatizer":
continue
opt += name + "\n" + ("-" * (2 + len(name))) + "\n\n"
opt += ".. autoclass:: OpenAttack.text_process.lemmatizer.%s(OpenAttack.text_process.lemmatizer.Lemmatizer)\n" % name
opt += " :members:\n\n"
import OpenAttack.text_process.constituency_parser
opt += "ConstituencyParser\n============================\n\n"
opt += """
.. autoclass:: OpenAttack.text_process.constituency_parser.ConstituencyParser
:members: __call__
"""
for name in getSubClasses(OpenAttack.text_process.constituency_parser, OpenAttack.text_process.constituency_parser.ConstituencyParser):
if name == "ConstituencyParser":
continue
opt += name + "\n" + ("-" * (2 + len(name))) + "\n\n"
opt += ".. autoclass:: OpenAttack.text_process.constituency_parser.%s(OpenAttack.text_process.constituency_parser.ConstituencyParser)\n" % name
opt += " :members:\n\n"
open(path, "w", encoding="utf-8").write(opt)
return opt
def make_utils(path):
opt = "=====================\nutils API\n=====================\n\n"
for name in OpenAttack.utils.__dir__():
if name.startswith("__"):
continue
obj = OpenAttack.utils.__dict__[name]
if type(obj).__name__ == "module":
continue
opt += name + "\n" + ("-" * (2 + len(name))) + "\n\n"
if type(obj).__name__ == "function":
opt += ".. autofunction:: OpenAttack.utils." + name + "\n\n"
else:
opt += ".. autoclass:: OpenAttack.utils." + name + "\n"
opt += " :members: " + "\n\n"
open(path, "w", encoding="utf-8").write(opt)
return opt
def main(path):
make_attacker(os.path.join(path, "attacker.rst"))
make_attack_eval(os.path.join(path, "attack_eval.rst"))
make_victim(os.path.join(path, "victim.rst"))
make_data_manager(os.path.join(path, "data_manager.rst"))
make_data(os.path.join(path, "..", "data"))
make_metric(os.path.join(path, "metric.rst"))
make_substitute(os.path.join(path, "substitute.rst"))
make_text_processor(os.path.join(path, "text_processor.rst"))
make_utils(os.path.join(path, "utils.rst"))
if __name__ == "__main__":
import sys
path = os.path.abspath(sys.argv[1])
main(path)