-
Notifications
You must be signed in to change notification settings - Fork 11
/
ontodoc
executable file
·293 lines (276 loc) · 9.05 KB
/
ontodoc
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
#!/usr/bin/env python3
"""`ontodoc` CLI tool."""
# pylint: disable=wrong-import-position,wrong-import-order
import os
import sys
import argparse
import subprocess # nosec
from pathlib import Path
# Support running from uninstalled package by adding parent dir to sys path
rootdir = os.path.abspath(
os.path.realpath((os.path.dirname(os.path.dirname(__file__))))
)
if rootdir not in sys.path:
sys.path.insert(1, rootdir)
from ontopy import World, onto_path # pylint: disable=import-error
from ontopy.ontodoc import ( # pylint: disable=import-error
OntoDoc,
get_style,
get_figformat,
get_maxwidth,
get_docpp,
)
from ontopy.ontodoc_rst import OntologyDocumentation
from ontopy.utils import get_format
import owlready2
def main(argv: list = None):
"""Main run function.
Parameters:
argv: List of arguments, similar to `sys.argv[1:]`.
Mainly for testing purposes, since it allows one to invoke the tool
manually / through Python.
"""
# pylint: disable=too-many-locals,too-many-statements
parser = argparse.ArgumentParser(
description="Tool for documenting ontologies.",
epilog=(
"The easiest way to generate nice-looking documentation using "
"pandoc is to copy the emmodoc example to the current directory "
"and adapt it. See "
"https://github.com/emmo-repo/EMMOntoPy/tree/master/examples/"
"emmodoc for more info."
),
)
parser.add_argument(
"iri",
metavar="IRI",
help="File name or URI of the ontology to document.",
)
parser.add_argument("outfile", metavar="OUTFILE", help="Output file.")
parser.add_argument(
"--database",
"-d",
metavar="FILENAME",
default=":memory:",
help=(
"Load ontology from Owlready2 sqlite3 database. The `iri` argument"
" should in this case be the IRI of the ontology you want to "
"document."
),
)
parser.add_argument(
"--local",
"-l",
action="store_true",
help=(
"Load imported ontologies locally. Their paths are specified in "
"Protègè catalog files or via the --path option. The IRI should "
"be a file name."
),
)
parser.add_argument(
"--imported",
"-i",
action="store_true",
help="Whether to also include imported ontologies.",
)
parser.add_argument(
"--no-catalog",
"-n",
action="store_false",
dest="url_from_catalog",
default=None,
help="Whether to not read catalog file even if it exists.",
)
parser.add_argument(
"--catalog-file",
default="catalog-v001.xml",
help=(
"Name of Protègè catalog file in the same folder as the ontology. "
"This option is used together with --local and defaults to "
'"catalog-v001.xml".'
),
)
parser.add_argument(
"--path",
action="append",
default=[],
help=(
"Paths where imported ontologies can be found. May be provided as "
"a comma-separated string and/or with multiple --path options."
),
)
parser.add_argument(
"--reasoner",
nargs="?",
const="HermiT",
choices=["HermiT", "Pellet", "FaCT++"],
help=(
'Run given reasoner on the ontology. Valid reasoners are "HermiT" '
'(default), "Pellet" and "FaCT++".'
),
)
parser.add_argument(
"--template",
"-t",
metavar="FILE",
help=(
"ontodoc input template. If not provided, a simple default "
"template will be used. Don't confuse it with the pandoc "
"templates."
),
)
parser.add_argument(
"--format",
"-f",
metavar="FORMAT",
help=(
'Output format. May be "rst", "md", "simple-html" or any other '
"format supported by pandoc. By default the format is inferred "
"from OUTFILE."
),
)
parser.add_argument(
"--figdir",
"-D",
metavar="DIR",
default="genfigs",
help=(
"Default directory to store generated figures. If a relative path "
"is given, it is relative to the template (see --template), or the"
" current directory, if --template is not given. Default: "
'"genfigs"'
),
)
parser.add_argument(
"--figformat",
"-F",
help=(
"Format for generated figures. The default is inferred from "
'"--format."'
),
)
parser.add_argument(
"--max-figwidth",
"-w",
help="Maximum figure width. The default is inferred from --format.",
)
parser.add_argument(
"--pandoc-option",
"-p",
metavar="STRING",
action="append",
dest="pandoc_options",
help=(
"Additional pandoc long options overriding those read from "
'--pandoc-option-file. Use "--pandoc-option=XXX=Y" to add pandoc '
"option --XXX=Y. It is also possible to remove pandoc option --XXX"
' with "--pandoc-option=no-XXX". This option may be provided '
"multiple times."
),
)
parser.add_argument(
"--pandoc-option-file",
"-P",
metavar="FILE",
action="append",
dest="pandoc_option_files",
help=(
"YAML file with additional pandoc options. Note, that default "
'pandoc options are read from the files "pandoc-options.yaml" and '
'"pandoc-FORMAT-options.yaml" (where FORMAT is format specified '
"with --format). This option allows to override the defaults and "
"add additional pandoc options. This option may be provided "
"multiple times."
),
)
parser.add_argument(
"--keep-generated",
"-k",
metavar="FILE",
dest="genfile",
help=(
"Keep a copy of generated markdown input file for pandoc (for "
"debugging)."
),
)
parser.add_argument(
"--iri-regex",
"-r",
metavar="REGEX",
help=(
"Regular expression matching IRIs to include in the documentation."
),
)
args = parser.parse_args(args=argv)
# Append to onto_path
for paths in args.path:
for path in paths.split(","):
if path not in onto_path:
onto_path.append(path)
# Load ontology
iri = args.iri if args.iri[-1] in "#/" else f"{args.iri}#"
world = World(filename=args.database)
if args.database != ":memory:" and iri not in world.ontologies:
parser.error(
"The IRI argument should be one of the ontologies in the database:"
"\n " + "\n ".join(world.ontologies.keys())
)
onto = world.get_ontology(args.iri)
try:
onto.load(
only_local=args.local,
url_from_catalog=args.url_from_catalog,
catalog_file=args.catalog_file,
)
except owlready2.OwlReadyOntologyParsingError as exc:
parser.error(f"error parsing {args.iri!r}: {exc}")
# Sync reasoner
if args.reasoner:
onto.sync_reasoner(reasoner=args.reasoner)
# Get output format
fmt = get_format(args.outfile, default="html", fmt=args.format)
if fmt == "rst":
# New reStructuredText format
od = OntologyDocumentation(
onto,
recursive=args.imported,
iri_regex=args.iri_regex,
)
docfile = Path(args.outfile)
indexfile = docfile.with_name("index.rst")
conffile = docfile.with_name("conf.py")
od.write_refdoc(docfile=docfile)
if not indexfile.exists():
print(f"Generating index template: {indexfile}")
od.write_index_template(indexfile=indexfile, docfile=docfile)
if not conffile.exists():
print(f"Generating configuration template: {conffile}")
od.write_conf_template(conffile=conffile, docfile=docfile)
else:
# Instantiate old ontodoc instance
style = get_style(fmt)
figformat = args.figformat if args.figformat else get_figformat(fmt)
maxwidth = args.max_figwidth if args.max_figwidth else get_maxwidth(fmt)
ontodoc = OntoDoc(onto, style=style)
docpp = get_docpp(
ontodoc,
args.template,
figdir=args.figdir,
figformat=figformat,
maxwidth=maxwidth,
imported=args.imported,
)
docpp.process()
try:
docpp.write(
args.outfile,
fmt=args.format,
pandoc_option_files=args.pandoc_option_files,
pandoc_options=args.pandoc_options,
genfile=args.genfile,
)
except subprocess.CalledProcessError as exc:
sys.exit(exc.returncode) # Exit without traceback on pandoc errors
if __name__ == "__main__":
main()