-
Notifications
You must be signed in to change notification settings - Fork 9
/
ontoversion
executable file
·67 lines (53 loc) · 1.77 KB
/
ontoversion
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
#!/usr/bin/env python3
"""Prints version of an ontology to standard output.
This script uses rdflib and the versionIRI tag on an ontology to infer
the version.
"""
import argparse
import sys
import rdflib
from rdflib.util import guess_format
from ontopy.utils import infer_version, FMAP
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.
"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"iri",
metavar="IRI",
help="IRI/file to OWL source to extract the version from.",
)
parser.add_argument(
"--format",
"-f",
choices=set(list(FMAP.keys()) + list(FMAP.values())),
help="Ontology format. Default: Guess format with rdflib.guess_format.",
) # add accepted formats
try:
args = parser.parse_args(args=argv)
except SystemExit as exc:
sys.exit(exc.code) # Exit without traceback on invalid arguments
# Extract base IRI and versionIRI
graph = rdflib.Graph()
# Guess format if format not given
fmt = args.format if args.format else guess_format(args.iri, fmap=FMAP)
try:
graph.parse(args.iri, format=fmt)
except Exception as err: # pylint: disable=W0703
print("rdflib could not parse the ontology.")
print(err)
sys.exit()
iri, version_iri = list(
graph.subject_objects(
rdflib.URIRef("http://www.w3.org/2002/07/owl#versionIRI")
)
)[0]
# Infer version from IRI and versionIRI
version = infer_version(iri, version_iri)
print(version)
if __name__ == "__main__":
main()